parameters: (self, dataset, model="resnet50", batch_size=128)
return_statement: return features
docstring: Compute feature vectors for an image dataset using a neural network.
  
          Parameters:
              dataset (torch.utils.data.Dataset): dataset
              model (str or torch.nn.Module, optional): pretrained model.
                  If it is a str, use the last hidden model of that model.
              batch_size (int, optional): batch size
docstring_summary: Compute feature vectors for an image dataset using a neural network.
docstring_tokens: Compute feature vectors for an image dataset using a neural network .
function: def image_feature_data(self, dataset, model="resnet50", batch_size=128):
          """
          Compute feature vectors for an image dataset using a neural network.
  
          Parameters:
              dataset (torch.utils.data.Dataset): dataset
              model (str or torch.nn.Module, optional): pretrained model.
                  If it is a str, use the last hidden model of that model.
              batch_size (int, optional): batch size
          """
          import torch
          import torchvision
          from torch import nn
  
          logger.info("computing %s feature" % model)
          if isinstance(model, str):
              full_model = getattr(torchvision.models, model)(pretrained=True)
              model = nn.Sequential(*list(full_model.children())[:-1])
          num_worker = multiprocessing.cpu_count()
          data_loader = torch.utils.data.DataLoader(dataset,
                                                    batch_size=batch_size, num_workers=num_worker, shuffle=False)
          model = model.cuda()
          model.eval()
  
          features = []
          with torch.no_grad():
              for i, (batch_images, batch_labels) in enumerate(data_loader):
                  if i % 100 == 0:
                      logger.info("%g%%" % (100.0 * i * batch_size / len(dataset)))
                  batch_images = batch_images.cuda()
                  batch_features = model(batch_images).view(batch_images.size(0), -1).cpu().numpy()
                  features.append(batch_features)
          features = np.concatenate(features)
  
          return features
function_tokens: def image_feature_data ( self , dataset , model = "resnet50" , batch_size = 128 ) : import torch import torchvision from torch import nn logger . info ( "computing %s feature" % model ) if isinstance ( model , str ) : full_model = getattr ( torchvision . models , model ) ( pretrained = True ) model = nn . Sequential ( * list ( full_model . children ( ) ) [ : - 1 ] ) num_worker = multiprocessing . cpu_count ( ) data_loader = torch . utils . data . DataLoader ( dataset , batch_size = batch_size , num_workers = num_worker , shuffle = False ) model = model . cuda ( ) model . eval ( ) features = [ ] with torch . no_grad ( ) : for i , ( batch_images , batch_labels ) in enumerate ( data_loader ) : if i % 100 == 0 : logger . info ( "%g%%" % ( 100.0 * i * batch_size / len ( dataset ) ) ) batch_images = batch_images . cuda ( ) batch_features = model ( batch_images ) . view ( batch_images . size ( 0 ) , - 1 ) . cpu ( ) . numpy ( ) features . append ( batch_features ) features = np . concatenate ( features ) return features