Issue
''' 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
Load the dataset you were given. Images should be stored in an X variable and your labels in a Y variable. Split your dataset into train, validation and test set and pre-process your data for training.
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))
Solution
You are missing a return
statement in your forward()
method.
def forward(self,x):
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x # <--- THIS
Answered By - ayandas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.