예제 #1
0
 def convert_caffemodel_to_npz(self, path_caffemodel, path_npz):
     # As CaffeFunction uses shortcut symbols,
     # we import CaffeFunction here.
     from chainer.links.caffe.caffe_function import CaffeFunction
     caffemodel = CaffeFunction(path_caffemodel)
     _transfer_resnet152(caffemodel, self)
     npz.save_npz(path_npz, self, compression=False)
예제 #2
0
def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('model_name',
                        choices=('resnet50', 'resnet101', 'resnet152'))
    parser.add_argument('caffemodel')
    parser.add_argument('output', nargs='?', default=None)
    args = parser.parse_args()

    caffemodel = CaffeFunction(args.caffemodel)
    if args.model_name == 'resnet50':
        model = ResNet50(pretrained_model=None, n_class=1000, mode='he')
        model(np.zeros((1, 3, 224, 224), dtype=np.float32))
        _transfer_resnet50(caffemodel, model)
    elif args.model_name == 'resnet101':
        model = ResNet101(pretrained_model=None, n_class=1000, mode='he')
        model(np.zeros((1, 3, 224, 224), dtype=np.float32))
        _transfer_resnet101(caffemodel, model)
    elif args.model_name == 'resnet152':
        model = ResNet152(pretrained_model=None, n_class=1000, mode='he')
        model(np.zeros((1, 3, 224, 224), dtype=np.float32))
        _transfer_resnet152(caffemodel, model)

    if args.output is None:
        output = '{}_imagenet_convert.npz'.format(args.model_name)
    else:
        output = args.output
    chainer.serializers.save_npz(output, model)
예제 #3
0
파일: vgg.py 프로젝트: km-t/dcpython
    def convert_caffemodel_to_npz(cls, path_caffemodel, path_npz):
        """Converts a pre-trained caffemodel to a chainer model.

        Args:
            path_caffemodel (str): Path of the pre-trained caffemodel.
            path_npz (str): Path of the converted chainer model.
        """

        # As CaffeFunction uses shortcut symbols,
        # we import CaffeFunction here.
        from chainer.links.caffe.caffe_function import CaffeFunction
        caffemodel = CaffeFunction(path_caffemodel)
        npz.save_npz(path_npz, caffemodel, compression=False)
예제 #4
0
    def _load_pretrained_weights(self, caffemodel_path):
        # As CaffeFunction uses shortcut symbols,
        # CaffeFunction is imported here.
        from chainer.links.caffe.caffe_function import CaffeFunction
        src = CaffeFunction(caffemodel_path)

        self.conv1.W.data[:] = src.conv1.W.data
        self.conv1.b.data[:] = src.conv1.b.data
        self.bn1.avg_mean[:] = src.bn_conv1.avg_mean
        self.bn1.avg_var[:] = src.bn_conv1.avg_var
        self.bn1.gamma.data[:] = src.scale_conv1.W.data
        self.bn1.beta.data[:] = src.scale_conv1.bias.b.data

        self._load_weights_block(self.res2, src, ['2a', '2b', '2c'])
        self._load_weights_block(self.res3, src, ['3a', '3b', '3c', '3d'])
        self._load_weights_block(self.res4, src,
                                 ['4a', '4b', '4c', '4d', '4e', '4f'])
        self._load_weights_block(self.res5, src, ['5a', '5b', '5c'])
예제 #5
0
def load_model(model_name, n_class):
    archs = {'nin': NIN, 'vgg16': VGG16BatchNormalization}
    model = archs[model_name](n_class=n_class)
    if model_name == 'nin':
        pass
    elif model_name == 'vgg16':
        rospack = rospkg.RosPack()
        model_path = osp.join(rospack.get_path('decopin_hand'), 'scripts',
                              'vgg16', 'VGG_ILSVRC_16_layers.npz')
        if not osp.exists(model_path):
            from chainer.dataset import download
            from chainer.links.caffe.caffe_function import CaffeFunction
            path_caffemodel = download.cached_download(
                'http://www.robots.ox.ac.uk/%7Evgg/software/very_deep/caffe/VGG_ILSVRC_19_layers.caffemodel'
            )
            caffemodel = CaffeFunction(path_caffemodel)
            npz.save_npz(model_path, caffemodel, compression=False)

        vgg16 = VGG16Layers(
            pretrained_model=model_path)  # original VGG16 model
        print('Load model from {}'.format(model_path))
        for l in model.children():
            if l.name.startswith('conv'):
                # l.disable_update()  # Comment-in for transfer learning, comment-out for fine tuning
                l1 = getattr(vgg16, l.name)
                l2 = getattr(model, l.name)
                assert l1.W.shape == l2.W.shape
                assert l1.b.shape == l2.b.shape
                l2.W.data[...] = l1.W.data[...]
                l2.b.data[...] = l1.b.data[...]
            elif l.name in ['fc6', 'fc7']:
                l1 = getattr(vgg16, l.name)
                l2 = getattr(model, l.name)
                assert l1.W.size == l2.W.size
                assert l1.b.size == l2.b.size
                l2.W.data[...] = l1.W.data.reshape(l2.W.shape)[...]
                l2.b.data[...] = l1.b.data.reshape(l2.b.shape)[...]
    else:
        print('Model type {} is invalid.'.format(model_name))
        exit()

    return model
