Esempio n. 1
0
def _check_pretrained_model(n_fg_class, pretrained_model, models):
    if pretrained_model in models:
        model = models[pretrained_model]
        if n_fg_class and not n_fg_class == model['n_fg_class']:
            raise ValueError('n_fg_class mismatch')
        n_fg_class = model['n_fg_class']

        root = get_dataset_directory('pfnet/chainercv/models')
        basename = os.path.basename(model['url'])
        path = os.path.join(root, basename)
        if not os.path.exists(path):
            download_file = download.cached_download(model['url'])
            os.rename(download_file, path)

        if not _available:
            warnings.warn(
                'cv2 is not installed on your environment. '
                'Pretrained models are trained with cv2. '
                'The performace may change with Pillow backend.',
                RuntimeWarning)
    elif pretrained_model:
        path = pretrained_model
    else:
        path = None

    return n_fg_class, path
Esempio n. 2
0
    def __init__(self, n_class=None, pretrained_model=None, initialW=None):
        if n_class is None:
            if pretrained_model not in self._models:
                raise ValueError(
                    'The n_class needs to be supplied as an argument.')
            n_class = self._models[pretrained_model]['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,
                                                   n_class,
                                                   1,
                                                   1,
                                                   0,
                                                   initialW=initialW)

        self.n_class = n_class

        if pretrained_model in self._models:
            data_root = get_dataset_directory('pfnet/chainercv/models')
            url = self._models[pretrained_model]['url']
            fn = os.path.basename(url)
            dest_fn = os.path.join(data_root, fn)
            if not os.path.exists(dest_fn):
                download_file = download.cached_download(url)
                os.rename(download_file, dest_fn)
            chainer.serializers.load_npz(dest_fn, self)
        elif pretrained_model:
            chainer.serializers.load_npz(pretrained_model, self)
Esempio n. 3
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={}):
        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 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 = VGG16FeatureExtractor(initialW=vgg_initialW)
        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=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 pretrained_model in self._models:
            data_root = get_dataset_directory('pfnet/chainercv/models')
            url = self._models[pretrained_model]['url']
            fn = os.path.basename(url)
            dest_fn = os.path.join(data_root, fn)
            if not os.path.exists(dest_fn):
                download_file = download.cached_download(url)
                os.rename(download_file, dest_fn)
            chainer.serializers.load_npz(dest_fn, self)
        elif pretrained_model == 'imagenet':
            self._copy_imagenet_pretrained_vgg16()
        elif pretrained_model:
            chainer.serializers.load_npz(pretrained_model, self)