コード例 #1
0
    def __init__(self,
                 n_fg_class=None,
                 pretrained_model='imagenet',
                 detection=True,
                 segmentation=False,
                 attention=False):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        super(Multi_task_300, self).__init__(
            extractor=Multi_task_VGG16Extractor300(detection=detection,
                                                   segmentation=segmentation,
                                                   attention=attention),
            multibox=Multibox(n_class=param['n_fg_class'] + 1,
                              aspect_ratios=((2, ), (2, 3), (2, 3), (2, 3),
                                             (2, ), (2, ))),
            steps=(8, 16, 32, 64, 100, 300),
            sizes=(30, 60, 111, 162, 213, 264, 315),
            mean=_imagenet_mean)

        with self.init_scope():
            self.multimask = Multimask(n_class=param['n_fg_class'] + 1,
                                       scale_num=6)
        self.detection = detection
        self.segmentation = segmentation

        self._prediction_detection = False

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #2
0
    def __init__(self, n_fg_class=None, pretrained_model=None,
                 min_size=800, max_size=1333):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        base = self._base(n_class=1, arch='he')
        base.pick = ('res2', 'res3', 'res4', 'res5')
        base.pool1 = lambda x: F.max_pooling_2d(
            x, 3, stride=2, pad=1, cover_all=False)
        base.remove_unused()
        extractor = FPN(
            base, len(base.pick), (1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64))

        super(FasterRCNNFPNResNet, self).__init__(
            extractor=extractor,
            rpn=RPN(extractor.scales),
            head=Head(param['n_fg_class'] + 1, extractor.scales),
            min_size=min_size, max_size=max_size
        )

        if path == 'imagenet':
            _copyparams(
                self.extractor.base,
                self._base(pretrained_model='imagenet', arch='he'))
        elif path:
            chainer.serializers.load_npz(path, self)
コード例 #3
0
    def __init__(self,
                 n_fg_class=None,
                 pretrained_model='imagenet',
                 detection=True,
                 segmentation=False,
                 attention=False):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        super(Multi_task_512, self).__init__(
            extractor=Multi_task_VGG16Extractor512(detection=detection,
                                                   segmentation=segmentation,
                                                   attention=attention),
            multibox=Multibox(n_class=param['n_fg_class'] + 1,
                              aspect_ratios=((2, ), (2, 3), (2, 3), (2, 3),
                                             (2, 3), (2, ), (2, ))),
            steps=(8, 16, 32, 64, 128, 256, 512),
            sizes=(35.84, 76.8, 153.6, 230.4, 307.2, 384.0, 460.8, 537.6),
            mean=_imagenet_mean)

        with self.init_scope():
            self.multimask = Multimask(n_class=param['n_fg_class'] + 1,
                                       scale_num=7)
        self.detection = detection
        self.segmentation = segmentation

        self._prediction_detection = False

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #4
0
    def __init__(self, n_layer, pretrained_model=None, initialW=None):
        n_block = self._blocks[n_layer]

        _, path = utils.prepare_pretrained_model({}, pretrained_model,
                                                 self._models[n_layer])

        super(DilatedResNet, self).__init__()
        with self.init_scope():
            self.conv1_1 = Conv2DBNActiv(None,
                                         64,
                                         3,
                                         2,
                                         1,
                                         1,
                                         initialW=initialW)
            self.conv1_2 = Conv2DBNActiv(64, 64, 3, 1, 1, 1, initialW=initialW)
            self.conv1_3 = Conv2DBNActiv(64,
                                         128,
                                         3,
                                         1,
                                         1,
                                         1,
                                         initialW=initialW)
            self.pool1 = lambda x: F.max_pooling_2d(
                x, ksize=3, stride=2, pad=1)
            self.res2 = ResBlock(n_block[0],
                                 128,
                                 64,
                                 256,
                                 1,
                                 1,
                                 initialW=initialW,
                                 stride_first=False)
            self.res3 = ResBlock(n_block[1],
                                 256,
                                 128,
                                 512,
                                 2,
                                 1,
                                 initialW=initialW,
                                 stride_first=False)
            self.res4 = ResBlock(n_block[2],
                                 512,
                                 256,
                                 1024,
                                 1,
                                 2,
                                 initialW=initialW,
                                 stride_first=False)
            self.res5 = ResBlock(n_block[3],
                                 1024,
                                 512,
                                 2048,
                                 1,
                                 4,
                                 initialW=initialW,
                                 stride_first=False)

        if path:
            chainer.serializers.load_npz(path, self, ignore_names=None)
