I am pretty new to pytorch and I am trying to implement semantic segmentation using
fcn_resnet50 as mentioned on https://pytorch.org/docs/stable/torchvision/models.html#semantic-segmentation
While running my code, I receive the following error:
‘collections.OrderedDict’ object has no attribute ‘log_softmax’
This correspond to the line in the code —> 10 loss = criterion(outputs, labels)
I defined my criterion loss function as criterion = nn.CrossEntropyLoss()
Any help is appreciated!!!
Note that (some) torchvision segmentation models will use a
dict as the output.
Could you check that?
I used the model
deeplabv3_resnet101 and met the same error. I checked that the output is indeed a dict object.
Could you tell me how to use the output in the criterion?
Here is my code in which
inputs is a tesor of NxCxHxW and
labels is Nx1xHxW
criterion = nn.CrossEntropyLoss(ignore_index=0)
outputs = model(inputs)
loss = criterion(outputs, labels)
I tried to use
loss = criterion(outputs["out"], labels).
But there is a RuntimeError: invalid argument 3: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4 at …\aten\src\THNN/generic/SpatialClassNLLCriterion.c:59
Another question is that my dataset(images) have 5 classes and some ignoer pixels then should I set the
num_classes to 5 or 6 in the model ?
outputs['out'] should work and the error points to the additional dimension in your
Squeeze dim1 before passing
labels to the loss function and it should work:
labels = labels.squeeze(1)
If you have 5 valid classes and an additional class you want to ignore, you could use 6 classes in total and pass
ignore_index=class_index_to_ignore to your loss function.
@ptrblck It works now. Many thanks!