Well, while using Desnsenet169, I am getting proper accuracies, but in the case of ResNet - 101/50, I am not getting any accuracies. It is printing like this -



 Deb_Prakash_Chatterj:

Well, while using Desnsenet169, I am getting proper accuracies, but in the case of ResNet - 101/50, I am not getting any accuracies. It is printing like this -
Accuracy: 0.0000…
Accuracy: 0.0000…
Accuracy: 0.0000…
Accuracy: 0.0000…
Here is a little glimpse of my code -
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

model = models.resnet101(pretrained=True)

for param in model.parameters():
    param.requires_grad = True

model.classifier = nn.Sequential(nn.Linear(2048, 1024),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(1024,4),
nn.LogSoftmax(dim=1))
for epoch in range(epochs):
running_loss = 0
model.train()
for images, labels in dataloader_train:
#steps += 1
images, labels = images.to(device), labels.to(device)

optimizer.zero_grad()

output = model.forward(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()

running_loss += loss.item()

#if steps % print_every == 0:
valid_loss = 0
accuracy = 0
model.eval()
for images, labels in dataloader_test:
optimizer.zero_grad()
with torch.no_grad():
  images, labels = images.to(device), labels.to(device)

  output = model.forward(images)
  loss = criterion(output, labels)
      
  valid_loss += loss.item()
      
  ps = torch.exp(output)
     
  top_p, top_class = ps.topk(1, dim = 1)
  equals = top_class == labels.view(*top_class.shape)
  accuracy += torch.mean(equals.type(torch.FloatTensor))

print("Accuracy: {:.4f}… " .format(accuracy/len(dataloader_test)))
model.train()


Here is a little glimpse of my code -
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

model = models.resnet101(pretrained=True)

for param in model.parameters():
    param.requires_grad = True

model.classifier = nn.Sequential(nn.Linear(2048, 1024),
                             nn.ReLU(),
                             nn.Dropout(0.4),
                             nn.Linear(1024,4),
                             nn.LogSoftmax(dim=1))

for epoch in range(epochs):

  running_loss = 0
  model.train()
  for images, labels in dataloader_train:

    #steps += 1
    images, labels = images.to(device), labels.to(device)

    optimizer.zero_grad()

    output = model.forward(images)
    loss = criterion(output, labels)
    loss.backward()
    optimizer.step()

    running_loss += loss.item()

  #if steps % print_every == 0:
  valid_loss = 0
  accuracy = 0
  model.eval()
  for images, labels in dataloader_test:
    optimizer.zero_grad()
    with torch.no_grad():
   
      images, labels = images.to(device), labels.to(device)

      output = model.forward(images)
      loss = criterion(output, labels)
      
      valid_loss += loss.item()
      
      ps = torch.exp(output)
     
      top_p, top_class = ps.topk(1, dim = 1)
      equals = top_class == labels.view(*top_class.shape)
      accuracy += torch.mean(equals.type(torch.FloatTensor))
   
  print("Accuracy: {:.4f}.. " .format(accuracy/len(dataloader_test)))
  model.train()

Can you guys please help me to do this with getting proper accuracies? Thank you.