YyzHarry / imbalanced-semi-self

[NeurIPS 2020] Semi-Supervision (Unlabeled Data) & Self-Supervision Improve Class-Imbalanced / Long-Tailed Learning
https://arxiv.org/abs/2006.07529
MIT License
735 stars 115 forks source link

Where can I setting the CE(Uniform) and CE(Balanced) ? #10

Closed hooseok closed 3 years ago

hooseok commented 3 years ago

I see the Self-supervised pretrained learning (SSP). There are many models in SSP.

  1. CE(Uniform) + SSP
  2. CE(Balanced) + SSP

Where can I setting the CB in train.py code? In my opinion, per_cls_weights seems to set a uniform or balance. Does the CB setting mean 'Reweight' in args.train_rule?

    if args.train_rule == 'Reweight':
        beta = 0.9999
        effective_num = 1.0 - np.power(beta, cls_num_list)
        per_cls_weights = (1.0 - beta) / np.array(effective_num)
        per_cls_weights = per_cls_weights / np.sum(per_cls_weights) * len(cls_num_list)
        per_cls_weights = torch.FloatTensor(per_cls_weights).cuda(args.gpu)
    elif args.train_rule == 'DRW':
        idx = epoch // 160
        betas = [0, 0.9999]
        effective_num = 1.0 - np.power(betas[idx], cls_num_list)
        per_cls_weights = (1.0 - betas[idx]) / np.array(effective_num)
        per_cls_weights = per_cls_weights / np.sum(per_cls_weights) * len(cls_num_list)
        per_cls_weights = torch.FloatTensor(per_cls_weights).cuda(args.gpu)
    else:
        per_cls_weights = None

    if args.loss_type == 'CE':
        criterion = nn.CrossEntropyLoss(weight=per_cls_weights).cuda(args.gpu)
    elif args.loss_type == 'LDAM':
        criterion = LDAMLoss(cls_num_list=cls_num_list, max_m=0.5, s=30, weight=per_cls_weights).cuda(args.gpu)
    elif args.loss_type == 'Focal':
        criterion = FocalLoss(weight=per_cls_weights, gamma=1).cuda(args.gpu)
    else:
        warnings.warn('Loss type is not listed')
        return
YyzHarry commented 3 years ago

Hi, thanks for your interest. "CE(Balanced)" means CE with class-balanced sampling, which corresponds to "Resample" as for args.train_rule. You can also choose "Reweight", which means re-weighting the loss for each class according to # of samples, by changing args.train_rule. https://github.com/YyzHarry/imbalanced-semi-self/blob/16d8f02264d9e16602d1a47acc43053b6bb007c4/train.py#L31-L32