| import math |
| from functools import partial |
|
|
| import torch |
| import torch.nn as nn |
|
|
|
|
| class MultiboxLoss(nn.Module): |
| def __init__(self, num_classes, alpha=1.0, neg_pos_ratio=3.0, |
| background_label_id=0, negatives_for_hard=100.0): |
| self.num_classes = num_classes |
| self.alpha = alpha |
| self.neg_pos_ratio = neg_pos_ratio |
| if background_label_id != 0: |
| raise Exception('Only 0 as background label id is supported') |
| self.background_label_id = background_label_id |
| self.negatives_for_hard = torch.FloatTensor([negatives_for_hard])[0] |
|
|
| def _l1_smooth_loss(self, y_true, y_pred): |
| abs_loss = torch.abs(y_true - y_pred) |
| sq_loss = 0.5 * (y_true - y_pred)**2 |
| l1_loss = torch.where(abs_loss < 1.0, sq_loss, abs_loss - 0.5) |
| return torch.sum(l1_loss, -1) |
|
|
| def _softmax_loss(self, y_true, y_pred): |
| y_pred = torch.clamp(y_pred, min = 1e-7) |
| softmax_loss = -torch.sum(y_true * torch.log(y_pred), |
| axis=-1) |
| return softmax_loss |
|
|
| def forward(self, y_true, y_pred): |
| |
| |
| |
| |
| num_boxes = y_true.size()[1] |
| y_pred = torch.cat([y_pred[0], nn.Softmax(-1)(y_pred[1])], dim = -1) |
|
|
| |
| |
| |
| |
| conf_loss = self._softmax_loss(y_true[:, :, 4:-1], y_pred[:, :, 4:]) |
| |
| |
| |
| |
| |
| loc_loss = self._l1_smooth_loss(y_true[:, :, :4], |
| y_pred[:, :, :4]) |
|
|
| |
| |
| |
| pos_loc_loss = torch.sum(loc_loss * y_true[:, :, -1], |
| axis=1) |
| pos_conf_loss = torch.sum(conf_loss * y_true[:, :, -1], |
| axis=1) |
|
|
| |
| |
| |
| |
| num_pos = torch.sum(y_true[:, :, -1], axis=-1) |
|
|
| |
| |
| |
| |
| num_neg = torch.min(self.neg_pos_ratio * num_pos, num_boxes - num_pos) |
| |
| pos_num_neg_mask = num_neg > 0 |
| |
| |
| |
| |
| has_min = torch.sum(pos_num_neg_mask) |
| |
| |
| |
| |
| |
| |
| |
| num_neg_batch = torch.sum(num_neg) if has_min > 0 else self.negatives_for_hard |
|
|
| |
| |
| |
| |
| |
| confs_start = 4 + self.background_label_id + 1 |
| confs_end = confs_start + self.num_classes - 1 |
|
|
| |
| |
| |
| |
| |
| max_confs = torch.sum(y_pred[:, :, confs_start:confs_end], dim=2) |
|
|
| |
| |
| |
| |
| |
| max_confs = (max_confs * (1 - y_true[:, :, -1])).view([-1]) |
|
|
| _, indices = torch.topk(max_confs, k = int(num_neg_batch.cpu().numpy().tolist())) |
|
|
| neg_conf_loss = torch.gather(conf_loss.view([-1]), 0, indices) |
|
|
| |
| num_pos = torch.where(num_pos != 0, num_pos, torch.ones_like(num_pos)) |
| total_loss = torch.sum(pos_conf_loss) + torch.sum(neg_conf_loss) + torch.sum(self.alpha * pos_loc_loss) |
| total_loss = total_loss / torch.sum(num_pos) |
| return total_loss |
|
|
| def weights_init(net, init_type='normal', init_gain=0.02): |
| def init_func(m): |
| classname = m.__class__.__name__ |
| if hasattr(m, 'weight') and classname.find('Conv') != -1: |
| if init_type == 'normal': |
| torch.nn.init.normal_(m.weight.data, 0.0, init_gain) |
| elif init_type == 'xavier': |
| torch.nn.init.xavier_normal_(m.weight.data, gain=init_gain) |
| elif init_type == 'kaiming': |
| torch.nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') |
| elif init_type == 'orthogonal': |
| torch.nn.init.orthogonal_(m.weight.data, gain=init_gain) |
| else: |
| raise NotImplementedError('initialization method [%s] is not implemented' % init_type) |
| elif classname.find('BatchNorm2d') != -1: |
| torch.nn.init.normal_(m.weight.data, 1.0, 0.02) |
| torch.nn.init.constant_(m.bias.data, 0.0) |
| print('initialize network with %s type' % init_type) |
| net.apply(init_func) |
|
|
| def get_lr_scheduler(lr_decay_type, lr, min_lr, total_iters, warmup_iters_ratio = 0.05, warmup_lr_ratio = 0.1, no_aug_iter_ratio = 0.05, step_num = 10): |
| def yolox_warm_cos_lr(lr, min_lr, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter, iters): |
| if iters <= warmup_total_iters: |
| |
| lr = (lr - warmup_lr_start) * pow(iters / float(warmup_total_iters), 2) + warmup_lr_start |
| elif iters >= total_iters - no_aug_iter: |
| lr = min_lr |
| else: |
| lr = min_lr + 0.5 * (lr - min_lr) * ( |
| 1.0 + math.cos(math.pi* (iters - warmup_total_iters) / (total_iters - warmup_total_iters - no_aug_iter)) |
| ) |
| return lr |
|
|
| def step_lr(lr, decay_rate, step_size, iters): |
| if step_size < 1: |
| raise ValueError("step_size must above 1.") |
| n = iters // step_size |
| out_lr = lr * decay_rate ** n |
| return out_lr |
|
|
| if lr_decay_type == "cos": |
| warmup_total_iters = min(max(warmup_iters_ratio * total_iters, 1), 3) |
| warmup_lr_start = max(warmup_lr_ratio * lr, 1e-6) |
| no_aug_iter = min(max(no_aug_iter_ratio * total_iters, 1), 15) |
| func = partial(yolox_warm_cos_lr ,lr, min_lr, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter) |
| else: |
| decay_rate = (min_lr / lr) ** (1 / (step_num - 1)) |
| step_size = total_iters / step_num |
| func = partial(step_lr, lr, decay_rate, step_size) |
|
|
| return func |
|
|
| def set_optimizer_lr(optimizer, lr_scheduler_func, epoch): |
| lr = lr_scheduler_func(epoch) |
| for param_group in optimizer.param_groups: |
| param_group['lr'] = lr |
|
|