Ejemplo n.º 1
0
    def __init__(self,
                 net=None,
                 version=None,
                 target_size=None,
                 anchor_sizes=[32, 64, 128, 256, 512],
                 anchor_size_ratios=[1, pow(2, 1 / 3),
                                     pow(2, 2 / 3)],
                 anchor_aspect_ratios=[0.5, 1, 2],
                 box_offset=(0.5, 0.5),
                 anchor_box_clip=True,
                 ctx=mx.cpu()):
        super(AnchorNet, self).__init__()

        self._net = net
        if version not in [0, 1, 2, 3, 4, 5, 6]:
            raise ValueError

        feature_sizes = []
        bifpn = get_bifpn(version, ctx=mx.cpu(), dummy=True)(
            mx.nd.random_uniform(low=0,
                                 high=1,
                                 shape=(1, 3, target_size[0], target_size[1]),
                                 ctx=mx.cpu()))
        for bif in bifpn:
            feature_sizes.append(bif.shape[2:])  # h, w

        # get_fpn_resnet 외부에서 보내지 않으면. 무조건 forward 한번 시켜야 하는데,
        # 네트워크를 정확히 정의 해놓지 않아서...(default init) 쓸데 없는 코드를
        # 넣어야 한다.
        with self.name_scope():

            # 아래 두줄은 self.name_scope()안에 있어야 한다. - 새롭게 anchor만드는 네크워크를 생성하는 것이므로.!!!
            # self.name_scope() 밖에 있으면 기존의 self._net 과 이름이 겹친다.

            self._bifpn = get_bifpn(version, ctx=ctx, dummy=True)
            self._bifpn.forward(mx.nd.ones(shape=(1, 3) + target_size,
                                           ctx=ctx))
            self._anchor_generators = HybridSequential()

            for index, feature_size, anchor_size in zip(
                    range(len(feature_sizes)), feature_sizes, anchor_sizes):
                self._anchor_generators.add(
                    EfficientAnchorGenerator(
                        index=index,
                        input_size=target_size,
                        feature_size=feature_size,
                        anchor_size=anchor_size,
                        anchor_size_ratios=anchor_size_ratios,
                        anchor_aspect_ratios=anchor_aspect_ratios,
                        box_offset=box_offset,
                        box_clip=anchor_box_clip))

        self._anchor_generators.initialize(ctx=ctx)
Ejemplo n.º 2
0
    def __init__(self,
                 version=0,
                 input_size=(512, 512),
                 anchor_size_ratios=[1, pow(2, 1 / 3),
                                     pow(2, 2 / 3)],
                 anchor_aspect_ratios=[0.5, 1, 2],
                 num_classes=1,
                 ctx=mx.cpu()):
        super(EfficientNet_Except_Anchor, self).__init__()

        if version not in [0, 1, 2, 3, 4, 5, 6]:
            raise ValueError

        feature_sizes = []
        fpn_output = get_bifpn(version, ctx=mx.cpu(), dummy=True)(
            mx.nd.random_uniform(low=0,
                                 high=1,
                                 shape=(1, 3, input_size[0], input_size[1]),
                                 ctx=mx.cpu()))
        for fpn in fpn_output:
            feature_sizes.append(fpn.shape[2:])  # h, w

        self._bifpn = get_bifpn(version, ctx=ctx)
        self._num_classes = num_classes

        with self.name_scope():

            self._class_subnet = HybridSequential()
            self._box_subnet = HybridSequential()
            for _ in range(3):
                self._class_subnet.add(
                    ConvPredictor(num_channel=256,
                                  kernel=(3, 3),
                                  pad=(1, 1),
                                  stride=(1, 1),
                                  activation='relu',
                                  use_bias=True,
                                  in_channels=0,
                                  weight_initializer=mx.init.Normal(0.01),
                                  bias_initializer='zeros'))
                self._box_subnet.add(
                    ConvPredictor(num_channel=256,
                                  kernel=3,
                                  pad=1,
                                  stride=1,
                                  activation='relu',
                                  use_bias=True,
                                  in_channels=0,
                                  weight_initializer=mx.init.Normal(0.01),
                                  bias_initializer='zeros'))
            '''
            bias_initializer=mx.init.Constant(-np.log((1-0.01)/0.01)?
            논문에서,
             For the final convlayer of the classification subnet, we set the bias initialization to 
             b = − log((1 − π)/π), where π specifies that at that at the start of training every anchor should be 
             labeled as foreground with confidence of ∼π. We use π = .01 in all experiments, 
             although results are robust to the exact value. As explained in section 3.3, 
             this initialization prevents the large number of background anchors from generating a large, 
             destabilizing loss value in the first iteration of training.

             -> 초기에 class subnet 마지막 bias를 b = − log((1 − π)/π)로 설정함으로써, 
             모든  anchor를 0.01 값을 가지는 foreground로 만들어버리는 초기화 방법
             거의 대부분인 background anchor가 첫 번째 학습에서 불안정한 loss 값을 가지는 것을 방지해준다함.
            '''
            prior = 0.01
            self._class_subnet.add(
                ConvPredictor(
                    num_channel=num_classes * len(anchor_size_ratios) *
                    len(anchor_aspect_ratios),
                    kernel=(3, 3),
                    pad=(1, 1),
                    stride=(1, 1),
                    activation=None,
                    use_bias=True,
                    in_channels=0,
                    weight_initializer=mx.init.Normal(0.01),
                    bias_initializer=mx.init.Constant(-np.log((1 - prior) /
                                                              prior))))

            self._box_subnet.add(
                ConvPredictor(num_channel=4 * len(anchor_size_ratios) *
                              len(anchor_aspect_ratios),
                              kernel=3,
                              pad=1,
                              stride=1,
                              activation=None,
                              use_bias=True,
                              in_channels=0,
                              weight_initializer=mx.init.Normal(0.01),
                              bias_initializer='zeros'))

        self._class_subnet.initialize(ctx=ctx)
        self._box_subnet.initialize(ctx=ctx)
        print(f"{self.__class__.__name__} Head weight init 완료")
