def __init__(self, width, height, anchors=None, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), iou_thresh=0.5, box_norm=(0.1, 0.1, 0.2, 0.2), bilateral_kernel_size=None, sigma_vals=None, grayscale=False, **kwargs): self._width = width self._height = height self._anchors = anchors self._mean = mean self._std = std self._bilateral_kernel_size = bilateral_kernel_size self._sigma_vals = sigma_vals self._grayscale = grayscale if anchors is None: return # since we do not have predictions yet, so we ignore sampling here from gluoncv.model_zoo.ssd.target import SSDTargetGenerator self._target_generator = SSDTargetGenerator(iou_thresh=iou_thresh, stds=box_norm, negative_mining_ratio=-1, **kwargs)
def __init__(self, anchors, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), **kwargs): self.anchors = anchors iou_thresh = 0.2 stds = (0.1, 0.1, 0.2, 0.2) self.tGen = SSDTargetGenerator(iou_thresh=iou_thresh, stds=stds)
def __init__(self, anchors, iou_thresh=0.5, box_norm=(0.1, 0.1, 0.2, 0.2), **kwargs): self._anchors = anchors self._target_generator = SSDTargetGenerator(iou_thresh=iou_thresh, stds=box_norm, **kwargs)
from gluoncv import model_zoo net = model_zoo.get_model('ssd_300_vgg16_atrous_voc', pretrained_base=False, pretrained=False) ############################################################################ # Some preparation before training from mxnet import gluon net.initialize() conf_loss = gluon.loss.SoftmaxCrossEntropyLoss() loc_loss = gluon.loss.HuberLoss() ############################################################################ # Simulate the training steps by manually compute losses: # You can always use ``gluoncv.loss.SSDMultiBoxLoss`` which fulfills this function. from mxnet import autograd from gluoncv.model_zoo.ssd.target import SSDTargetGenerator target_generator = SSDTargetGenerator() with autograd.record(): # 1. forward pass cls_preds, box_preds, anchors = net(x) # 2. generate training targets cls_targets, box_targets, box_masks = target_generator( anchors, cls_preds, gt_boxes, gt_ids) num_positive = (cls_targets > 0).sum().asscalar() cls_mask = (cls_targets >= 0).expand_dims(axis=-1) # negative targets should be ignored in loss # 3 losses, here we have two options, batch-wise or sample wise norm # 3.1 batch wise normalization: divide loss by the summation of num positive targets in batch batch_conf_loss = conf_loss(cls_preds, cls_targets, cls_mask) / num_positive batch_loc_loss = loc_loss(box_preds, box_targets, box_masks) / num_positive # 3.2 sample wise normalization: divide by num positive targets in this sample(image) sample_num_positive = (cls_targets > 0).sum(axis=0, exclude=True) sample_conf_loss = conf_loss(cls_preds, cls_targets, cls_mask) / sample_num_positive