当前位置: 首页 > 面试题库 >

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

郎鸿朗
2023-03-14
问题内容

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

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

附加信息:我安装了python,带有python接口的caffe。我显然对咖啡没有经验。


问题答案:

这是一个很好的函数,可以将caffe net转换为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_


 类似资料:
  • 问题内容: 我因此创建了一个数组: 我要这样做的是在512x512图像的中心显示一个红点。(至少从…开始,我想我可以从那里找出其余的内容) 问题答案: 您可以使用PIL创建(并显示)图像:

  • 问题内容: 我在与要转换为numpy数组的视频相同的文件夹中有一个python脚本。我的视频称为“ test.mp4”。 在我的脚本中,我想调用并返回一个numpy数组。所得的numpy数组应为图像的numpy数组,其中每个图像均为3-d numpy数组。 那有意义吗? 谢谢! 问题答案: 下面的脚本可以满足您的需求。您可以将它的一部分分成函数。 下面的代码不检查错误,特别是生产代码将检查每个变量

  • 我已经使用tensorflow后端训练了一个DNN,我想在FireBase中托管它。训练好的模型被保存为.meta文件,我尝试使用下面的代码将模型转换为tflite,但我遇到了一些错误。那么我如何将这个模型转换成Tensorflow Lite呢?

  • 问题内容: 假设我有; 我尝试转换; 我现在正在vstack上通过迭代来解决它,但是对于特别大的LIST来说确实很慢 您对最佳有效方法有何建议? 问题答案: 通常,您可以沿任意轴连接整个数组序列: 但你 也 必须对列表中的形状和每个阵列的维度担心(用于2维3x5的输出,你需要确保它们都是2维正由-5阵列的话)。如果要将一维数组连接为二维输出的行,则需要扩展其维数。 正如Jorge的答案所指出的那样

  • 问题内容: 我有一个RGB图像。我想将其转换为numpy数组。我做了以下 它创建一个没有形状的数组。我假设它是一个iplimage对象。 问题答案: 您可以使用较新的OpenCV python接口(如果我没记错的话,自Ope​​nCV 2.2起就可以使用)。它本机使用numpy数组: 结果:

  • 问题内容: 我有从熊猫生成的以下形式的字典列表。我想将其转换为json格式。 但是,json.dumps会引发错误:TypeError:685不可序列化JSON 我猜这是从numpy到python(?)的类型转换问题。 但是,当我使用np.int32(v)转换数组中每个字典的值v时,仍会引发错误。 编辑:这是完整的代码 问题答案: 看来您是正确的: 这里的可惜的是,numpy的数字不给你什么任何暗