Ejemplo n.º 3
0
    def __init__(
        self,
        version=0,
        input_size=(512, 512),
        anchor_sizes=[32, 64, 128, 256, 512],
        anchor_size_ratios=[1, pow(2, 1 / 3), pow(2, 2 / 3)],
        anchor_aspect_ratios=[0.5, 1, 2],
        num_classes=1,  # foreground만
        anchor_box_offset=(0.5, 0.5),
        anchor_box_clip=True,
        alloc_size=[256, 256],
        ctx=mx.cpu()):
        super(Efficient, self).__init__()

        if version not in [0, 1, 2, 3, 4, 5, 6]:
            raise ValueError

        # Box / Class Layer - 논문에 나온대로
        repeat = [3, 3, 3, 4, 4, 4, 5, 5]
        channels = [64, 88, 112, 160, 224, 288, 384, 384]

        feature_sizes = []
        bifpn_output = get_bifpn(version, ctx=mx.cpu(), dummy=True)(
            mx.nd.random_uniform(low=0,
                                 high=1,
                                 shape=(1, 3, input_size[0], input_size[1]),
                                 ctx=mx.cpu()))
        for bifpn in bifpn_output:
            feature_sizes.append(bifpn.shape[2:])  # h, w

        self._bifpn = get_bifpn(version, ctx=ctx)
        self._num_classes = num_classes

        with self.name_scope():

            self._class_subnet = HybridSequential()
            self._box_subnet = HybridSequential()
            self._anchor_generators = HybridSequential()
            for _ in range(repeat[version]):
                self._class_subnet.add(
                    ConvPredictor(
                        num_channel=channels[version],
                        kernel=(3, 3),
                        pad=(1, 1),
                        stride=(1, 1),
                        activation='relu',
                        use_bias=True,
                        in_channels=0,
                    ))
                self._box_subnet.add(
                    ConvPredictor(
                        num_channel=channels[version],
                        kernel=(3, 3),
                        pad=(1, 1),
                        stride=(1, 1),
                        activation='relu',
                        use_bias=True,
                        in_channels=0,
                    ))
            '''
            bias_initializer=mx.init.Constant(-np.log((1-0.01)/0.01)?
            논문에서,
             For the final convlayer of the classification subnet, we set the bias initialization to 
             b = − log((1 − π)/π), where π specifies that at that at the start of training every anchor should be 
             labeled as foreground with confidence of ∼π. We use π = .01 in all experiments, 
             although results are robust to the exact value. As explained in section 3.3, 
             this initialization prevents the large number of background anchors from generating a large, 
             destabilizing loss value in the first iteration of training.

             -> 초기에 class subnet 마지막 bias를 b = − log((1 − π)/π)로 설정함으로써, 
             모든 anchor를 0.01 값을 가지는 foreground로 만들어버리는 초기화 방법
             거의 대부분인 background anchor 가 첫 번째 학습에서 불안정한 loss 값을 가지는 것을 방지해준다함.
            '''

            prior = 0.01
            self._class_subnet.add(
                ConvPredictor(
                    num_channel=num_classes * len(anchor_size_ratios) *
                    len(anchor_aspect_ratios),
                    kernel=(3, 3),
                    pad=(1, 1),
                    stride=(1, 1),
                    activation=None,
                    use_bias=True,
                    in_channels=0,
                    bias_initializer=mx.init.Constant(-np.log((1 - prior) /
                                                              prior))))

            self._box_subnet.add(
                ConvPredictor(
                    num_channel=4 * len(anchor_size_ratios) *
                    len(anchor_aspect_ratios),
                    kernel=(3, 3),
                    pad=(1, 1),
                    stride=(1, 1),
                    activation=None,
                    use_bias=True,
                    in_channels=0,
                ))

            for index, feature_size, anchor_size in zip(
                    range(len(feature_sizes)), feature_sizes, anchor_sizes):
                self._anchor_generators.add(
                    EfficientAnchorGenerator(
                        index=index,
                        input_size=input_size,
                        feature_size=feature_size,
                        anchor_size=anchor_size,
                        anchor_size_ratios=anchor_size_ratios,
                        anchor_aspect_ratios=anchor_aspect_ratios,
                        box_offset=anchor_box_offset,
                        box_clip=anchor_box_clip,
                        alloc_size=(alloc_size[0] // (2**index),
                                    alloc_size[1] // (2**index))))

            self._class_subnet.initialize(ctx=ctx)
            self._box_subnet.initialize(ctx=ctx)
            self._anchor_generators.initialize(ctx=ctx)
            logging.info(
                f"{self.__class__.__name__}_{version} Head weight init 완료")