def __init__(self, stages, channels, anchors, strides, classes, alloc_size=(128, 128), nms_thresh=0.45, nms_topk=400, post_nms=100, pos_iou_thresh=1.0, ignore_iou_thresh=0.7, num_sync_bn_devices=-1, **kwargs): super(YOLOV3, self).__init__(**kwargs) self._classes = classes self.nms_thresh = nms_thresh self.nms_topk = nms_topk self.post_nms = post_nms self._pos_iou_thresh = pos_iou_thresh self._ignore_iou_thresh = ignore_iou_thresh if pos_iou_thresh >= 1: self._target_generator = YOLOV3TargetMerger(len(classes), ignore_iou_thresh) else: raise NotImplementedError( "pos_iou_thresh({}) < 1.0 is not implemented!".format(pos_iou_thresh)) self._loss = YOLOV3Loss() with self.name_scope(): self.stages = nn.HybridSequential() self.transitions = nn.HybridSequential() self.yolo_blocks = nn.HybridSequential() self.yolo_outputs = nn.HybridSequential() # note that anchors and strides should be used in reverse order for i, stage, channel, anchor, stride in zip( range(len(stages)), stages, channels, anchors[::-1], strides[::-1]): self.stages.add(stage) block = YOLODetectionBlockV3(channel, num_sync_bn_devices) self.yolo_blocks.add(block) output = YOLOOutputV3(i, len(classes), anchor, stride, alloc_size=alloc_size) self.yolo_outputs.add(output) if i > 0: self.transitions.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
def reset_class(self, classes): """Reset class categories and class predictors. Parameters ---------- classes : iterable of str The new categories. ['apple', 'orange'] for example. """ self._clear_cached_op() self._classes = classes if self._pos_iou_thresh >= 1: self._target_generator = YOLOV3TargetMerger(len(classes), self._ignore_iou_thresh) for outputs in self.yolo_outputs: outputs.reset_class(classes)
def __init__(self, batch_axis=0, weight=None, box_loss_type='mse', **kwargs): super(YOLOv3Loss, self).__init__(weight, batch_axis, **kwargs) self._sigmoid_ce = mloss.SigmoidBinaryCrossEntropyLoss( from_sigmoid=False) self.target = YOLOV3TargetMerger(20, ignore_iou_thresh=0.5) self._loss_type = box_loss_type if box_loss_type == 'mse': self._l1_loss = mloss.L1Loss() # self._l2_loss = mloss.L2Loss() else: self._iou_loss = IoULoss(x1y1x2y2=True, loss_type=box_loss_type)