-2

I have used FCN ResNet50 model for semantic segmentation of document images. I've been trying to resolve this issue but so far have not been able to find success. This is the link for the model on google colab: https://colab.research.google.com/drive/1slJilG1ZBOsk6AqM6AOUaaCxHFSXVMCM?usp=sharing

This is the error:

TypeError: Traceback (most recent call last)
<ipython-input-12-f6d0f244c03c> in <module>()
     13 
     14 model_ft = train_model(final_model, train_dl, criterion, optimizer_ft, exp_lr_scheduler,
---> 15                        num_epochs=25)

<ipython-input-11-683ce68860de> in train_model(model, dataloaders, criterion, optimizer, scheduler, num_epochs)
     31                 with torch.set_grad_enabled(phase == 'train'):
     32                     outputs, aux = model(inputs.float())
---> 33                     _, preds = torch.max(outputs, 1)
     34                     loss = criterion(outputs, labels)
     35 

TypeError: max() received an invalid combination of arguments - got (str, int), but expected one of:
 * (Tensor input)
 * (Tensor input, Tensor other, *, Tensor out)
 * (Tensor input, int dim, bool keepdim, *, tuple of Tensors out)
 * (Tensor input, name dim, bool keepdim, *, tuple of Tensors out)
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

attaching pretrained segmentation models in torchvision

model(inputs.float())

returns a dictionary, not an array. Consequently what you are passing to max is set of keys from this dictionary (since dictionary, when iterated over, is treated as a collection of keys, which happen to be strings in this case). Just read correct key from your model. The exact key depends on which model you loaded.

outputs_dict = model(inputs.float())
print(outputs_dict.keys())

Looking at https://github.com/pytorch/vision/blob/master/torchvision/models/segmentation/_utils.py

I would expect the key to be called "out", and auxiliary output to be placed in "aux".

lejlot
  • 64,777
  • 8
  • 131
  • 164
  • I have made the following changes to the code : Line 1-> outputs = outputs_dict["out"] Line 2-> _, preds = torch.argmax(outputs, 1) But now the following error occurs: too many values to unpack (expected 2) . Please help – Suniket Das Jul 30 '21 at 19:28
  • a question on SO is not a debugging thread. A question is supposed to ask one, specific problem, and the answer is to address it. If you have new issue you should ask a new question thus it can be tracked and seen by others that encounter it. – lejlot Jul 31 '21 at 18:12
  • The unpacking issue is because argmax should output one value, why do you do _, preds = torch.argmax(outputs, 1)? it should just be preds = torch.argmax(outputs, 1) – lejlot Jul 31 '21 at 18:18