109. Adjusting Data Structure For Loss Function

Depending on the loss function, you might need to change the data structure of the tensor coming out from the model.

In my case, I’m currently doing a binary semantic segmentation using pytorch, and the output of the model is [Batch_size,Num_Classes,Img_Height,Img_Width].

output.shape = torch.size([
                    Batch_size,
                    2(Since it's binary segmentation),
                    Height,
                    Width])

Then you pass that down to the loss function with the correct label data

loss = criterion(output, label)
  1. When using Mean Squared Error as your loss function, it expects both output and label to be the same shape. For example, if you’ve imported the label data as grayscale, the channel would be 1 and the dimensions won’t match because the model’s output channel for a binary segmentation is 2.

  2. When using Cross Entropy Loss as your loss function, it expects the output to be the same dimension the model outputted, but expects the label to be [Batch_size,Height,Width]. It seems that the one-hot-encoding process is being applied inside the api.

Also, if you want to evaluate your model, let’s say, using scikit-learn’s F1score or Jaccard_score like below,,

from sklearn.metrics import f1_score, jaccard_score
f1score = f1_score(label,output)
IOUscore = jaccard_score(label,output)

Both label and output are expected to be a 1D array and hardmaxed so that all the numbers in the array are either 0s or 1s.