如何使用 vgg16 为 ImageNet 准备和拆分数据集 [关闭]

Posted

技术标签:

【中文标题】如何使用 vgg16 为 ImageNet 准备和拆分数据集 [关闭]【英文标题】:How can I prepare and split data set for ImageNet with vgg16 [closed] 【发布时间】:2022-01-21 02:58:42 【问题描述】:
 ''' 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))

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于如何使用 vgg16 为 ImageNet 准备和拆分数据集 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

imagenet-vgg-verydeep-19参数解析

)-在ImageNet上训练VGGNet

在灰度图像网络上训练的 VGG16

Keras如何改变加载模型的可训练层

当用作预训练特征提取器时,VGG16 应该提取多少特征?

深度学习之基于Tensorflow2.0实现VGG16网络