예제 #1
0
    def __init__(self, spec, num_sync_bn_devices, **kwargs):
        super(BasicYOLONet, self).__init__(**kwargs)

        layers = spec['layers']
        channels = spec['channels']

        assert len(layers) == len(channels) - 1, (
            "len(channels) should equal to len(layers) + 1, given {} vs {}".
            format(len(channels), len(layers)))

        with self.name_scope():
            self.stages = gluon.nn.HybridSequential()
            self.stages.add(_conv2d(channels[0], 3, 1, 1))

            for nlayer, channel in zip(layers[0:], channels[1:]):
                stage = gluon.nn.HybridSequential()
                stage.add(_conv2d(channel, 3, 1, 2))
                for _ in range(nlayer):
                    stage.add(
                        DarknetBasicBlockV3(channel // 2, num_sync_bn_devices))
                self.stages.add(stage)

        anchors = spec['all_anchors']
        self.slice_point = spec['slice_point']

        self.transitions, self.yolo_blocks, self.yolo_outputs = YOLOPyrmaid(
            channels[-len(anchors):], anchors, self.slice_point[-1],
            num_sync_bn_devices)

        self.num_pyrmaid_layers = len(anchors)
예제 #2
0
 def __init__(self, channel, num_sync_bn_devices=-1, **kwargs):
     super(FYOLODetectionBlock, self).__init__(**kwargs)
     assert channel % 2 == 0, "channel {} cannot be divided by 2".format(channel)
     with self.name_scope():
         self.body = nn.HybridSequential(prefix='')
         for _ in range(3):
             # 1x1 reduce
             self.body.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
             # 3x3 expand
             self.body.add(_conv2d(channel * 2, 3, 1, 1, num_sync_bn_devices))
예제 #3
0
 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))
예제 #4
0
 def __init__(self, channel, dilation, num_sync_bn_devices=-1, **kwargs):
     super(DarknetDLBlock, self).__init__(**kwargs)
     self.body = nn.HybridSequential(prefix='')
     # 1x1 reduce
     self.body.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
     # 3x3 conv expand
     self.body.add(_conv2d_dl(channel * 2, 3, dilation, dilation, 1, num_sync_bn_devices))
예제 #5
0
 def __init__(self,
              layers,
              channels,
              dilations,
              num_sync_bn_devices=-1,
              **kwargs):
     super(DarknetLSFSimple, self).__init__(**kwargs)
     assert len(layers) == len(channels) - 1, (
         "len(channels) should equal to len(layers) + 1, given {} vs {}".
         format(len(channels), len(layers)))
     assert len(layers) == len(dilations) - 1, (
         "len(dilations) should equal to len(layers) + 1, given {} vs {}".
         format(len(dilations), len(layers)))
     self._stride = dilations[0] * pow(2, len(layers))
     with self.name_scope():
         self.features = nn.HybridSequential()
         # first 3x3 conv with channels[0], dilation=padding=stride=dilations[0]
         d = dilations[0]
         self.features.add(
             _conv2d_dl(channels[0], 3, d, d, d, num_sync_bn_devices))
         for nlayer, channel, dilation in zip(layers, channels[1:],
                                              dilations[1:]):
             assert channel % 2 == 0, "channel {} cannot be divided by 2".format(
                 channel)
             # add downsample conv with stride=2
             self.features.add(
                 _conv2d(channel, 3, 1, 2, num_sync_bn_devices))
             # add nlayer basic blocks
             for _ in range(nlayer):
                 self.features.add(
                     DarknetDLBlock(channel // 2, dilation,
                                    num_sync_bn_devices))
예제 #6
0
 def __init__(self, channel, dilations, num_sync_bn_devices=-1, **kwargs):
     super(YOLODetectionBlockV4, self).__init__(**kwargs)
     assert channel % 2 == 0, "channel {} cannot be divided by 2".format(
         channel)
     with self.name_scope():
         d = dilations
         self.body = nn.HybridSequential(prefix='')
         for i in range(2):
             # 1x1 reduce
             self.body.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
             # 3x3 expand
             self.body.add(
                 _conv2d_dl(channel * 2, 3, d[i], d[i], 1,
                            num_sync_bn_devices))
         self.body.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
         self.tip = _conv2d_dl(channel * 2, 3, d[-1], d[-1], 1,
                               num_sync_bn_devices)
예제 #7
0
 def __init__(self, channel, dilation, num_sync_bn_devices=-1, **kwargs):
     super(DarknetLSFBlock, self).__init__(**kwargs)
     self.body = nn.HybridSequential(prefix='')
     # 1x1 reduce
     self.body.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))
     # 3x3 conv expand
     self.body.add(_conv2d_dl(channel * 2, 3, dilation, dilation//2, dilation, num_sync_bn_devices))
     # downsample path. compute pool_size
     pool_size = 3 + 2 * (dilation - 1)
     self.downsample = nn.HybridSequential(prefix='')
     self.downsample.add(nn.MaxPool2D(pool_size=pool_size, strides=dilation, padding=dilation))
예제 #8
0
def YOLOPyrmaid(pyrmaid_channels, anchors, total_channels_per_anchor,
                num_sync_bn_devices):
    transitions = gluon.nn.HybridSequential()
    yolo_blocks = gluon.nn.HybridSequential()
    yolo_outputs = gluon.nn.HybridSequential()

    # note that anchors and strides should be used in reverse order
    for i, channel, anchor in zip(range(len(anchors)), pyrmaid_channels[::-1],
                                  anchors[::-1]):

        output = YOLOOutput(total_channels_per_anchor, len(anchor))
        yolo_outputs.add(output)
        block = YOLODetectionBlockV3(channel, num_sync_bn_devices)
        yolo_blocks.add(block)
        if i > 0:
            transitions.add(_conv2d(channel, 1, 0, 1, num_sync_bn_devices))

    return transitions, yolo_blocks, yolo_outputs