Caffe(二)-Python-自定義網絡層

這裏咱們用一個例子先來體驗一下python

  • 首先定義一下咱們的環境變量 $PYTHONPATH,我這兒是Windows開發環境,至於Windows Caffe怎麼編譯由讀者本身下去搞定

我使用的控制檯是 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

編寫本身的TestLayer

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

編寫完咱們的ayers之後寫出網絡結構

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

可視化咱們的網絡結構之後如圖
網絡

編寫solver

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

相關文章
相關標籤/搜索