コード例 #5
0
ファイル: pspnet.py プロジェクト: zhuyiche/chainercv
    def __init__(self,
                 n_class=None,
                 pretrained_model=None,
                 input_size=None,
                 initialW=None,
                 comm=None):
        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'input_size': input_size
            }, pretrained_model, self._models, {'input_size': (713, 713)})

        if comm is not None:
            bn_kwargs = {'comm': comm}
        else:
            bn_kwargs = {}
        if initialW is None:
            initialW = chainer.initializers.HeNormal()
        extractor = DilatedResNet(101, initialW, bn_kwargs)
        extractor.pick = ('res4', 'res5')
        super(PSPNetResNet101,
              self).__init__(extractor, param['n_class'], param['input_size'],
                             initialW, bn_kwargs)

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #6
0
    def __init__(self,
                 n_class=None,
                 pretrained_model=None,
                 min_input_size=None,
                 scales=None,
                 flip=None,
                 extractor_kwargs=None,
                 aspp_kwargs=None,
                 decoder_kwargs=None):
        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'min_input_size': min_input_size,
                'scales': scales,
                'flip': flip,
                'extractor_kwargs': extractor_kwargs,
                'aspp_kwargs': aspp_kwargs,
                'decoder_kwargs': decoder_kwargs
            },
            pretrained_model,
            self._models,
            default={'min_input_size': (513, 513)})

        super(DeepLabV3plusXception65,
              self).__init__(Xception65(**param['extractor_kwargs']),
                             SeparableASPP(2048, 256, **param['aspp_kwargs']),
                             Decoder(256, param['n_class'], 48, 256,
                                     **param['decoder_kwargs']),
                             min_input_size=param['min_input_size'],
                             scales=param['scales'],
                             flip=param['flip'])

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #7
0
    def __init__(self,
                 n_layer,
                 n_class=None,
                 pretrained_model=None,
                 mean=None,
                 initialW=None,
                 fc_kwargs={},
                 arch='fb'):
        if arch == 'fb':
            stride_first = False
            conv1_no_bias = True
        elif arch == 'he':
            stride_first = True
            # Kaiming He uses bias only for ResNet50
            conv1_no_bias = n_layer != 50
        else:
            raise ValueError('arch is expected to be one of [\'he\', \'fb\']')
        blocks = self._blocks[n_layer]

        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'mean': mean
            }, pretrained_model, self._models[arch][n_layer], {
                'n_class': 1000,
                'mean': _imagenet_mean
            })
        self.mean = param['mean']

        if initialW is None:
            initialW = initializers.HeNormal(scale=1., fan_option='fan_out')
        if 'initialW' not in fc_kwargs:
            fc_kwargs['initialW'] = initializers.Normal(scale=0.01)
        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            initialW = initializers.constant.Zero()
            fc_kwargs['initialW'] = initializers.constant.Zero()
        kwargs = {'initialW': initialW, 'stride_first': stride_first}

        super(ResNet, self).__init__()
        with self.init_scope():
            self.conv1 = Conv2DBNActiv(None,
                                       64,
                                       7,
                                       2,
                                       3,
                                       nobias=conv1_no_bias,
                                       initialW=initialW)
            self.pool1 = lambda x: F.max_pooling_2d(x, ksize=3, stride=2)
            self.res2 = ResBlock(blocks[0], None, 64, 256, 1, **kwargs)
            self.res3 = ResBlock(blocks[1], None, 128, 512, 2, **kwargs)
            self.res4 = ResBlock(blocks[2], None, 256, 1024, 2, **kwargs)
            self.res5 = ResBlock(blocks[3], None, 512, 2048, 2, **kwargs)
            self.pool5 = lambda x: F.average(x, axis=(2, 3))
            self.fc6 = L.Linear(None, param['n_class'], **fc_kwargs)
            self.prob = F.softmax

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #8
0
    def __init__(self, n_fg_class=None, pretrained_model=None):
        super(YOLOv3, self).__init__()

        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        self.n_fg_class = param['n_fg_class']
        self.use_preset('visualize')

        with self.init_scope():
            self.extractor = Darknet53Extractor()
            self.subnet = chainer.ChainList()

        for i, n in enumerate((512, 256, 128)):
            self.subnet.append(chainer.Sequential(
                Conv2DBNActiv(n * 2, 3, pad=1, activ=_leaky_relu),
                Convolution2D(
                    len(self._anchors[i]) * (4 + 1 + self.n_fg_class), 1)))

        default_bbox = []
        step = []
        for k, grid in enumerate(self.extractor.grids):
            for v, u in itertools.product(range(grid), repeat=2):
                for h, w in self._anchors[k]:
                    default_bbox.append((v, u, h, w))
                    step.append(self.insize / grid)
        self._default_bbox = np.array(default_bbox, dtype=np.float32)
        self._step = np.array(step, dtype=np.float32)

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #9
0
    def __init__(
        self,
        n_fg_class=None,
        pretrained_model=None,
        min_size=600,
        max_size=1000,
        ratios=[0.5, 1, 2],
        anchor_scales=[8, 16, 32],
        loc_normalize_mean=(0.0, 0.0, 0.0, 0.0),
        loc_normalize_std=(0.2, 0.2, 0.5, 0.5),
        iter2=True,
        resnet_initialW=None,
        rpn_initialW=None,
        head_initialW=None,
        proposal_creator_params={
            'nms_thresh': 0.7,
            'n_train_pre_nms': 6000,
            'n_train_post_nms': 300,
            'n_test_pre_nms': 6000,
            'n_test_post_nms': 300,
            'force_cpu_nms': False,
            'min_size': 16
        }):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        if rpn_initialW is None:
            rpn_initialW = chainer.initializers.Normal(0.01)
        if resnet_initialW is None and pretrained_model:
            resnet_initialW = chainer.initializers.constant.Zero()

        extractor = ResNet101Extractor(initialW=resnet_initialW)
        rpn = RegionProposalNetwork(
            1024,
            512,
            ratios=ratios,
            anchor_scales=anchor_scales,
            feat_stride=self.feat_stride,
            initialW=rpn_initialW,
            proposal_creator_params=proposal_creator_params)
        head = FCISResNet101Head(param['n_fg_class'] + 1,
                                 roi_size=21,
                                 group_size=7,
                                 spatial_scale=1. / self.feat_stride,
                                 loc_normalize_mean=loc_normalize_mean,
                                 loc_normalize_std=loc_normalize_std,
                                 iter2=iter2,
                                 initialW=head_initialW)

        mean = np.array([123.15, 115.90, 103.06], dtype=np.float32)[:, None,
                                                                    None]

        super(FCISResNet101,
              self).__init__(extractor, rpn, head, mean, min_size, max_size,
                             loc_normalize_mean, loc_normalize_std)

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #10
0
    def __init__(self,
                 n_fg_class=None,
                 pretrained_model=None,
                 min_size=600,
                 max_size=1000,
                 ratios=[0.5, 1, 2],
                 anchor_scales=[8, 16, 32],
                 vgg_initialW=None,
                 rpn_initialW=None,
                 loc_initialW=None,
                 score_initialW=None,
                 proposal_creator_params={}):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        if loc_initialW is None:
            loc_initialW = chainer.initializers.Normal(0.001)
        if score_initialW is None:
            score_initialW = chainer.initializers.Normal(0.01)
        if rpn_initialW is None:
            rpn_initialW = chainer.initializers.Normal(0.01)
        if vgg_initialW is None and pretrained_model:
            vgg_initialW = chainer.initializers.constant.Zero()

        extractor = VGG16(initialW=vgg_initialW)
        extractor.pick = 'conv5_3'
        # Delete all layers after conv5_3.
        extractor.remove_unused()
        rpn = RegionProposalNetwork(
            512,
            512,
            ratios=ratios,
            anchor_scales=anchor_scales,
            feat_stride=self.feat_stride,
            initialW=rpn_initialW,
            proposal_creator_params=proposal_creator_params,
        )
        head = VGG16RoIHead(param['n_fg_class'] + 1,
                            roi_size=7,
                            spatial_scale=1. / self.feat_stride,
                            vgg_initialW=vgg_initialW,
                            loc_initialW=loc_initialW,
                            score_initialW=score_initialW)

        super(FasterRCNNVGG16,
              self).__init__(extractor,
                             rpn,
                             head,
                             mean=np.array([122.7717, 115.9465, 102.9801],
                                           dtype=np.float32)[:, None, None],
                             min_size=min_size,
                             max_size=max_size)

        if path == 'imagenet':
            self._copy_imagenet_pretrained_vgg16()
        elif path:
            chainer.serializers.load_npz(path, self)
