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 write it in model.prototxt
with different phase. For example, caffe model prototxt implement two data layer with different phases:
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
....
}
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
....
}
How should I do in python to get such implementation?
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.
I find an useful method.
You can add a key named name
for your test phase layer, and modify the keys ntop
and top
just like this:
net.data = L.Data(name='data',
include=dict(phase=caffe_pb2.Phase.Value('TRAIN')),
ntop=1)
net.test_data = L.Data(name='data',
include=dict(phase=caffe_pb2.Phase.Value('TEST')),
top='data',
ntop=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With