Loss function for an image

What loss functions/ criterion do i choose when my model output is an image? I would like to have pixelwise L2 loss.

nn.MSELoss should work in this case and accepts 4-dimensional tensors (image tensors) as the model output and target.

1 Like

Another question. My network takes the input image and outputs an image of same size. Then i add my input image to the ouput image and that should be equal to the ground image. → Input + Net(Input) = Ground Truth. Do i need to add the input image already in the Net class or can i add the input image to the output by torch.add(input,output) just before calculating the loss?

You can add it directly to the desired tensor and don’t need to do so in the model’s forward method, as there wouldn’t be a difference.

1 Like