如何加载caffe模型并转换为numpy数组? [英] How do I load a caffe model and convert to a numpy array?

查看:241
本文介绍了如何加载caffe模型并转换为numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个caffemodel文件,其中包含ethereon的caffe-tensorflow转换实用程序不支持的图层.我想生成我的caffemodel的numpy表示形式.

I have a caffemodel file that contains layers that are not supported by ethereon's caffe-tensorflow conversion utility. I would like to generate a numpy representation of my caffemodel.

我的问题是,如何将caffemodel文件(如果有用,也有prototxt)转换为numpy文件?

My question is, how do I convert a caffemodel file (I also have the prototxt, if that is useful) to a numpy file?

其他信息:我安装了python,带有python接口的caffe.我显然对咖啡没有经验.

Additional info: I have python, caffe with python interfaces etc installed. I am clearly not experienced with caffe.

推荐答案

这是一个很好的函数,可以将caffe net转换为python词典列表,因此您可以按需要对其进行腌制和读取:

Here's a nice function that converts a caffe net to a python list of dictionaries, so you can pickle it and read it anyway you want:

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_

这篇关于如何加载caffe模型并转换为numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