利用程序写caffe协议:同名层

本文介绍了使用 Caffe 的 Python API 进行网络定义的方法,包括数据层的配置、卷积层参数设置及 Reshape 层的应用。通过具体示例展示了如何避免层名冲突和正确书写网络结构。

利用程序写协议,最大的困难在于:同名层的书写:

# coding=gbk
from caffe import layers as L, params as P
import caffe

ns = caffe.NetSpec()
ns.Features,ns.Headposes,ns.Smiles = L.Data(name="data", ntop=3,
                 include={'phase':caffe.TRAIN})
ns.test_data = L.Data(name="data", ntop = 0, top=['Features','Headposes','Smiles'],
                 include={'phase':caffe.TEST})

print '{}'.format(ns.to_proto())

输出:

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

黏在一起的书写:

n.relu1= L.ReLU(n.conv1,ntop = 0,top='conv1')
n.norm1 = L.LRN(n.conv1,local_size=5,alpha=0.0001,beta=0.75)

结果:

这里写图片描述

code:

n.conv1 = L.Convolution(n.Features, kernel_size=11,stride=4, num_output=96, 
                            param=[dict(lr_mult=1,decay_mult=1), dict(lr_mult=2,decay_mult=0)],
                            weight_filler=dict(type='gaussian',std= 0.01),
                            bias_filler=dict(type='constant',value= 0))

output:

layer {
  name: "conv1"
  type: "Convolution"
  bottom: "Features"
  top: "conv1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

Reshape定义在Python中:

n.resh = L.Reshape(n.fc3, reshape_param={'shape':{'dim': [1, 1, 64, 64]}})

Note that the shape vector [1, 1, 64, 64] is passed as a list and not as a string like in the prototxt syntax.

In fact, any entry defined as repeated in caffe.proto, should be considered as a list/vector when interfacing using NetSpec.

参考网址:
https://stackoverflow.com/questions/38480599/how-to-reshape-layer-in-caffe-with-python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值