如何使用 vgg16 为 ImageNet 准备和拆分数据集

How can I prepare and split data set for ImageNet with vgg16

 ''' I am trying to classify image using PyTorch but I did manage to 
  stipulate my our data set to use it with vgg16 architecture ''' 
    
# ADD YOUR CODE HERE
def evaluate():
  running_loss = 0.0 #    counter = 0
   # Tell torch not to calculate gradients
  with torch.no_grad():
    for i, data in enumerate(testloader, 0):
      # get the inputs; data is a list of [inputs, labels]
      inputs, labels = data
      # Move to device
      inputs = inputs.to(device = device)
      labels = labels.to(device = device)
      # Forward pass
      outputs = model(inputs)
      # Calculate Loss
      loss = criterion(outputs, labels)
      # Add loss to the validation set's running loss
      running_loss += loss.item()
    # Since our model  find the real percentages by  the  following 
  val_loss = running_loss / len(testloader)
  print('val loss: %.3f' % (val_loss))
     # Get the top class of the output
  return val_loss
## 1. Dataset 

加载给定的数据集。图像应存储在 X 变量中,标签应存储在 Y 变量中。将您的数据集拆分为训练集、验证集和测试集,并对数据进行预处理以进行训练。

def eval_acc(train=False):
  correct = 0
  total = 0
  # since we're not training, we don't need to calculate the 
  #gradients 
  #for our outputs
  with torch.no_grad():
      loader = trainloader if train else testloader 
      for data in loader:
          images, labels = data
          images = images.to(device = device)
          labels = labels.to(device = device)
          # calculate outputs by running images through the network
          outputs = model(images)
          # the class with the highest energy is what we choose as 
          #prediction
          _, predicted = torch.max(outputs.data, 1)
          total += labels.size(0)
          correct += (predicted == labels).sum().item()

   # Print out the information
  print('Accuracy of the network on the 10000 %s images: %d %%' % ('train' if train else 'test', 100 * correct / total))

您的 forward() 方法中缺少 return 语句。

def forward(self,x):
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    return x # <--- THIS