3

I know that one can (programmably) design a network using caffe.Netspec(), and basically the main purpose is to write its prototxt.

net = caffe.NetSpec()
.. (define) ..
with open('my_network.prototoxt', 'w') as f:
    print(net.to_proto(), file=f)

However, instead of starting from scratch, I need to append layers based on a given prototxt, let's say, base.prototxt. What I want is something like

net = caffe.NetSpec()
with open('base.prototoxt, 'r') as f:          
    net.from_proto(file=f)            # <== is there something like this?
.. (append) ..
with open('my_network.prototoxt', 'w') as f:   
    print(net.to_proto(), file=f)

Could anyone please advise?

YW P Kwon
  • 2,138
  • 5
  • 23
  • 39

1 Answers1

0

You can load the model like:

model = caffe.proto.caffe_pb2.NetParameter()
input_file = open(filename, 'r')
text_format.Merge(str(input_file.read()), model)
input_file.close()
return model

And then copy layers, etc. like:

for i in range(0, len(model.layer)):
    new_model.layer.extend([model.layer[i]])

This will work also when your model / new_model is of type

model = caffe.proto.caffe_pb2.NetParameter()

HTH.

rkellerm
  • 5,362
  • 8
  • 58
  • 95