How to reuse same network twice within a new network in CAFFE

泪湿孤枕 提交于 2020-01-13 13:10:14

问题


I have a pretrained network (let's call it N) I would like to use twice within a new network. Anybody knows how to duplicate it? Then I would like to assign a different learning rate to each copy.

For example (N1 is the 1st copy of N, N2 is the 2nd copy of N), the new network might look like:

N1 --> [joint ip 
N2 -->    layer]

I know how to reuse N with a single copy, however, since N1 and N2 will have different (finetune) learning rates, I don't know how can I make 2 copies of N and assign different learning rate for each.

Thanks!


回答1:


Using the same net twice is something called "Siamese network". The way it is implemented in caffe is by explicitly duplicating the network, but using "name" param for each parameters blob to create a single copy of the underlying parameters. See this prototxt for example.
Once you explicitly defeine the net twice, you can assign different "lr_mult" params for each copy.

So suppose your reference network N has an input layer (which I'll skip in this example) and an inner product layer named "ip1". Then

 layer {
   name: "ip1_a"
   bottom: "data_a"
   top: "ip1_a"
   type: "InnerProduct"
   inner_product_param {
     num_output: 10
   }
   param {
     name: "ip1_w"  # NOTE THIS NAME!
     lr_mult: 1
   }
   param {
     name: "ip1_b"
     lr_mult: 2
   }
 }
 layer {
   name: "ip1_b"
   bottom: "data_b"
   top: "ip1_b"
   type: "InnerProduct"
   inner_product_param {
     num_output: 10
   }
   param {
     name: "ip1_w"  # NOTE THIS NAME: it's the same!
     lr_mult: 10 # different LR for this branch
   }
   param {
     name: "ip1_b"
     lr_mult: 20
   }
 }
 # one layer to combine them     
 layer {
   type: "Concat"
   bottom: "ip1_a"
   bottom: "ip1_b"
   top: "ip1_combine"
   name: "concat"
 }
 layer {
   name: "joint_ip"
   type: "InnerProduct"
   bottom: "ip1_combine"
   top: "joint_ip"
   inner_product_param {
     num_output: 30
   }
 } 

If you finetune, you might need to do some net-surgery in order of the original wieghts to be saved in the .caffemodel file with the names "ip1_w" and "ip1_b".



来源:https://stackoverflow.com/questions/33983627/how-to-reuse-same-network-twice-within-a-new-network-in-caffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!