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)
def _copy_imagenet_pretrained_vgg16(self): pretrained_model = VGG16(pretrained_model='imagenet') self.extractor.conv1_1.copyparams(pretrained_model.conv1_1) self.extractor.conv1_2.copyparams(pretrained_model.conv1_2) self.extractor.conv2_1.copyparams(pretrained_model.conv2_1) self.extractor.conv2_2.copyparams(pretrained_model.conv2_2) self.extractor.conv3_1.copyparams(pretrained_model.conv3_1) self.extractor.conv3_2.copyparams(pretrained_model.conv3_2) self.extractor.conv3_3.copyparams(pretrained_model.conv3_3) self.extractor.conv4_1.copyparams(pretrained_model.conv4_1) self.extractor.conv4_2.copyparams(pretrained_model.conv4_2) self.extractor.conv4_3.copyparams(pretrained_model.conv4_3) self.extractor.conv5_1.copyparams(pretrained_model.conv5_1) self.extractor.conv5_2.copyparams(pretrained_model.conv5_2) self.extractor.conv5_3.copyparams(pretrained_model.conv5_3) self.head.fc6.copyparams(pretrained_model.fc6) self.head.fc7.copyparams(pretrained_model.fc7)
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, mask_initialW=None, proposal_creator_params=None, pooling_func=functions.roi_align_2d, roi_size=7, ): if n_fg_class is None: if pretrained_model not in self._models: raise ValueError( 'The n_fg_class needs to be supplied as an argument') n_fg_class = self._models[pretrained_model]['n_fg_class'] 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 mask_initialW is None: mask_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() if proposal_creator_params is None: proposal_creator_params = dict( n_train_pre_nms=12000, n_train_post_nms=2000, n_test_pre_nms=6000, n_test_post_nms=1000, min_size=0, ) 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( n_fg_class + 1, roi_size=roi_size, spatial_scale=1. / self.feat_stride, vgg_initialW=vgg_initialW, loc_initialW=loc_initialW, score_initialW=score_initialW, mask_initialW=mask_initialW, pooling_func=pooling_func, ) super(MaskRCNNVGG16, 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 pretrained_model in self._models: path = download_model(self._models[pretrained_model]['url']) chainer.serializers.load_npz(path, self) elif pretrained_model == 'imagenet': self._copy_imagenet_pretrained_vgg16() elif pretrained_model: chainer.serializers.load_npz(pretrained_model, self)