Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion class_balanced_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ def CB_loss(labels, logits, samples_per_cls, no_of_classes, loss_type, beta, gam
Returns:
cb_loss: A float tensor representing class balanced loss
"""
assert labels.get_device() == logits.get_device()
device = labels.get_device()
device = torch.device("cuda:" + str(device)) if device >= 0 else torch.device("cpu")

effective_num = 1.0 - np.power(beta, samples_per_cls)
weights = (1.0 - beta) / np.array(effective_num)
weights = weights / np.sum(weights) * no_of_classes

labels_one_hot = F.one_hot(labels, no_of_classes).float()

weights = torch.tensor(weights).float()
weights = torch.tensor(weights).float().to(device)
weights = weights.unsqueeze(0)
weights = weights.repeat(labels_one_hot.shape[0],1) * labels_one_hot
weights = weights.sum(1)
Expand Down