caffe: model definition: write same layer with different phase using caffe.NetSpec()

前端 未结 5 710
野性不改
野性不改 2020-12-03 23:00

I want to set up a caffe CNN with python, using caffe.NetSpec() interface. Although I saw we can put test net in solver.prototxt, I would like to w

5条回答
  •  不思量自难忘°
    2020-12-03 23:41

    I assume you mean how to define phase when writing a prototxt using caffe.NetSpec?

    from caffe import layers as L, params as P, to_proto
    import caffe
    
    ns = caffe.NetSpec()
    ns.data = L.Data(name="data", 
                     data_param={'source':'/path/to/lmdb','batch_size':32},
                     include={'phase':caffe.TEST})
    

    If you want to have BOTH train and test layers in the same prototxt, what I usually do is making one ns for train with ALL layers and another ns_test with only the test version of the duplicate layers only. Then, when writing the actual prototxt file:

    with open('model.prototxt', 'w') as W:
      W.write('%s\n' % ns_test.to_proto())
      W.write('%s\n' % ns.to_proto())
    

    This way you'll have BOTH phases in the same prototxt. A bit hacky, I know.

提交回复
热议问题