从Python中的caffe .prototxt模型定义中读取网络参数

时间:2017-09-07 15:09:59

标签: python protocol-buffers caffe pycaffe

我想从Python中.prototxt中定义的caffe网络中读出网络参数,因为layer_dict中的图层对象只告诉我,例如它是一个“卷积”层,但不是kernel_size文件中明确定义的strides.prototxt等内容。

所以我要说model.prototxt就像这样:

name: "Model"
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param {
    shape: {
      dim: 64
      dim: 1
      dim: 28
      dim: 28
    }
  }
}
layer {
  name: "conv2d_1"
  type: "Convolution"
  bottom: "data"
  top: "conv2d_1"
  convolution_param {
    num_output: 32
    kernel_size: 3
    stride: 1
    weight_filler {
      type: "gaussian" # initialize the filters from a Gaussian
      std: 0.01        # distribution with stdev 0.01 (default mean: 0)
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

layer {
  name: "dense_1"
  type: "InnerProduct"
  bottom: "conv2d_1"
  top: "out"
  inner_product_param {
    num_output: 1024
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

我发现可以像这样解析模型:

from caffe.proto import caffe_pb2
import google.protobuf.text_format
net = caffe_pb2.NetParameter()
f = open('model.prototxt', 'r')
net = google.protobuf.text_format.Merge(str(f.read()), net)
f.close()

但我不知道如何从protobuf消息中获取结果对象中的字段。

2 个答案:

答案 0 :(得分:3)

您可以遍历这些图层并询问相应的参数,例如:

for i in range(0, len(net.layer)):
    if net.layer[i].type == 'Convolution':
        net.layer[i].convolution_param.bias_term = True # bias term, for example

可以在caffe.proto中找到相应的* _param类型,例如:

optional ConvolutionParameter convolution_param = 106

答案 1 :(得分:0)

Caffe prototxt文件是基于Google Protobuf构建的。为了有问题地访问它们,您需要使用该软件包。这是一个示例脚本(source):

from caffe.proto import caffe_pb2
import google.protobuf.text_format as txtf

net = caffe_pb2.NetParameter()

fn = '/tmp/net.prototxt'
with open(fn) as f:
    s = f.read()
    txtf.Merge(s, net)

net.name = 'my new net'
layerNames = [l.name for l in net.layer]
idx = layerNames.index('fc6')
l = net.layer[idx]
l.param[0].lr_mult = 1.3

outFn = '/tmp/newNet.prototxt'
print 'writing', outFn
with open(outFn, 'w') as f:
    f.write(str(net))