Keypointrcnn (torchvision model) - compute validation loss

Hi,

I am trying to use the torch vision.detection.keypointrcnn model in Pytorch Lightning.
The model returns a dictionary of losses in training mode,
loss_dict = model(images, targets).
I would like the model to return the validation loss in the validation step, but I am getting predictions instead of the losses. Should I implement the loss myself?

Thanks in advance.

yes, you have to or you can check how it’s been done inside vision.detection.keypointrcnn code . I’d suggest calculating loss yourself in training_step too to make it more explainable.

In the training step, the model only returns a dictionary of losses, so I can’t calculate the loss myself.
In the validation step, the model only returns predictions…, there is a way around it but I don’t think this is a good solution. Mask R-CNN: Dictionary of losses for the validation set · Issue #1350 · pytorch/vision · GitHub

the reason is in validation_step the model is set to eval mode and self.training becomes False, so it doesn’t return losses in validation_step. There is no way around for that, you need to change the code. Either update the model as suggested in the link you shared or copy the code to calculate the loss under validation_step to get losses.

1 Like