Loading from caffemodel File - Python

1,870 views
Skip to first unread message

David Mascharka

unread,
Jun 7, 2016, 4:30:54 PM6/7/16
to Caffe Users
I'm having some trouble loading a caffemodel in python. I'm just trying to get access to the network right now so I've compiled the protobuf file to caffe_pb2. I'm using the caffemodel available from here for this. Here's what I've got:

import caffe_pb2

model
= open('yu-koltun-net.caffemodel', 'rb')
binary_content
= model.read()

protobuf
= caffe_pb2.NetParameter()
protobuf
.ParseFromString(binary_content)

layers
= protobuf.layers

# now see if there are any layers
print len(layers) # outputs 0

How are there no layers? Is this the wrong way to read a caffemodel file? I know I can do

import caffe

net
= caffe.Net('yu-koltun-net.prototxt', 'yu-koltun-net.caffemodel', caffe.TEST)

but I just want to access some information about the layers.

David Mascharka

unread,
Jun 8, 2016, 10:18:40 AM6/8/16
to Caffe Users
Ah, I was using probobuf.layers which is deprecated. I should be using protobuf.layer and now it works.

sk06

unread,
Apr 27, 2017, 5:30:55 AM4/27/17
to Caffe Users
Hi,

I want to read convolution_param specs like kernel_size, pad, stride, axis info of a convolution layer directly from caffemodel file. Please help me with some suggestions.

Thanks
SK06
Message has been deleted

Ankit Singh

unread,
Mar 13, 2019, 8:36:05 AM3/13/19
to Caffe Users
@sk06, You could use the convolution_param from layer to get the specs (similarly how you would do with a prototxt file),

from caffe.proto.caffe_pb2 import NetParameter

with open('path_to_caffemodel.caffemodel', 'rb') as fp:
    net 
= NetParameter()
    net
.ParseFromString(fp.read())

for idx, layer in enumerate(net.layer):
    
if(layer.type == 'Convolution'):
        conv_param 
= layer.convolution_param
        print(idx, layer.name)
        
print(conv_param.kernel_size)
        print(conv_param.pad)
        print(conv_param.stride)
        
print(conv_param.axis)
Reply all
Reply to author
Forward
0 new messages