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

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

问题描述

我想用python建立一个caffe CNN,使用caffe.NetSpec()接口.虽然我看到我们可以将测试网络放在solver.prototxt中,但我想把它写在不同阶段的model.prototxt中.例如,caffe model prototxt 实现了两个数据层不同阶段:

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 编写 prototxt 时如何定义阶段?

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

如果你想在同一个 prototxt 中同时拥有训练和测试层,我通常做的是制作一个 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
' % ns_test.to_proto())
  W.write('%s
' % ns.to_proto())

这样,您将在同一个 prototxt 中拥有两个阶段.有点老套,我知道.

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

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

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