How do you export .caffemodels to other applications?

2.5k views Asked by At

Is it possible to translate the info in a .caffemodel file such that it could be read by (for example) Matlab. That is, is there a way to write your model using something else that prototxt and import the weights trained using Caffe?

If the answer is "Nope, it's a binary file and will always remain that way", is there some documentation regarding the structure of the file so that one could extract the important information somehow?

2

There are 2 answers

3
Ghassem Tofighi On BEST ANSWER

As you know, .caffemodel consists of weights and biases. A simple way to read weights and biases for a caffemodel given the prototxt would be to just load the network in Python and read the weights.

You can use:

import caffe
net = caffe.Net(<prototxt-file>,<model-file>,<phase>);

and access the params from net.params

source

0
yihui.dev On

I'll take VGG as an example

from caffe.proto import caffe_pb2
net = caffe_pb2.NetParameter()
caffemodel = sys.argv[1]
with open(caffemodel, 'rb') as f:
    net.ParseFromString(f.read())
for i in net.layer:
    print i.ListFields()[0][-1]
#conv1
#relu1
#norm1
#pool1
#conv2
#relu2
#norm2
#pool2
#conv3
#relu3
#conv4
#relu4
#conv5
#relu5
#pool5
#fc6
#relu6
#drop6
#fc7
#relu7
#drop7
#fc8
#prob