Instead of the cifar100. I am using just 4 classes (hair color) of the CelebAHQ dataset. The data is unbalanced and I need to change the loss function by adding weights. I am using an existing framework:
(Source: pytorch-cifar100/train.py at 2149cb57f517c6e5fa7262f958652227225d125b · weiaicunzai/pytorch-cifar100 · GitHub 2)
Replacing in train.py the line:
loss_function = nn.CrossEntropyLoss()
with
w = np.array([1/black/all_samples, 1/blond/all_samples, 1/brown/all_samples, 1/gray/all_samples])
cw = torch.tensor([w[0], w[1], w[2], w[3]], dtype=torch.float32).cuda() # class weights for 0, 1, 3, 4
loss_function = nn.CrossEntropyLoss(weight=cw)

The output is not that good… How can I improve the weights? Should I use the keywords reduce? (        # loss_function = nn.CrossEntropyLoss(ignore_index=255, weight=cw, reduction='none')