3

I would like to read out the network parameters from a caffe network defined in a .prototxt in Python, as the layer object in the layer_dict only tell me e.g. that its a "Convolution" layer, but not things like kernel_size, strides etc. that are well defined in the .prototxt file.

So lets say I have a model.prototxt like so:

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
    }
  }
}

I have found that one can parse the model like so:

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()

but I have no idea how to get the fields from the protobuf message out of the resulting object.

Harsh Wardhan
  • 2,110
  • 10
  • 36
  • 51
Jarno
  • 6,243
  • 3
  • 42
  • 57

2 Answers2

5

You can iterate through the layers and ask about their corresponding param, e.g.:

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

The appropriate *_param type can be found in caffe.proto, for example:

optional ConvolutionParameter convolution_param = 106
rkellerm
  • 5,362
  • 8
  • 58
  • 95
0

Caffe prototxt files are built on Google Protobuf. In order to problematically access them, you need to use that package. Here is an example script (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))
DankMasterDan
  • 1,900
  • 4
  • 23
  • 35