caffe:模型定义:使用caffe.NetSpec()编写具有不同相位的同一层 [英] caffe: model definition: write same layer with different phase using caffe.NetSpec()

查看:200
本文介绍了caffe:模型定义:使用caffe.NetSpec()编写具有不同相位的同一层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用caffe.NetSpec()界面使用python设置caffe cnn.尽管我看到我们可以将测试网放在solver.prototxt中,但是我想用不同的相位在model.prototxt中编写它.例如, caffe模型原型实现两个数据层,不同阶段:

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
  }
....
}

我应该如何在python中获得这种实现?

How should I do in python to get such implementation?

推荐答案

我假设您的意思是使用caffe.NetSpec编写原型时如何定义阶段?

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})

如果要在同一原型中同时包含训练层和测试层,我通常要做的是制作一个ns用于训练所有层,而另一个ns_test则仅使用重复层的测试版本.然后,在编写实际的prototxt文件时:

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())

这样,您将在同一原型中同时拥有两个阶段.我知道有点hacky.

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

这篇关于caffe:模型定义:使用caffe.NetSpec()编写具有不同相位的同一层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