Solutions to caffe-layer creation failure

本文介绍了解决Caffe在Windows环境下编译及运行时遇到的问题,特别是关于未知层类型错误的处理方法。通过复制特定的cpp文件到项目中,并确保所有使用的层类型都被正确注册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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等等,也都是同样的解决方案。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值