2

I have pretrained a VGG network in chainer loadable npz file format, but add a new FC layer to last layer , and I modify last layer output class_number. I have already modify the layer name in order to use chainer loadable file to other unaltered layer. But I failed.

Traceback (most recent call last):
  File "chainercv/trainer/train.py", line 194, in <module>
    main()
  File "chainercv/trainer/train.py", line 85, in main
    mean_file=args.mean)  # 可改为/home/machen/face_expr/result/snapshot_model.npz
  File "/home/machen/face_expr/chainercv/links/model/faster_rcnn/faster_rcnn_vgg.py", line 131, in __init__
    chainer.serializers.load_npz(pretrained_model, self)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/chainer-3.0.0a1-py3.6.egg/chainer/serializers/npz.py", line 140, in load_npz
    d.load(obj)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/chainer-3.0.0a1-py3.6.egg/chainer/serializer.py", line 82, in load
    obj.serialize(self)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/chainer-3.0.0a1-py3.6.egg/chainer/link.py", line 794, in serialize
    d[name].serialize(serializer[name])
  File "/usr/local/anaconda3/lib/python3.6/site-packages/chainer-3.0.0a1-py3.6.egg/chainer/link.py", line 794, in serialize
    d[name].serialize(serializer[name])
  File "/usr/local/anaconda3/lib/python3.6/site-packages/chainer-3.0.0a1-py3.6.egg/chainer/link.py", line 550, in serialize
    data = serializer(name, param.data)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/chainer-3.0.0a1-py3.6.egg/chainer/serializers/npz.py", line 106, in __call__
    dataset = self.npz[key]
  File "/usr/local/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py", line 237, in __getitem__
    raise KeyError("%s is not a file in the archive" % key)
KeyError: 'head/score_mod/W is not a file in the archive'
machen
  • 283
  • 2
  • 10
  • How about loading model before modify layer name? When loading trained-model, it will look up the layer name to assign parameter, so I think the original layer name is necessary when loading. – corochann Aug 09 '17 at 11:33
  • I want to modify the FC layer's output neuron number, so the pretrained model cannot use for this layer. And I want add new layer to my model. – machen Aug 09 '17 at 15:12

1 Answers1

1

I made custom class to over write some layer like this. you can control load timing for pretrained model using init flag

class MyRes(chainer.Chain):
def __init__(self, path=default_path, init=False):
    super(MyRes, self).__init__(
        c1 = L.Convolution2D(None, 64, 7, 2, 3),
        resnet = ResNet50Layers(pretrained_model=None),
    )   
    if not init:
        serializers.load_npz(path, self.resnet)
    self.resnet.conv1 = self.c1

when you init for training you can simply add path of model

res = MyRes(path=pretrained_model_path)

when you load trained model of MyModel, set init flag like this

res = MyRes(init=True)
serializers.load_npz(saved_myres, self.resnet)
  • Do you mean you only to wrap an outter class MyRes outside ResNet50Layers which is unaltered layer to use pretrained model? – machen Aug 09 '17 at 15:07
  • what is your last word: serializers.load_npz(saved_myres, self.resnet) mean? – machen Aug 09 '17 at 15:08
  • I think the point is altering the layer after loading the pre-trained model. – corochann Aug 10 '17 at 00:03
  • @corochann I only have one model class definition, How to altering the layer after load, I don't have ability to modify code after the process is already running. – machen Aug 10 '17 at 00:47
  • @corochann Oh, after what you say, I check the code above , I understand. – machen Aug 10 '17 at 00:49
  • @corochann When I use your method, report error: ValueError: numpy and cupy must not be used together type(W): , type(x): – machen Aug 10 '17 at 02:49
  • numpy is used in CPU, while cupy is used in GPU. Are you using GPU? if yes, did you write model.to_gpu() ? – corochann Aug 10 '17 at 06:42