コード例 #11
0
ファイル: vgg16.py プロジェクト: zhuyiche/chainercv
    def __init__(self,
                 n_class=None,
                 pretrained_model=None,
                 mean=None,
                 initialW=None,
                 initial_bias=None):
        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'mean': mean
            }, pretrained_model, self._models, {
                'n_class': 1000,
                'mean': _imagenet_mean
            })
        self.mean = param['mean']

        if initialW is None:
            # Employ default initializers used in the original paper.
            initialW = normal.Normal(0.01)
        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            initialW = constant.Zero()
        kwargs = {'initialW': initialW, 'initial_bias': initial_bias}

        super(VGG16, self).__init__()
        with self.init_scope():
            self.conv1_1 = Conv2DActiv(None, 64, 3, 1, 1, **kwargs)
            self.conv1_2 = Conv2DActiv(None, 64, 3, 1, 1, **kwargs)
            self.pool1 = _max_pooling_2d
            self.conv2_1 = Conv2DActiv(None, 128, 3, 1, 1, **kwargs)
            self.conv2_2 = Conv2DActiv(None, 128, 3, 1, 1, **kwargs)
            self.pool2 = _max_pooling_2d
            self.conv3_1 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs)
            self.conv3_2 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs)
            self.conv3_3 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs)
            self.pool3 = _max_pooling_2d
            self.conv4_1 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs)
            self.conv4_2 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs)
            self.conv4_3 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs)
            self.pool4 = _max_pooling_2d
            self.conv5_1 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs)
            self.conv5_2 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs)
            self.conv5_3 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs)
            self.pool5 = _max_pooling_2d
            self.fc6 = Linear(None, 4096, **kwargs)
            self.fc6_relu = relu
            self.fc6_dropout = dropout
            self.fc7 = Linear(None, 4096, **kwargs)
            self.fc7_relu = relu
            self.fc7_dropout = dropout
            self.fc8 = Linear(None, param['n_class'], **kwargs)
            self.prob = softmax

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #12
0
ファイル: se_resnext.py プロジェクト: zutshianand/chainercv
    def __init__(self,
                 n_layer,
                 n_class=None,
                 pretrained_model=None,
                 mean=None,
                 initialW=None,
                 fc_kwargs={}):
        blocks = self._blocks[n_layer]

        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'mean': mean
            }, pretrained_model, self._models[n_layer], {
                'n_class': 1000,
                'mean': _imagenet_mean
            })
        self.mean = param['mean']

        if initialW is None:
            initialW = initializers.HeNormal(scale=1., fan_option='fan_out')
        if 'initialW' not in fc_kwargs:
            fc_kwargs['initialW'] = initializers.Normal(scale=0.01)
        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            initialW = initializers.constant.Zero()
            fc_kwargs['initialW'] = initializers.constant.Zero()
        kwargs = {
            'groups': 32,
            'initialW': initialW,
            'stride_first': False,
            'add_seblock': True
        }

        super(SEResNeXt, self).__init__()
        with self.init_scope():
            self.conv1 = Conv2DBNActiv(None,
                                       64,
                                       7,
                                       2,
                                       3,
                                       nobias=True,
                                       initialW=initialW)
            self.pool1 = lambda x: F.max_pooling_2d(x, ksize=3, stride=2)
            self.res2 = ResBlock(blocks[0], None, 128, 256, 1, **kwargs)
            self.res3 = ResBlock(blocks[1], None, 256, 512, 2, **kwargs)
            self.res4 = ResBlock(blocks[2], None, 512, 1024, 2, **kwargs)
            self.res5 = ResBlock(blocks[3], None, 1024, 2048, 2, **kwargs)
            self.pool5 = lambda x: F.average(x, axis=(2, 3))
            self.fc6 = L.Linear(None, param['n_class'], **fc_kwargs)
            self.prob = F.softmax

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #13
0
    def __init__(self, n_fg_class=None, pretrained_model=None):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        super(M2Det300, self).__init__(
            extractor=VGG16MLFPN300(),
            multibox=Multibox(
                n_class=param['n_fg_class'] + 1,
                aspect_ratios=((2,), (2, 3), (2, 3), (2, 3), (2,), (2,))),
            steps=(8, 16, 32, 64, 100, 300),
            sizes=(30, 60, 111, 162, 213, 264, 315),
            mean=_imagenet_mean)

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #14
0
    def __init__(self, n_fg_class=None, pretrained_model=None):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        super(M2Det512, self).__init__(
            extractor=VGG16MLFPN512(),
            multibox=Multibox(
                n_class=param['n_fg_class'] + 1,
                aspect_ratios=((2,), (2, 3), (2, 3), (2, 3), (2,), (2,))),
            steps=(8, 16, 32, 64, 128, 256),
            sizes=(30.72, 76.8, 168.96, 261.12, 353.28, 445.44, 537.6),
            mean=_imagenet_mean)

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #15
0
    def __init__(self, n_fg_class=None, pretrained_model=None):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        super(M2Det320, self).__init__(
            extractor=VGG16MLFPN320(),
            multibox=Multibox(
                n_class=param['n_fg_class'] + 1,
                aspect_ratios=((2,), (2, 3), (2, 3), (2, 3), (2,), (2,))),
            steps=(8, 16, 32, 64, 107, 320),
            sizes=(25.6, 48, 105.6, 163.2, 220.8, 278.4, 336),
            mean=_imagenet_mean)

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #16
0
    def __init__(self, n_fg_class=None, pretrained_model=None):
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        super(SSD512, self).__init__(
            extractor=VGG16Extractor512(),
            multibox=Multibox(n_class=param['n_fg_class'] + 1,
                              aspect_ratios=((2, ), (2, 3), (2, 3), (2, 3),
                                             (2, 3), (2, ), (2, ))),
            steps=(8, 16, 32, 64, 128, 256, 512),
            sizes=(35.84, 76.8, 153.6, 230.4, 307.2, 384.0, 460.8, 537.6),
            mean=_imagenet_mean)

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #17
0
    def __init__(self,
                 pretrained_model=None,
                 n_fg_class=21,
                 n_point=1000,
                 n_iter=2,
                 mean=(0.485, 0.456, 0.406),
                 std=(0.229, 0.224, 0.225)):
        super(DenseFusion, self).__init__()
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)
        self.n_fg_class = param['n_fg_class']
        self.n_point = n_point
        self.n_iter = n_iter

        with self.init_scope():
            self.posenet = PoseNet(None, n_fg_class, n_point, mean, std)
            self.refiner = PoseRefineNet(n_fg_class, n_point)

        if pretrained_model is not None:
            chainer.serializers.load_npz(path, self)
