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


''' 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

最新更新