I'm currently trying to do semantic segmentation with a deep learning model on images. The dataset is highly imbalanced and i would like to try weighted sampling. I'm using pytorch and a dataloader with WeightedRandomSampler. If I understand the WeightedRandomSampler correctly, it wants a weight for every image in my training set.
My question is: how do I set the weights when every image has more than one class? I'm thinking of setting the weight for each image $i$ to:
$$ w_i = \frac{1}{m_i}\sum^C_{c=1}n_{i,c}\frac{1}{f_c} $$
where $m_i$ is the total number of pixels in image $i$, $C$ is the number of classes, $n_{i,c}$ is the number of pixels with class $c$ in image $i$, and $f_c$ is the frequency of class $c$ in the entire training set. Is this a valid way to set the weights? Is there some standard way to do this (that i just missed)?