コード例 #18
0
    def __init__(self, n_class=None, pretrained_model=None, initialW=None):
        param, path = utils.prepare_pretrained_model(
            {'n_class': n_class}, pretrained_model, self._models)
        self.n_class = param['n_class']

        if initialW is None:
            initialW = chainer.initializers.HeNormal()

        super(SegNetBasic, self).__init__()
        with self.init_scope():
            self.conv1 = L.Convolution2D(
                None, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv1_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv2 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv2_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv3 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv3_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv4 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv4_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv_decode4 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv_decode4_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv_decode3 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv_decode3_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv_decode2 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv_decode2_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv_decode1 = L.Convolution2D(
                64, 64, 7, 1, 3, nobias=True, initialW=initialW)
            self.conv_decode1_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv_classifier = L.Convolution2D(
                64, self.n_class, 1, 1, 0, initialW=initialW)

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #19
0
    def __init__(self, n_fg_class=None, pretrained_model=None):
        super(YOLOv2, self).__init__()

        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        self.n_fg_class = param['n_fg_class']
        self.use_preset('visualize')

        with self.init_scope():
            self.extractor = Darknet19Extractor()
            self.subnet = Convolution2D(
                len(self._anchors) * (4 + 1 + self.n_fg_class), 1)

        default_bbox = []
        for v, u in itertools.product(range(self.extractor.grid), repeat=2):
            for h, w in self._anchors:
                default_bbox.append((v, u, h, w))
        self._default_bbox = np.array(default_bbox, dtype=np.float32)

        if path:
            chainer.serializers.load_npz(path, self, strict=False)
コード例 #20
0
    def __init__(self,
                 pretrained_model=None,
                 n_fg_class=21,
                 n_point=1000,
                 mean=(0.485, 0.456, 0.406),
                 std=(0.229, 0.224, 0.225)):
        super(PoseNet, self).__init__()
        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)
        self.n_fg_class = param['n_fg_class']
        self.n_point = n_point
        self.mean = np.array(mean, dtype=np.float32)[:, None, None]
        self.std = np.array(std, dtype=np.float32)[:, None, None]

        with self.init_scope():
            # extractor
            self.resnet_extractor = ResNet18Extractor()
            self.pspnet_extractor = PSPNetExtractor()
            self.posenet_extractor = PoseNetExtractor(self.n_point)
            # conv1
            self.conv1_rot = L.Convolution1D(1408, 640, 1)
            self.conv1_trans = L.Convolution1D(1408, 640, 1)
            self.conv1_conf = L.Convolution1D(1408, 640, 1)
            # conv2
            self.conv2_rot = L.Convolution1D(640, 256, 1)
            self.conv2_trans = L.Convolution1D(640, 256, 1)
            self.conv2_conf = L.Convolution1D(640, 256, 1)
            # conv3
            self.conv3_rot = L.Convolution1D(256, 128, 1)
            self.conv3_trans = L.Convolution1D(256, 128, 1)
            self.conv3_conf = L.Convolution1D(256, 128, 1)
            # conv4
            self.conv4_rot = L.Convolution1D(128, self.n_fg_class * 4, 1)
            self.conv4_trans = L.Convolution1D(128, self.n_fg_class * 3, 1)
            self.conv4_conf = L.Convolution1D(128, self.n_fg_class, 1)

        if pretrained_model is not None:
            chainer.serializers.load_npz(path, self)
