Solutions to caffe-layer creation failure

在Windows下对Caffe源代码进行重新编译之后,新建x64工程,来构建深度神经网络,主要代码:

初始化一个网络

char *proto = "data\test.prototxt";
Caffe::set_mode(Caffe::CPU);
// Caffe::set_mode(Caffe::GPU);
// Caffe::SetDevice(0);
shared_ptr<Net<float> > net;
net.reset(new Net<float>(proto, caffe::TEST));

加载已训练好的模型

char *model = "data\*.caffemodel";
net->CopyTrainedLayersFrom(model);

CHECK_EQ(net->num_inputs(), 2) << "Network should have exactly two input.";
CHECK_EQ(net->num_outputs(), 2) << "Network should have exactly two output.";

在运行的时候,在生成conv1时会出现如下问题:

Check failed: registry.count(type) == 1 (0 ys. 2) Unknown layer type: Convolution (known type: )

这里写图片描述

问了一些“专家“,说我prototxt文件写错了,但是在Linux下没问题啊。于是我google了一下得到如下回答:

回答1http://stackoverflow.com/questions/30325108/caffe-layer-creation-failure

This error occurs when trying to link caffe statically to an executable. You need to pass extra linker flags to make sure that layer registration code gets included.

然而并看不懂。。。

回答2http://dev.naver.com/projects/mydeeplearning/forum/94648

Body was changed
@@ -8,6 +8,7 @@
we need to copy layer_factory.cpp from caffe lib project and duplicate it in the executable project.
And we need to call layer initialization explicitly in the new layer_factory.cpp
(e.g. REGISTER_LAYER_CLASS(Data))
+Also original layer initializations in each layer need to be commented not to be initialized twice.

See layer_factory.cpp in pycaffe or caffe-main project.

这个回答说把src/caffe/layer_factory.cpp复制到你的工程目录。


于是我照办了,并把layer_factory.cpp这个文件单独build一下,再次运行主程序,发现错误消失,但出现了一个新错误:

Check failed: registry.count(type) == 1 (0 ys. 2) Unknown layer type: ROIPooling (known type: Convolution, Pooling, LRN, ReLU, Sigmoid, Softmax, TanH, Python)

原因是layer_factory.cpp仅仅定义了Convolution, Pooling, LRN, ReLU, Sigmoid, Softmax, TanH这些层的创建,没有定义ROIPooling,但我们可以照葫芦画瓢,把src/caffe/layers/roi_pooling_layer.cpp复制到主工程目录,编译,完成。

如果遇到其他错误,例如Dropout, INNER_PRODUCT等等,也都是同样的解决方案。

原文地址:https://www.cnblogs.com/lixuebin/p/10814843.html