Thursday, December 3, 2015

How to parse a caffe deploy.prototxt file or solver.prototxt file using python.


1) Install google's protobuf by downloading the source from https://developers.google.com/protocol-buffers/docs/downloads (I had protobuf-2.6.1.tar.gz), untarring (tar -zxvf [filename]), and following the instructions in the README

2) Get your caffe .proto file. It lives in [caffe folder]/src/caffe/proto/caffe.proto

3) Compile the caffe.proto file with protoc --python_out=. caffe.proto; it will produce caffe_pb2.py in the same directory.

4.1) For parsing a deploy.prototxt file, use:

import caffe_pb2 #you created this module with the protoc command
from google.protobuf.text_format import Merge
net = caffe_pb2.NetParameter()
Merge((open("deploy.prototxt",'r').read()), net)

4.2) For parsing a solver.prototxt file, use:

import caffe_pb2 #you created this module with the protoc command
from google.protobuf.text_format import Merge
solver = caffe_pb2.SolverParameter()
Merge((open("solver.prototxt",'r').read()), solver)

Remember, you can inspect the attributes of the object using dir(net) or dir(solver)

1 comment:

  1. Hi,

    Thanks for the post. I need one suggestion. How to read convolution_param field like kernel_size, pad, stride, axis values from deploy.prototxt.
    Thanks

    ReplyDelete