예제 #6
0
    def convert_caffemodel_to_npz(cls, path_caffemodel, path_npz, n_layers=50):
        """Converts a pre-trained caffemodel to a chainer model.
        Args:
            path_caffemodel (str): Path of the pre-trained caffemodel.
            path_npz (str): Path of the converted chainer model.
        """

        # As CaffeFunction uses shortcut symbols,
        # we import CaffeFunction here.
        from chainer.links.caffe.caffe_function import CaffeFunction
        caffemodel = CaffeFunction(path_caffemodel)
        chainermodel = cls(pretrained_model=None, n_layers=n_layers)
        if n_layers == 50:
            _transfer_resnet50(caffemodel, chainermodel)
        elif n_layers == 101:
            _transfer_resnet101(caffemodel, chainermodel)
        elif n_layers == 152:
            _transfer_resnet152(caffemodel, chainermodel)
        else:
            raise ValueError('The n_layers argument should be either 50, 101,'
                             ' or 152, but {} was given.'.format(n_layers))
        npz.save_npz(path_npz, chainermodel, compression=False)
예제 #7
0
def convert_caffemodel_to_npz(path_caffemodel, path_npz):
    from chainer.links.caffe.caffe_function import CaffeFunction
    caffemodel = CaffeFunction(path_caffemodel)
    npz.save_npz(path_npz, caffemodel, compression=False)
예제 #8
0
            self.conv3 = L.Convolution2D(None, 384, 3, pad=1)
            self.conv4 = L.Convolution2D(None, 384, 3, pad=1)
            self.conv5 = L.Convolution2D(None, 256, 3, pad=1)
            self.fc6 = L.Linear(None, 4096)
            self.fc7 = L.Linear(None, 4096)
            self.fc8 = L.Linear(None, nb_class)

    def __call__(self, x, t):
        h = F.max_pooling_2d(F.local_response_normalization(
            F.relu(self.conv1(x))),
                             3,
                             stride=2)
        h = F.max_pooling_2d(F.local_response_normalization(
            F.relu(self.conv2(h))),
                             3,
                             stride=2)
        h = F.relu(self.conv3(h))
        h = F.relu(self.conv4(h))
        h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2)
        h = F.dropout(F.relu(self.fc6(h)))
        h = F.dropout(F.relu(self.fc7(h)))
        h = self.fc8(h)
        return h


if __name__ == "__main__":
    caffemodel = CaffeFunction("bvlc_alexnet.caffemodel")
    npz.save_npz("alexnet.npz", caffemodel, compression=False)
    alexnet = Alex()
    npz.load_npz("alexnet.npz", alexnet)
예제 #9
0
def load_caffemodel(file_path):
    caffemodel = CaffeFunction("mobilenet.caffemodel")
