How to load caffe model and convert to numpy array?
I have a caffemodel file that contains layers that are not supported by ethereon's caffe-tensorflow transform utility. I would like to create a numpy view of my caffemodel.
My question is, how do I convert the caffemodel file (I also have a prototype, if useful) to a numpy file?
More info: I have python, caffe with python interfaces, etc. I obviously don't taste with coffee.
+3
source to share
1 answer
Here's a nice function that converts the caffe net into a list of python dictionaries, so you can clean it up and read it anyway:
import caffe
def shai_net_to_py_readable(prototxt_filename, caffemodel_filename):
net = caffe.Net(prototxt_filename, caffemodel_filename, caffe.TEST) # read the net + weights
pynet_ = []
for li in xrange(len(net.layers)): # for each layer in the net
layer = {} # store layer information
layer['name'] = net._layer_names[li]
# for each input to the layer (aka "bottom") store its name and shape
layer['bottoms'] = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape)
for bi in list(net._bottom_ids(li))]
# for each output of the layer (aka "top") store its name and shape
layer['tops'] = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape)
for bi in list(net._top_ids(li))]
layer['type'] = net.layers[li].type # type of the layer
# the internal parameters of the layer. not all layers has weights.
layer['weights'] = [net.layers[li].blobs[bi].data[...]
for bi in xrange(len(net.layers[li].blobs))]
pynet_.append(layer)
return pynet_
+6
source to share