0

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?

V.Vocor
  • 429
  • 1
  • 5
  • 16

2 Answers2

2

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

Ghassem Tofighi
  • 381
  • 4
  • 16
  • I see. Is it only through that interface you can do it? Pycaffe isn't working on my unit. It can't find Google protobuf. – V.Vocor Feb 17 '16 at 20:00
  • 1
    I believe this is the easiest way to do it. Try to research about why Pycaffe is not working (https://groups.google.com/forum/#!topic/caffe-users/9Q10WkpCGxs). – Ghassem Tofighi Feb 17 '16 at 20:03
  • By god Ghassem, you solved both my problems! You are the best. – V.Vocor Feb 17 '16 at 20:25
0

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
yihui.dev
  • 602
  • 8
  • 10