I think the default torch.nn.CrossEntropyLoss(size_average=False) loss used between predicted and true classes is not the correct choice, and here's why:
Class predictions are passed through a sigmoid: pred_cls = torch.sigmoid(prediction[:, :, 5:]), therefore, the values are between [0, 1].
In the perfect case (where truth class = 1 and other classes = 0), the difference (in magnitude) is (and always will be at most) 1. Taking the log of the softmax (what CrossEntropyLoss does) of a vector with values between [0, 1] isn't _helping_ the loss function to clearly _detect_ the correct class, again, because the relative difference is small.
In my understanding, CrossEntropyLoss measures the relative difference between the truth class and other classes (i.e.: a good classification = large value for truth class and small values for other classes). For example: [0.1, 2.1, 23.1, 0.7] is a good prediction for class [3], the relative difference between the 3rd element and the rest is big. Please correct me if I am wrong.
Maybe we can just use MSELoss for classes, too?
Right now, I just came across the commit where you have changed the CrossEntropyLoss, sorry for not looking before opening the issue.
Nevertheless, was this the reason for the change?
Optimizing the network to minimize the crossentropy loss means minimizing the distance between ground truth labels (1 for index of class and 0 for the others) and the prediction of the network. In the case where a sigmoid activation function is applied the output will be between 0 and 1, but the network is not constrained so that the outputs sum to one, as is the case when a softmax activation function is used. The softmax activation function is normally applied together with the crossentropy loss as this essentially means that the network will be trained to maximize the output where the ground truth label is 1 and minimize all others. The fact that the difference is only one in this case is not really an issue. However, in this case the authors argued that it makes more sense to use a sigmoid activation function (instead of softmax) since the class labels for an object does not necessarily need to be mutually exclusive, e.g. a labrador should be able to be labeled as both a dog and a labrador. In this case it makes more sense to use a binary crossentropy loss, since this loss does not assume that the outputs should sum to one.
Thanks for the awesome repo. For my dataset all the objects are exclusive so I think using softmax will be more efficient. Can you suggest where should i make changes to use softmax classifier instead of sigmoid for multi-class classifier.
Most helpful comment
Optimizing the network to minimize the crossentropy loss means minimizing the distance between ground truth labels (1 for index of class and 0 for the others) and the prediction of the network. In the case where a sigmoid activation function is applied the output will be between 0 and 1, but the network is not constrained so that the outputs sum to one, as is the case when a softmax activation function is used. The softmax activation function is normally applied together with the crossentropy loss as this essentially means that the network will be trained to maximize the output where the ground truth label is 1 and minimize all others. The fact that the difference is only one in this case is not really an issue. However, in this case the authors argued that it makes more sense to use a sigmoid activation function (instead of softmax) since the class labels for an object does not necessarily need to be mutually exclusive, e.g. a labrador should be able to be labeled as both a dog and a labrador. In this case it makes more sense to use a binary crossentropy loss, since this loss does not assume that the outputs should sum to one.