如何加载caffe模型并转换为numpy数组?

7
我有一个caffemodel文件,其中包含ethereon的caffe-tensorflow转换工具不支持的层。我想生成我的caffemodel的numpy表示。
我的问题是:如何将caffemodel文件(如果有用的话,我也有prototxt)转换为numpy文件?
其他信息:我已经安装了Python、带有Python接口的Caffe等。我显然对Caffe不熟悉。
1个回答

12
以下是一个很好的函数,它可以将一个caffe网络转换为Python字典列表,因此您可以对其进行序列化并以任何您想要的方式进行读取:
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's 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_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接