Beispiel #1
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)
Beispiel #2
0
    def __init__(self, initialW=None):
        super(ResNet101Extractor, self).__init__()

        if initialW is None:
            initialW = chainer.initializers.HeNormal()
        kwargs = {
            'initialW': initialW,
            'bn_kwargs': {
                'eps': 1e-5
            },
            'stride_first': True
        }

        with self.init_scope():
            # ResNet
            self.conv1 = Conv2DBNActiv(3,
                                       64,
                                       7,
                                       2,
                                       3,
                                       nobias=True,
                                       initialW=initialW)
            self.pool1 = lambda x: F.max_pooling_2d(x, ksize=3, stride=2)
            self.res2 = ResBlock(3, 64, 64, 256, 1, **kwargs)
            self.res3 = ResBlock(4, 256, 128, 512, 2, **kwargs)
            self.res4 = ResBlock(23, 512, 256, 1024, 2, **kwargs)
            self.res5 = ResBlock(3, 1024, 512, 2048, 1, 2, **kwargs)
Beispiel #3
0
    def __init__(self,
                 n_layer,
                 n_class=None,
                 pretrained_model=None,
                 mean=None,
                 initialW=None,
                 fc_kwargs={},
                 arch='fb'):
        if arch == 'fb':
            if pretrained_model == 'imagenet':
                raise ValueError(
                    'Pretrained weights for Facebook ResNet models '
                    'are not supported. Please set mode to \'he\'.')
            stride_first = False
            conv1_no_bias = True
        elif arch == 'he':
            stride_first = True
            conv1_no_bias = False
        else:
            raise ValueError('arch is expected to be one of [\'he\', \'fb\']')
        _models = self._models[arch][n_layer]
        blocks = self._blocks[n_layer]

        if n_class is None:
            if pretrained_model in _models:
                n_class = _models[pretrained_model]['n_class']
            else:
                n_class = 1000

        if mean is None:
            if pretrained_model in _models:
                mean = _models[pretrained_model]['mean']
            else:
                mean = _imagenet_mean
        self.mean = mean

        if initialW is None:
            initialW = 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 = _global_average_pooling_2d
            self.fc6 = L.Linear(None, n_class, **fc_kwargs)
            self.prob = F.softmax

        if pretrained_model in _models:
            if not _available:
                warnings.warn(
                    'cv2 is not installed on your environment. '
                    'The scores of ResNets reported in the '
                    'README of the ChainerCV\'s classification '
                    'example are calculated using OpenCV as the '
                    'backend. With Pillow as the '
                    'backend, the scores would change.', RuntimeWarning)
            path = download_model(_models[pretrained_model]['url'])
            chainer.serializers.load_npz(path, self)
        elif pretrained_model:
            chainer.serializers.load_npz(pretrained_model, self)
Beispiel #4
0
    def __init__(self,
                 n_layer,
                 n_class=None,
                 pretrained_model=None,
                 mean=None,
                 initialW=None,
                 arch='fb'):
        if arch == 'fb':
            if pretrained_model == 'imagenet':
                raise ValueError(
                    'Pretrained weights for Facebook ResNet models '
                    'are not supported. Please set mode to \'he\'.')
            stride_first = False
            conv1_no_bias = True
        elif arch == 'he':
            stride_first = True
            conv1_no_bias = False
        else:
            raise ValueError('arch is expected to be one of [\'he\', \'fb\']')
        _models = self._models[arch][n_layer]
        blocks = self._blocks[n_layer]

        if n_class is None:
            if pretrained_model in _models:
                n_class = _models[pretrained_model]['n_class']
            else:
                n_class = 1000

        if mean is None:
            if pretrained_model in _models:
                mean = _models[pretrained_model]['mean']
            else:
                mean = _imagenet_mean
        self.mean = mean

        if initialW is None:
            conv_initialW = HeNormal(scale=1., fan_option='fan_out')
            fc_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()
        kwargs = {'initialW': conv_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=conv_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 = _global_average_pooling_2d
            self.fc6 = L.Linear(None, n_class, initialW=fc_initialW)
            self.prob = F.softmax

        if pretrained_model in _models:
            path = download_model(_models[pretrained_model]['url'])
            chainer.serializers.load_npz(path, self)
        elif pretrained_model:
            chainer.serializers.load_npz(pretrained_model, self)