Detectron: caffemodel转pkl

Detectron 利用caffe2的模型转换功能,将caffemodel转换为pkl,以VGG16为例。

首先去VGG官网下载pretrained的caffemodel和prototxt,http://www.robots.ox.ac.uk/~vgg/research/very_deep/
然后运行如下代码

1
2
3
4
python2 tools/pickle_caffe_blobs.py \
--prototxt /home/all/models/VGG_ILSVRC_16_layers_deploy.prototxt \
--caffemodel /home/all/models/VGG_ILSVRC_16_layers.caffemodel \
--output output/vgg16.pkl

这时会出现问题

1
2
3
4
5
6
7
8
9
10
Traceback (most recent call last):
File "tools/pickle_caffe_blobs.py", line 223, in <module>
args.prototxt_file_name, args.caffemodel_file_name
File "tools/pickle_caffe_blobs.py", line 210, in load_and_convert_caffe_model
caffenet, caffenet_weights
File "/home/all/lib/anaconda2/envs/caffe2/lib/python2.7/site-packages/caffe2/python/caffe_translator.py", line 299, in TranslateModel
return TranslatorRegistry.TranslateModel(*args, **kwargs)
File "/home/all/lib/anaconda2/envs/caffe2/lib/python2.7/site-packages/caffe2/python/caffe_translator.py", line 254, in TranslateModel
'I think something is wrong. This translation script '
ValueError: I think something is wrong. This translation script only accepts new style layers that are stored in the layer field.

查询issue/823可知,这是因为caffe2转换工具使用的是caffe V1版的proto和caffemodel,而早期的caffemodel如vgg16是老版本,因此需要进行升级。
升级工具在caffe/tools里,因此先下载caffe然后编译,编译前需要修改Makefile.config来设置环境变量

1
2
3
git clone https://github.com/BVLC/caffe.git
cd caffe
make

然后使用工具升级prototxt和caffemodel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd build/tools
xuzhewei@LabServer000:~/lib/caffe-1.0/build/tools$ ./upgrade_net_proto_text /home/all/models/VGG_ILSVRC_16_layers_deploy.prototxt /home/all/models/VGG_ILSVRC_16_layers_deploy_V1.prototxt
I0419 14:14:13.562763 23401 upgrade_proto.cpp:53] Attempting to upgrade input file specified using deprecated V1LayerParameter: /home/all/models/VGG_ILSVRC_16_layers_deploy.prototxt
I0419 14:14:13.563534 23401 upgrade_proto.cpp:61] Successfully upgraded file specified using deprecated V1LayerParameter
I0419 14:14:13.563611 23401 upgrade_proto.cpp:67] Attempting to upgrade input file specified using deprecated input fields: /home/all/models/VGG_ILSVRC_16_layers_deploy.prototxt
I0419 14:14:13.563650 23401 upgrade_proto.cpp:70] Successfully upgraded file specified using deprecated input fields.
W0419 14:14:13.563665 23401 upgrade_proto.cpp:72] Note that future Caffe releases will only support input layers and not input fields.
I0419 14:14:13.564394 23401 upgrade_net_proto_text.cpp:49] Wrote upgraded NetParameter text proto to /home/all/models/VGG_ILSVRC_16_layers_deploy_V1.prototxt
xuzhewei@LabServer000:~/lib/caffe-1.0/build/tools$ ./upgrade_net_proto_binary /home/all/models/VGG_ILSVRC_16_layers.caffemodel /home/all/models/VGG_ILSVRC_16_layers_V1.caffemodel
I0419 14:15:38.643944 24177 upgrade_proto.cpp:53] Attempting to upgrade input file specified using deprecated V1LayerParameter: /home/all/models/VGG_ILSVRC_16_layers.caffemodel
I0419 14:15:40.964524 24177 upgrade_proto.cpp:61] Successfully upgraded file specified using deprecated V1LayerParameter
I0419 14:15:40.993201 24177 upgrade_proto.cpp:67] Attempting to upgrade input file specified using deprecated input fields: /home/all/models/VGG_ILSVRC_16_layers.caffemodel
I0419 14:15:40.993227 24177 upgrade_proto.cpp:70] Successfully upgraded file specified using deprecated input fields.
W0419 14:15:40.993252 24177 upgrade_proto.cpp:72] Note that future Caffe releases will only support input layers and not input fields.
I0419 14:15:41.512004 24177 upgrade_net_proto_binary.cpp:48] Wrote upgraded NetParameter binary proto to /home/all/models/VGG_ILSVRC_16_layers_V1.caffemodel

最后再运行detectron的转换代码

1
2
3
4
python2 tools/pickle_caffe_blobs.py \
--prototxt /home/all/models/VGG_ILSVRC_16_layers_deploy_V1.prototxt \
--caffemodel /home/all/models/VGG_ILSVRC_16_layers_V1.caffemodel \
--output output/vgg16.pkl