コード例 #21
0
    def __init__(
        self,
        n_fg_class=None,
        pretrained_model=None,
        min_size=800,
        max_size=1333,
        roi_size=7,
        ratios=[0.5, 1, 2],
        anchor_scales=[2, 4, 8, 16, 32],
        loc_normalize_mean=(0., 0., 0., 0.),
        loc_normalize_std=(0.1, 0.1, 0.2, 0.2),
        resnet_initialW=None,
        rpn_initialW=None,
        global_module_initialW=None,
        loc_initialW=None,
        score_initialW=None,
        proposal_creator_params=None,
    ):

        param, path = utils.prepare_pretrained_model(
            {'n_fg_class': n_fg_class}, pretrained_model, self._models)

        if resnet_initialW is None and pretrained_model:
            resnet_initialW = chainer.initializers.HeNormal()
        if rpn_initialW is None:
            rpn_initialW = chainer.initializers.Normal(0.01)
        if global_module_initialW is None:
            global_module_initialW = chainer.initializers.Normal(0.01)
        if loc_initialW is None:
            loc_initialW = chainer.initializers.Normal(0.001)
        if score_initialW is None:
            score_initialW = chainer.initializers.Normal(0.01)
        if proposal_creator_params is not None:
            self.proposal_creator_params = proposal_creator_params

        extractor = ResNet101Extractor(initialW=resnet_initialW)
        rpn = RegionProposalNetwork(
            1024,
            512,
            ratios=ratios,
            anchor_scales=anchor_scales,
            feat_stride=self.feat_stride,
            initialW=rpn_initialW,
            proposal_creator_params=self.proposal_creator_params,
        )

        head = LightHeadRCNNResNet101Head(
            param['n_fg_class'] + 1,
            roi_size=roi_size,
            spatial_scale=1. / self.feat_stride,
            global_module_initialW=global_module_initialW,
            loc_initialW=loc_initialW,
            score_initialW=score_initialW)
        mean = np.array([122.7717, 115.9465, 102.9801],
                        dtype=np.float32)[:, None, None]

        super(LightHeadRCNNResNet101,
              self).__init__(extractor, rpn, head, mean, min_size, max_size,
                             loc_normalize_mean, loc_normalize_std)

        if path == 'imagenet':
            self._copy_imagenet_pretrained_resnet()
        elif path:
            chainer.serializers.load_npz(path, self)
