这里咱们用一个例子先来体验一下python
我使用的控制台是 Windows PowerShellshell
$env:PATHPATH="F:\caffe-python\python\;F:\caffe-windows\windows\install\python"
这里F:\caffe-python\python
是个人新Layer的路径F:\caffe-windows\windows\install\python
是个人Caffe编译之后install的路径windows
import caffe import numpy as np class TestLayer(caffe.Layer): def setup(self, bottom, top): if len(bottom) != 1: raise Exception("Need two inputs to compute distance.") def reshape(self, bottom, top): print("-----------------1---------------------") top[0].reshape(1) def forward(self, bottom, top): top[0].data[...] = bottom[0].data print("-----------------2---------------------") def backward(self, top, propagate_down, bottom): bottom[...].data=top[0].data pass
import caffe import numpy as np class EuclideanLossLayer(caffe.Layer): def setup(self, bottom, top): # 输入检查 if len(bottom) != 2: raise Exception("Need two inputs to compute distance.") def reshape(self, bottom, top): # 输入检查 if bottom[0].count != bottom[1].count: raise Exception("Inputs must have the same dimension.") # 初始化梯度差分zeros_like函数的意义是建立一个与参数等大小的全0矩阵 self.diff = np.zeros_like(bottom[0].data, dtype=np.float32) # loss 输出(loss是一个标量) top[0].reshape(1) #前向传播(计算loss bottom[0].data是第一个输入 bottom[1].data是第二个输入) #注意:前向传播是输出top def forward(self, bottom, top): self.diff[...] = bottom[0].data - bottom[1].data top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2. #后向传播 #注意:前向传播是输出到bottom def backward(self, top, propagate_down, bottom): for i in range(2): if not propagate_down[i]: continue if i == 0: sign = 1 else: sign = -1 #偏差向后扩散 bottom[i].diff[...] = sign * self.diff / bottom[i].num
name: "TEST" layer { name: "cifar" type: "Data" top: "data" top: "label" include { phase: TRAIN } transform_param { mean_file: "examples/cifar10/Release/cifar10/mean.binaryproto" } data_param { source: "examples/cifar10/Release/cifar10/cifar10_train_lmdb" batch_size: 100 backend: LMDB } } layer { name: "cifar" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param { mean_file: "examples/cifar10/Release/cifar10/mean.binaryproto" } data_param { source: "examples/cifar10/Release/cifar10/cifar10_test_lmdb" batch_size: 100 backend: LMDB } } layer { name: "test1" type: "Python" bottom: "data" top: "test1" python_param { module: "test_layer" layer: "Test_Layer" } }
可视化咱们的网络结构之后如图
网络
net: "F:/caffe-python/python/test_layer.prototxt" base_lr: 0.001 lr_policy: "fixed" max_iter: 10 solver_mode: CPU
接下来在powershell里面去启动caffe
先cd到caffe所在的目录
个人目录是这样的ide
cd F:\Smart_Classroom\3rdparty\ALLPLATHFORM\caffe-windows\windows\examples\cifar10\Release
而后执行caffe函数
./caffe.exe train --solver=F:/caffe-python/python/test_python_layer_solver.prototxt
以下图:
在后向和前向传播的过程当中咱们成功的调用了两个print
至此,编写本身的Caffe层就成功了idea
PS: 编写的时候严格注意路径不然会出现如下报错
ssr