国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Caffe python layer 的自定義

2019-11-08 03:13:04
字體:
來源:轉載
供稿:網友

還是caffe的自定義層問題。相比于c,python的自定義層更為簡單:代碼少、外部文件少、方便執行。因此用這種方法實現有利于開發和實驗。


準備

首先還是要記得在編譯的時候加上WITH_PYTHON_LAYER的選項,如果沒有加可以先make clean刪除編譯后的文件,再重新編譯。

WITH_PYTHON_LAYER=1 make && make pycaffe

如果出現

layer_factory.hpp:77] Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: Python

這樣的錯誤,說明這一步沒有成功。


net中的添加

在caffe的配置net里我們要輸入有關層的信息,下面以EuclideanLossLayer層為例。簡單來說我們可以直接使用,因為原本caffe里面已經包括了用c編寫的代碼,現在我們把它改編為python層。

layer { type: 'Python' name: 'loss' top: 'loss' bottom: 'python的層文件需要在$PYTHONPATH目錄下。在PRototxt中,模塊名是pyloss,這意味著你的包括EuclideanLossLayer類名的py文件名也應該是pyloss.py


layer文件添加

# pyloss.pyimport caffeimport numpy as npclass EuclideanLossLayer(caffe.Layer): def setup(self, bottom, top): # check input pair if len(bottom) != 2: raise Exception("Need two inputs to compute distance.") def reshape(self, bottom, top): # check input dimensions match if bottom[0].count != bottom[1].count: raise Exception("Inputs must have the same dimension.") # difference is shape of inputs self.diff = np.zeros_like(bottom[0].data, dtype=np.float32) # loss output is scalar top[0].reshape(1) 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. 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

總結

經測試文件應該沒問題,可以讀取運行。不過沒有現成使用EuclideanLossLayer的網絡,最后計算結果沒有驗證。

Caffe Python Layer


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大足县| 阿拉尔市| 三台县| 宁波市| 凤冈县| 淳安县| 富源县| 邓州市| 丰宁| 邵东县| 甘德县| 宜昌市| 墨脱县| 昭苏县| 天峨县| 河南省| 霞浦县| 仪征市| 屯留县| 仁怀市| 涞水县| 天门市| 武清区| 中牟县| 鹤山市| 精河县| 千阳县| 浦北县| 贡嘎县| 禄劝| 密云县| 正镶白旗| 烟台市| 杭锦旗| 锦屏县| 叙永县| 甘谷县| 象山县| 绥中县| 平江县| 盈江县|