コード例 #22
0
    def __init__(self,
                 n_class=None,
                 pretrained_model=None,
                 mean=None,
                 initialW=None,
                 initial_bias=None,
                 arch='tf',
                 depth_multiplier=1.,
                 bn_kwargs=_bn_tf_default_params,
                 thousand_categories_mode=False):
        if depth_multiplier <= 0:
            raise ValueError('depth_multiplier must be greater than 0')

        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'mean': mean,
            }, pretrained_model, self._models[arch][depth_multiplier], {
                'n_class': 1000,
                'mean': _imagenet_mean,
            })
        self.mean = param['mean']
        self.n_class = param['n_class']

        super(MobileNetV2, self).__init__()

        def relu6(x):
            return clipped_relu(x, 6.)

        with self.init_scope():
            conv_out_channels = _depth_multiplied_output_channels(
                32, depth_multiplier)
            self.conv = TFConv2DBNActiv(in_channels=3,
                                        out_channels=conv_out_channels,
                                        stride=2,
                                        ksize=3,
                                        nobias=True,
                                        activ=relu6,
                                        initialW=initialW,
                                        bn_kwargs=bn_kwargs)
            expanded_out_channels = _depth_multiplied_output_channels(
                16, depth_multiplier)
            self.expanded_conv = ExpandedConv2D(
                expansion_size=expand_input_by_factor(1, divisible_by=1),
                in_channels=conv_out_channels,
                out_channels=expanded_out_channels,
                initialW=initialW,
                bn_kwargs=bn_kwargs)
            in_channels = expanded_out_channels
            out_channels_list = (24, ) * 2 + (32, ) * 3 + (64, ) * 4 + (
                96, ) * 3 + (160, ) * 3 + (320, )
            for i, out_channels in enumerate(out_channels_list):
                layer_id = i + 1
                if layer_id in (1, 3, 6, 13):
                    stride = 2
                else:
                    stride = 1
                multiplied_out_channels = _depth_multiplied_output_channels(
                    out_channels, depth_multiplier)
                setattr(
                    self, "expanded_conv_{}".format(layer_id),
                    ExpandedConv2D(in_channels=in_channels,
                                   out_channels=multiplied_out_channels,
                                   depthwise_stride=stride,
                                   initialW=initialW,
                                   bn_kwargs=bn_kwargs))
                in_channels = multiplied_out_channels
            if depth_multiplier < 1:
                conv1_out_channels = 1280
            else:
                conv1_out_channels = _depth_multiplied_output_channels(
                    1280, depth_multiplier)
            self.conv1 = TFConv2DBNActiv(in_channels=in_channels,
                                         out_channels=conv1_out_channels,
                                         ksize=1,
                                         nobias=True,
                                         initialW=initialW,
                                         activ=relu6,
                                         bn_kwargs=bn_kwargs)
            self.global_average_pool = \
                lambda x: average_pooling_2d(x, ksize=x.shape[2:4], stride=1)
            self.logits_conv = TFConvolution2D(
                in_channels=conv1_out_channels,
                out_channels=self.n_class,
                ksize=1,
                nobias=False,  # bias is needed
                initialW=initialW,
                initial_bias=initial_bias,
            )
            self.squeeze = lambda x: squeeze(x, axis=(2, 3))
            self.softmax = softmax

        if path:
            chainer.serializers.load_npz(path, self)

        if thousand_categories_mode and 1000 < n_class:
            self.logits_conv.W.data = np.delete(self.logits_conv.W.data, 0, 0)
            self.logits_conv.b.data = np.delete(self.logits_conv.b.data, 0)