예제 #10
0
    def __init__(self,
                 n_fg_class=None,
                 pretrained_model=None,
                 min_size=config.IMG_SIZE[0], max_size=config.IMG_SIZE[0],
                 vgg_initialW=None,
                 score_initialW=None,
                 mean_file=None,
                 extract_len=None,
                 dataset="BP4D", fold=3, split_idx=1):
        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 = len(config.AU_SQUEEZE)
        if score_initialW is None:
            score_initialW = chainer.initializers.Normal(0.01)
        if vgg_initialW is None and pretrained_model:
            vgg_initialW = chainer.initializers.constant.Zero()

        extractor = VGG19FeatureExtractor(initialW=vgg_initialW)
        head = VGG19RoIHead(
            n_fg_class,  # 注意:全0表示背景。010101才表示多label,因此无需一个特别的0的神经元节点
            roi_size=7, spatial_scale=1. / self.feat_stride, # 1/ 16.0 means after extract feature map, the map become 1/16 of original image, ROI bbox also needs shrink
            vgg_initialW=vgg_initialW,
            score_initialW=score_initialW,
            extract_len=extract_len
        )
        self.mean_file = mean_file
        mean_array = np.load(mean_file)
        print("loading mean_file in: {} done".format(mean_file))
        super(FasterRCNNVGG19, self).__init__(
            extractor,
            head,
            mean=mean_array,
            min_size=min_size,
            max_size=max_size
        )

        if pretrained_model in self._models and 'url' in self._models[pretrained_model]:
            path = download_model(self._models[pretrained_model]['url'])
            chainer.serializers.load_npz(path, self)
        elif pretrained_model == 'vgg':  # 只会走到这一elif里
            print("loading:{} imagenet pretrained model".format(self._models['imagenet']['path']))

            model_path = self._models['imagenet']['path']
            if model_path.endswith(".caffemodel"):
                caffe_model = CaffeFunction(model_path)

                chainer_model = VGG19Layers(pretrained_model=None)
                self._transfer_vgg(caffe_model, chainer_model)
                chainer_model_save_path = "{}/VGG_ILSVRC_16_layers.npz".format(os.path.dirname(model_path))
                chainer.serializers.save_npz(chainer_model_save_path, chainer_model)
                self._copy_imagenet_pretrained_vgg19(path=chainer_model_save_path)
            elif model_path.endswith(".npz"):
                self._copy_imagenet_pretrained_vgg19(path=model_path)
        elif pretrained_model == "vgg16":
            model_path = self._models['imagenet']['path']
            assert os.path.exists(model_path), model_path
            self._copy_imagenet_pretrained_vgg19(path=model_path)
            # model_path = '{0}/AU_rcnn_trained_model/{1}_vgg16/{2}_fold_{3}_vgg_linear_model_snapshot.npz'.format(config.ROOT_PATH, dataset, fold, split_idx)
            # assert os.path.exists(model_path), model_path
            # self._copy_param_from_vgg16(model_path)

        elif pretrained_model.endswith("npz"):
            print("loading :{} to AU R-CNN VGG19".format(pretrained_model))
            chainer.serializers.load_npz(pretrained_model, self)  #FIXME 我修改了最后加了一层fc8, 变成1024维向量,但是无法load
예제 #11
0
import argparse

from chainer.links.caffe.caffe_function import CaffeFunction
from chainer.serializers import npz

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Convert caffemodel to npz')
    parser.add_argument('--input_caffemodel',
                        '-i',
                        default='VGG_ILSVRC_16_layers.caffemodel')
    parser.add_argument('--output_npz',
                        '-o',
                        default='VGG_ILSVRC_16_layers.npz')
    args = parser.parse_args()

    caffemodel = CaffeFunction(args.input_caffemodel)
    npz.save_npz(args.output_npz, caffemodel, compression=False)
예제 #12
0
def caffe2npz(caffe_path):
    caffe_model = CaffeFunction(caffe_path)
    npz_filename = Path(caffe_path).stem + '.npz'
    npz.save_npz(npz_filename, caffe_model, compression=False)
    return npz_filename
예제 #13
0
 def convert_caffemodel_to_npz(cls, path_caffemodel, path_npz):
     from chainer.links.caffe.caffe_function import CaffeFunction
     caffemodel = CaffeFunction(path_caffemodel)
     chainermodel = cls(pretrained_model=None)
     _transfer_xception(caffemodel, chainermodel)
     chainer.serializers.save_npz(path_npz, chainermodel, compression=False)
예제 #14
0
def _convert_caffemodel_to_npz(path_caffemodel, path_npz):
    # As CaffeFunction uses shortcut symbols,
    # we import CaffeFunction here.
    from chainer.links.caffe.caffe_function import CaffeFunction
    caffemodel = CaffeFunction(path_caffemodel)
    npz.save_npz(path_npz, caffemodel, compression=False)
예제 #15
0
from chainer.links.caffe.caffe_function import CaffeFunction
from chainer.serializers import npz

caffemodel = CaffeFunction('./src/VGG_ILSVRC_19_layers.caffemodel')
npz.save_npz('VGG_ILSVRC_19_layers.npz', caffemodel, compression=False)