Hi Jason!



 Jason_Yuen:

The shape of output, target and predicted are in the following
output torch.Size([1, 22, 256, 256])
#(Batch size, nClasses, H, W)
target torch.Size([1, 1, 256, 256])
#(Batch size, Channels, H, W)
predict torch.Size([1, 256, 256])
#(Batch size H, W)



Yes, that should be okay then.  the “extra” dimension in target is
a singleton (size-1) dimension, so, in some sense, it doesn’t really
count.  (More precisely, when you “select it away” you don’t lose
any information – the resulting tensor with one fewer dimension
still has the same number of elements.)


I am dropping the channel dimension for loss calculation as I think that’s the required input type for CrossEntropyLoss


Yes, this is correct.  You do need to get rid of target's “extra” singleton
dimension to keep CrossEntropyLoss from complaining.


do you know the calculation method for accuracy is correct or not.


It looks correct.  (But don’t trust me – test and double-check it.)  Note
that because predicted and target don’t have the same shape
(target has that extra dimension), broadcasting will occur when you
evaluate predicted.eq (target.data).  But because it’s a singleton
dimension, you’ll get the same result, so nothing will break.


I am working on semantic segmentation problem and my training set is about 2000 images.


I haven’t used U-Net and I don’t really have any intuition about it,
but I imagine that 2000 training images should be enough to train
reasonably well.
I do notice that your accuracy starts off (that is, after just one epoch
of training) at about 50%.  Given that you have 22 classes, this seems
unexpectedly high.  Random guessing with 22 uniformly distributed
classes would give you an accuracy of only about 5%.
Is it possible that one of your classes is a “background” class and
a large fraction of your pixels are background pixels?  For example,
if half of your pixels were background pixels, you would get 50%
accuracy by always predicting background.
If your data set is unbalanced because some classes are rare or
especially common (such as in the background-pixel example),
you might want to try CrossEntropyLoss's class-weight weight
constructor argument to help counteract the class imbalance.
Best.
K. Frank