L1 loss by default uses reduction=mean and weighs loss on differently sized instances.
But consider the following scenario:
I have a set size for batches and image dimensions, but each image has different masks where only a certain region is of interest.
I think it could be useful to scale each instance’s loss by the area of the region of interest (i.e. number of ones in the mask).
I don’t believe elements which are exactly zero are ignored in L1Loss, are they?
Could something like this work?
# Assume 'input', 'output', 'mask' all are NCHW

output = model(input)
diff = input*mask - output*mask
weighted_diff = mask.sum([1,2,3]) * diff
loss = L1_loss(weighted_diff, torch.zeros_like(weighted_diff))