Extracting weights from .caffemodel without caffe installed in Python

£可爱£侵袭症+ 提交于 2019-12-21 05:46:07

问题


Is there a relatively simple way to extract weights in Python from one of the many pretrained models in Caffe Zoo WITHOUT CAFFE (nor pyCaffe)? i.e. parsing .caffemodel to hdf5/numpy or whatever format that can be read by Python?

All the answers I found use C++ code with caffe classes or Pycaffe. I have looked at pycaffe's code it looks like you really need caffe to make sense of the binary is that the only solution?


回答1:


I had to resolve that exact issue just now. Assuming you have a .caffemodel (binary proto format), it turns out to be quite simple.

  1. Download the latest caffe.proto

  2. Compile into python library: protoc --python_out=. caffe.proto

  3. Import and parse

The sample code below

import numpy as np
import sys, os
import argparse
import caffe_pb2 as cq

f = open('VGG_ILSVRC_16_layers.caffemodel', 'r')
cq2 = cq.NetParameter()
cq2.ParseFromString(f.read())
f.close()
print "name 1st layer: " + cq2.layers[0].name 

produces for me:

name 1st layer: conv1_1

Obviously you can extract anything else you want from your object. I just printed the name of my first layer as an example. Also, your model may be expressing the layers in either the layers array (deprecated) or the layer (no 's') array, but you get the gist.




回答2:


Nowadays, caffe can save the weights in two formats: BINARYPROTO, or HDF5. Binary weights files with extension .caffemodel are in BINARYPROTO format, while extension .caffemodel.h5 are in HDF5 format. Since the HDF5 format was introduced to caffe recently, I expect most models you currently encounter in the "model zoo" to be in the more "traditional" BINARYPROTO format.

If the weights are in stored in HDF5 format, you might be able to pick through them using h5py package.

However, the BINARYPROTO format is based on a binary serialization of google protocol buffer format that is defined by caffe.proto. I am no expert in protocol buffers, but I suspect you will have a really hard time deciphering the binary file without explicitly "compiling" the caffe.proto protobuf definition files (which is part of caffe build).

I suppose the easiest way to pick into the weights is by installing caffe and using its python/C++ interface. Why don't you just do that?




回答3:


As it so happens, ethereon made a wonderful library called caffe-tensorflow to convert caffe models to Tensorflow code, but that is not all! It also allows the user to convert .caffemodel files to .npy files without having to build pycaffe! It tests if caffe was built and if not it falls back to a pure google protobuf implementation.




回答4:


I don't understand why you want to do that without caffe/pycaffe, perhaps you are tired of deploying caffe on new machine ? But since caffemodel is special binary data type of caffe, using others' tool doesn't make life easier.

If you do insist to do this, there is another framework : Mocha on Julia, which provides a method to extracting caffemodel to hdf5. I hope this could help you.



来源:https://stackoverflow.com/questions/37572948/extracting-weights-from-caffemodel-without-caffe-installed-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!