Extracting net info from *.caffemodel

2,595 views
Skip to first unread message

Yet-another-newbie

unread,
Dec 14, 2016, 8:21:22 PM12/14/16
to Caffe Users
I'm working on an application that uses Caffe in Python. I have imported net info using
import caffe
net = caffe.Net('aaaa.prototxt', 'bbb.caffemodel', caffe.TEST)
 
I could extract the net layer names, weights and biases using the examples given here: http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb

May I know how I can extract following information from 'net' variable?
  • Type of each layer (CONVOLUTION, DECONVOLUTION, POOLING, LRN, RELU, INNER_PRODUCT, SOFTMAX, DROPOUT, etc)
  • Learning parameters
  • Convolution parameters
  • Pooling parameters
  • etc...
Is there any way to identify deconvolution layers? (If the type of the layer can be extracted from 'net', that's possible. But I don't have any idea how to do that.)
Is there any proper documentation about the structure of the 'net' variable?

Przemek D

unread,
Dec 15, 2016, 4:30:17 AM12/15/16
to Caffe Users
I know you can do:
print net.layers[0].type
and it returns a string containing the layer type. I don't know what is further than that. I used to think layers contains a vector of type LayerParameter, but fields like name or bottom do not exist.
Signing up for this thread as I'd like to know more too.

Yet-another-newbie

unread,
Dec 20, 2016, 8:47:56 PM12/20/16
to Caffe Users
Thanks, but there should be a way to read the layer name and type together. Just don't know how to....

Michele Pratusevich

unread,
Dec 22, 2016, 10:24:31 AM12/22/16
to Caffe Users
The weights you can get from making an auxiliary function like this:

def blobproto_to_array(blob):
    data = np.array(blob.data)
    return data.reshape(blob.num, blob.channels, blob.height, blob.width)

and then for a given layer (that you already have the name of)

layer_name = 'conv1'
weights = blobproto_to_array(net.params[layer_name][0])

and if there are biases,

biases = blobproto_to_array(net.params[layer_name][1])

Michele Pratusevich

unread,
Dec 23, 2016, 3:46:11 PM12/23/16
to Caffe Users
I was curious about your question so I dug in a little bit and found the following:
* the layer names come from net._layer_names
* the layer types come from net.layers[index].type

So you can get both pretty easily:

for layer_name, layer in zip(net._layer_names, net.layers):
    print layer_name, " type: ", layer.type

Hope that helps!

Yet-another-newbie

unread,
Dec 28, 2016, 3:32:36 AM12/28/16
to Caffe Users
Thanks for your reply.

Now I have another problem. This time I don't have a '.prototxt' file, but only a '.caffemodel' file. As per my understanding, I cannot call caffe.Net() without a '.prototxt'. Do you have any idea how to get net info from '.caffemodel' file?


Yet-another-newbie

unread,
Jan 2, 2017, 10:49:41 PM1/2/17
to Caffe Users
I found a solution. Here it is:

import caffe_pb2

model = open('model.caffemodel', 'rb')
net_param = caffe_pb2.NetParameter()
net_param.ParseFromString(model.read())

for layer in range(0, len(net_param.layer)):
    print net_param.layer[layer].name # layer name
    print net_param.layer[layer].type # layer type
    for blob in range(0, len(net_param.layer[layer].blobs)):
        print net_param.layer[layer].blobs[blob].data #weights and biases

Likewise, you can import any data from caffemodel file. Try 'print net_param' to see all available data.

Przemek D

unread,
Jan 3, 2017, 6:37:25 AM1/3/17
to Caffe Users
Didn't work for me at all due to a weird bug:

len(net_param.layer)

returned zero, while

len(net_param.layers)

contained the list of layers as expected. Changed layer to layers in the whole script and it worked. I had to suppress printing "blobs[blob].data" directly, because it outputs all model weights to the terminal, flooding it. Is there any way to print blob shapes? blobs[blob].data has no field named shape. Nice find though!

Yet-another-newbie

unread,
Jan 3, 2017, 7:57:51 PM1/3/17
to Caffe Users
In my case, blobs[blob].data HAS a field named shape. Seems our caffemodels are generated with different versions of caffe(?). Try 'print net_param' to see all available data.
Reply all
Reply to author
Forward
0 new messages