【问题标题】:How do I load a caffe model and convert to a numpy array?如何加载 caffe 模型并转换为 numpy 数组?
【发布时间】:2017-12-25 06:16:27
【问题描述】:

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

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

附加信息:我安装了 python、caffe 和 python 接口等。我显然对咖啡没有经验。

【问题讨论】:

    标签: python numpy neural-network deep-learning caffe


    【解决方案1】:

    这是一个不错的函数,可以将 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_
    

    【讨论】:

      猜你喜欢
      • 2018-05-21
      • 2017-11-23
      • 2017-10-12
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 2021-12-18
      相关资源
      最近更新 更多