caffe: model definition: write same layer with different phase using python

Created on 25 Apr 2016  路  8Comments  路  Source: BVLC/caffe

I want to set up a caffe CNN with python. Although I saw we can put test net in solver.prototxt, I would like to write it in model.prototxt with different phase using caffe.NetSpec(). 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 to get two layers with same name, but different phases?

Python

Most helpful comment

I find the solution proposed in this SO answer to be quite nice and elegant. I believe this solution may close this issue.

Here's the proposed workaround (using top and ntop arguments):

from caffe import layers as L, params as P, to_proto
import caffe

ns = caffe.NetSpec()
ns.data = L.Data(name="data", 
                 include={'phase':caffe.TEST})
ns.test_data = L.Data(name="data", ntop = 0, top='data',
                 include={'phase':caffe.TEST})
print '{}'.format(ns.to_proto())

Results with

layer {
name: "data"
type: "Data"
top: "data"
include {
phase: TRAIN
}
}
layer {
name: "data"
type: "Data"
top: "data"
include {
phase: TEST
}
}

All 8 comments

related stackoverflow thread: this and this

I want to do exactly the same thing but haven't figured out how yet. You can pass name= and top= arguments when creating the layers (which must have different names in Python), but then I don't know how to make the next layer take, as input (bottom), those layers under their name= name (NOT their Python name).

@raaaaaymond there is a workaround, you can find it here.

Ah yes. I didn't fully understand your answer there the first time I read it. I do now. Thank you. Still the solution is a little bit hacky (as you pointed out). But luckily in my case it does solve my problem.

I see this issue was raised also here. Any chance this will be addressed soon?

I find the solution proposed in this SO answer to be quite nice and elegant. I believe this solution may close this issue.

Here's the proposed workaround (using top and ntop arguments):

from caffe import layers as L, params as P, to_proto
import caffe

ns = caffe.NetSpec()
ns.data = L.Data(name="data", 
                 include={'phase':caffe.TEST})
ns.test_data = L.Data(name="data", ntop = 0, top='data',
                 include={'phase':caffe.TEST})
print '{}'.format(ns.to_proto())

Results with

layer {
name: "data"
type: "Data"
top: "data"
include {
phase: TRAIN
}
}
layer {
name: "data"
type: "Data"
top: "data"
include {
phase: TEST
}
}

what if I want to write this?
layer{
name: "m3@ssh_output_ohem"
type: "Concat"
bottom: "m3@ssh_3x3_output_ohem"
bottom: "m3@ssh_5x5_output_ohem"
bottom: "m3@ssh_7x7_output_ohem"
top: "m3@ssh_output_ohem"
concat_param{
axis: 1
}
propagate_down: false
propagate_down: false
propagate_down: false
}
There are three# propagate_down: false, and python can not receive the duplicate args.
How to write in python?

I solved the problem by using propagate_down=[False, False, False]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

malreddysid picture malreddysid  路  3Comments

shiorioxy picture shiorioxy  路  3Comments

lixin7895123 picture lixin7895123  路  3Comments

OpenHero picture OpenHero  路  3Comments

inferrna picture inferrna  路  3Comments