Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 221
user3162707 Avatar asked Apr 25 '16 15:04

user3162707


2 Answers

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.

like image 77
Shai Avatar answered Jan 04 '23 04:01

Shai


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)
like image 43
Rolrence Avatar answered Jan 04 '23 06:01

Rolrence