コード例 #23
0
    def __init__(self,
                 n_class=None,
                 pretrained_model=None,
                 input_size=None,
                 initialW=None):
        super(PSPNet, self).__init__()

        if pretrained_model == 'imagenet':
            extractor_pretrained_model = 'imagenet'
            pretrained_model = None
        else:
            extractor_pretrained_model = None

        param, path = utils.prepare_pretrained_model(
            {
                'n_class': n_class,
                'input_size': input_size
            },
            pretrained_model,
            self._models,
            default={'input_size': (713, 713)})
        n_class = param['n_class']
        input_size = param['input_size']
        if not isinstance(input_size, (list, tuple)):
            input_size = (int(input_size), int(input_size))
        self.input_size = input_size

        if initialW is None:
            if pretrained_model:
                initialW = initializers.constant.Zero()

        kwargs = self._extractor_kwargs
        kwargs.update({
            'pretrained_model': extractor_pretrained_model,
            'initialW': initialW
        })
        extractor = self._extractor_cls(**kwargs)
        extractor.pick = self._extractor_pick

        self.scales = None
        self.mean = _imagenet_mean

        pyramids = [6, 3, 2, 1]
        feat_size = (input_size[0] // 8, input_size[1] // 8)
        with self.init_scope():
            self.extractor = extractor
            self.ppm = PyramidPoolingModule(2048,
                                            feat_size,
                                            pyramids,
                                            initialW=initialW)
            self.head_conv1 = Conv2DBNActiv(4096,
                                            512,
                                            3,
                                            1,
                                            1,
                                            initialW=initialW)
            self.head_conv2 = L.Convolution2D(512, n_class, 1, 1, 0, False,
                                              initialW)

        if path:
            chainer.serializers.load_npz(path, self)
コード例 #24
0
    def __init__(self,
                 n_class=None,
                 pretrained_model=None,
                 initialW=None,
                 dtype='float32'):
        param, path = utils.prepare_pretrained_model({'n_class': n_class},
                                                     pretrained_model,
                                                     self._models)
        self.n_class = param['n_class']
        self.dtype = dtype

        if initialW is None:
            initialW = chainer.initializers.HeNormal()

        super(SegNetBasic, self).__init__()
        with self.init_scope():
            self.lrn = lambda x: F.local_response_normalization(
                x, 5, 1, 1e-4 / 5., 0.75)

            self.conv1 = L.Convolution2D(None,
                                         64,
                                         7,
                                         1,
                                         3,
                                         nobias=True,
                                         initialW=initialW)
            self.conv1_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv1_relu = lambda x: F.relu(x)
            self.conv1_pool = lambda x: F.max_pooling_2d(
                x, 2, 2, return_indices=True)

            self.conv2 = L.Convolution2D(64,
                                         64,
                                         7,
                                         1,
                                         3,
                                         nobias=True,
                                         initialW=initialW)
            self.conv2_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv2_relu = lambda x: F.relu(x)
            self.conv2_pool = lambda x: F.max_pooling_2d(
                x, 2, 2, return_indices=True)

            self.conv3 = L.Convolution2D(64,
                                         64,
                                         7,
                                         1,
                                         3,
                                         nobias=True,
                                         initialW=initialW)
            self.conv3_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv3_relu = lambda x: F.relu(x)
            self.conv3_pool = lambda x: F.max_pooling_2d(
                x, 2, 2, return_indices=True)

            self.conv4 = L.Convolution2D(64,
                                         64,
                                         7,
                                         1,
                                         3,
                                         nobias=True,
                                         initialW=initialW)
            self.conv4_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv4_relu = lambda x: F.relu(x)
            self.conv4_pool = lambda x: F.max_pooling_2d(
                x, 2, 2, return_indices=True)

            self.upsampling4 = lambda x, indices: self._upsampling_2d(
                x, indices)
            self.conv_decode4 = L.Convolution2D(64,
                                                64,
                                                7,
                                                1,
                                                3,
                                                nobias=True,
                                                initialW=initialW)
            self.conv_decode4_bn = L.BatchNormalization(64, initial_beta=0.001)

            self.upsampling3 = lambda x, indices: self._upsampling_2d(
                x, indices)
            self.conv_decode3 = L.Convolution2D(64,
                                                64,
                                                7,
                                                1,
                                                3,
                                                nobias=True,
                                                initialW=initialW)
            self.conv_decode3_bn = L.BatchNormalization(64, initial_beta=0.001)

            self.upsampling2 = lambda x, indices: self._upsampling_2d(
                x, indices)
            self.conv_decode2 = L.Convolution2D(64,
                                                64,
                                                7,
                                                1,
                                                3,
                                                nobias=True,
                                                initialW=initialW)
            self.conv_decode2_bn = L.BatchNormalization(64, initial_beta=0.001)

            self.upsampling1 = lambda x, indices: self._upsampling_2d(
                x, indices)
            self.conv_decode1 = L.Convolution2D(64,
                                                64,
                                                7,
                                                1,
                                                3,
                                                nobias=True,
                                                initialW=initialW)
            self.conv_decode1_bn = L.BatchNormalization(64, initial_beta=0.001)
            self.conv_classifier = L.Convolution2D(64,
                                                   self.n_class,
                                                   1,
                                                   1,
                                                   0,
                                                   initialW=initialW)

        self.type_cast_ada_loss = None

        if path:
            chainer.serializers.load_npz(path, self)