ShuffleNet在Caffe框架下的實現
本文是在實現Github上使用者farmingyard貼出的加速版ShuffleNet。下面是所包含的檔案:
作為一個深度學習方面的小白,剛開始真的是一頭霧水,在之前的Caffe框架使用中,只是單純的會把別人的deploy.prototxt,train.prototxt,solver.prototxt拿來用,製作資料集跑一跑,稍微有一點變化,例如有的網路github上只給一個deploy.prototxt檔案,或者是像這樣還給出 .cpp .cu .hpp檔案README.md又是這樣的:
在寫出其他兩個檔案以後直接巡行會出現以下問題:
I1017 22:40:08.742588 8634 solver.cpp:87] Creating training net from net file: /home/t702/zrx/ShuffleNet/train_val.prototxt
[libprotobuf ERROR google/protobuf/text_format.cc:245] Error parsing text-format caffe.NetParameter: 347:25: Message type "caffe.LayerParameter" has no field named "shuffle_channel_param".
F1017 22:40:08.742987 8634 upgrade_proto.cpp:88] Check failed: ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter file: /home/t702/zrx/ShuffleNet/train_val.prototxt
*** Check failure stack trace: ***
@ 0x7f3407747daa (unknown)
@ 0x7f3407747ce4 (unknown)
@ 0x7f34077476e6 (unknown)
@ 0x7f340774a687 (unknown)
@ 0x7f3407e7608e caffe::ReadNetParamsFromTextFileOrDie()
@ 0x7f3407e47dcc caffe::Solver<>::InitTrainNet()
@ 0x7f3407e48c83 caffe::Solver<>::Init()
@ 0x7f3407e48f5f caffe::Solver<>::Solver()
@ 0x7f3407ea5e91 caffe::Creator_SGDSolver<>()
@ 0x40ee6e caffe::SolverRegistry<>::CreateSolver()
@ 0x407efd train()
@ 0x40590c main
@ 0x7f3406753f45 (unknown)
@ 0x40617b (unknown)
@ (nil) (unknown)
下面主要就是針對這一問題進行解決。
1.相關檔案的使用問題
如果只給出了一個deploy.prototxt檔案,另外兩個.prototxt配置檔案都是需要我們使用者根據自己的情況和所給出的deploy.prototxt自己寫的,詢問過一個給出Caffe框架模型的原作者,大神說這是Caffe框架最基本的東西了。T T
然後像這樣給出
shuffle_channel_layer.cpp
shuffle_channel_layer.cu
shuffle_channel_layer.hpp
三個檔案的,基本都是網路結構中自己定義的layer,是Caffe框架本身沒有的,需要我們新增到Caffe框架下。
以ShuffleNet為例,具體使用步驟是將:
shuffle_channel_layer.cpp
shuffle_channel_layer.cu
放進caffe/src/caffe/layers路徑下,而
shuffle_channel_layer.hpp
放進caffe/include/caffe/layers路徑下。 然後類似於函式的宣告一樣,我們需要在caffe/src/caffe/proto/caffe.proto檔案中找到message LayerParameter{…}這條語句
並在其中新增語句:
message LayerParameter {
...
optional ShuffleChannelParameter shuffle_channel_param = 164;
...
}
...
message ShuffleChannelParameter {
optional uint32 group = 1[default = 1]; // The number of group
}
做完上述步驟後,在caffe需要重新編譯,也很簡單,直接在caffe資料夾下開啟命令列:
make all -j4
在執行caffe訓練時就可以使用 type: ShuffleChannel1 的layer了。
2.開始訓練
然後就是準備好資料集,根據deploy.prototxt檔案編寫好train.prototxt和solver.prototxt 就可以開始訓練了!
Ps:
github還有一個版本的ShuffleNet中直接使用會出現如下報錯:
I1017 22:42:00.554802 8701 solver.cpp:87] Creating training net from net file: /home/t702/zrx/ShuffleNet/train.prototxt
[libprotobuf ERROR google/protobuf/text_format.cc:245] Error parsing text-format caffe.NetParameter: 145:17: Message type "caffe.LayerParameter" has no field named "permute_param".
F1017 22:42:00.555064 8701 upgrade_proto.cpp:88] Check failed: ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter file: /home/t702/zrx/ShuffleNet/train.prototxt
*** Check failure stack trace: ***
@ 0x7f4ef019cdaa (unknown)
@ 0x7f4ef019cce4 (unknown)
@ 0x7f4ef019c6e6 (unknown)
@ 0x7f4ef019f687 (unknown)
@ 0x7f4ef08cb08e caffe::ReadNetParamsFromTextFileOrDie()
@ 0x7f4ef089cdcc caffe::Solver<>::InitTrainNet()
@ 0x7f4ef089dc83 caffe::Solver<>::Init()
@ 0x7f4ef089df5f caffe::Solver<>::Solver()
@ 0x7f4ef08fae91 caffe::Creator_SGDSolver<>()
@ 0x40ee6e caffe::SolverRegistry<>::CreateSolver()
@ 0x407efd train()
@ 0x40590c main
@ 0x7f4eef1a8f45 (unknown)
@ 0x40617b (unknown)
@ (nil) (unknown)
具體解決方法同上!