caffe添加loss層

1 寫好要添加的層的hpp cpp cu文件,cu文件非必需,看是否需要cuda加速
2 hpp文件放在include/caffe/layers文件夾下
3 cpp和cu放在src/caffe/layers文件夾下
4根據添加的層是否包含參數,更改caffe/src/proto下的caffe.proto
需要添加參數,則找到

// LayerParameter next available layer-specific ID: 152 (last added: box_annotator_ohem_param)

message LayerParameter {
  optional string name = 1; // the layer name
  optional string type = 2; // the layer type
  repeated string bottom = 3; // the name of each bottom blob
  repeated string top = 4; // the name of each top blob

// LayerParameter next available layer-specific ID: 152 (last added: box_annotator_ohem_param)

這一句表示下一個新添加的參數的ID應該從152開始,避免與之前的ID重複。
自己在下面添加新的層需要的參數就可以了

optional NEWLAYERParameter  NWELAYER_param = 152

然後再在message AllPassParameter { }中指定具體的該層的參數名稱及default值

如果新加的層不含參數,如relu層,則不需要改proto文件

5 編譯(important)
這裏用的microsoft版本的caffe,使用vs2013編譯,和cmake編譯的不一樣,也是出錯的根本原因

按要求在文件夾中添加了三個文件以後,再編譯vs順利通過,但是再調用新層的時候還是報錯

Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: 

還是找不到該層
原因是在vs編譯時,沒有將新加的三個文件添加到工程中
在這裏插入圖片描述
打開libcaffe的小三角,看到cu/layers下面有一排的cu文件,裏面沒有你新加的那個層的對應文件的話,需要手動添加進來,兩種方法,直接copy或者右鍵添加
同理還有include/layers和src/layers 分別添加hpp和cpp文件
再次編譯,成功,訓練中調用新層,成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章