caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.

本文介绍了解决Caffe编译过程中出现的与OpenCV图像读取和编码相关的链接错误的方法。通过修改Makefile文件并添加正确的OpenCV库依赖项,可以成功编译Caffe。

caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.

come from http://www.cnblogs.com/wangxiaocvpr/p/5200837.html


when I compile caffe file :

.build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
.build_debug/lib/make: libcaffe.so: undefined *** [.build_debug/tools/upgrade_net_proto_text.bin] Error 1
referencemake: *** Waiting for unfinished jobs....
 to `cv.build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::/_InputArray const&, usrint)/bin/'ld: 
warning: libcudart.so.5.5, needed by /usr/local/lib/libopencv_core.so, may conflict withcollect2: error: ld returned 1 exit status
 libcudart.so.7.5
.build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector.<int, std::allocatorbuild_debug<int> /> constlib&)'/libcaffe.so
.build_debug/: lib/undefined referencelibcaffe.so: to make:  `*** [.build_debug/tools/upgrade_net_proto_binary.bin] Error 1cvundefined: :referenceimread
( tocv:: `String cvconst&::imdecode, int(cv)'
.::_InputArraybuild_debug/ const&lib, int/)'
libcaffe.so: undefined reference to `cv::imencode(cv:collect2: error: ld returned 1 exit status
:String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_debug/lib/libcaffe.so: make: undefined reference*** [.build_debug/tools/caffe.bin] Error 1 to
 `cv::imdecode(cv::_InputArray const&, int)'
::imread(cv::String const&, int)'
collect2: error: ld returned 1 exit status
.build_debug/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
make: *** [.build_debug/tools/extract_features.bin] Error 1collect2: error: ld returned 1 exit status

make: *** [.build_debug/tools/compute_image_mean.bin] Error 1
.build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_debug/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
make: *** [.build_debug/tools/convert_imageset.bin] Error 1
wangxiao@wangxiao-GTX980:/media/wangxiao/247317a3-e6b5-45d4-81d1-956930526746/---------------/caffe-master$ 

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Answer: https://github.com/BVLC/caffe/issues/2348 
But if you delete all the file in build(rm -rf ./build/*) before "make all",you will success.I just success
so, just input the code and make all -j8 again.

well, same problem... and change the following code in Makefile:
>>>>>>>>  LIBRARIES += opencv_core opencv_highgui opencv_imgproc 
into :    LIBRARIES += opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

yeah, just add the: opencv_imgcodecs  at last.  and it really worked.



thus, the following steps may needed:

1. change the Makefile :

  add "opencv_imagecodecs" to the last of LIBRARIES += opencv_core opencv_highgui opencv_imgproc;

2. remove the files in build :

  rm -rf ./build/*

3. make all -j8

Caffe 框架中获取网络层参数可以通过访问网络对象 `net` 的 `layers` 属性实现。每个网络层对象包含其参数(如权重和偏置),这些参数通常存储在 `blobs` 属性中。以下是一个获取特定层参数的方法: ### 获取网络层参数的基本步骤: 1. **加载网络模型**:使用 Caffe 的 `Net` 类加载模型定义和权重文件。 2. **访问指定层**:通过 `net.layers` 获取所有层的列表,并通过索引或名称访问特定层。 3. **提取参数数据**:每层的参数存储在 `blobs` 中,可以通过 `blobs[0].data` 获取权重,`blobs[1].data` 获取偏置(如果存在)。 ### 示例代码 以下是一个使用 Python 接口获取卷积层参数的示例: ```python import caffe import numpy as np # 加载网络和权重 net = caffe.Net('path/to/deploy.prototxt', 'path/to/weights.caffemodel', caffe.TEST) # 打印所有层名称及其索引 for i, layer in enumerate(net.layers): print(f"Layer {i}: {layer.name}") # 获取指定层的参数(以第一个卷积层为例) layer_index = 1 # 假设第一个卷积层位于索引1 conv_layer = net.layers[layer_index] # 打印该层的权重和偏置 if len(conv_layer.blobs) > 0: weights = conv_layer.blobs[0].data print(f"Weights shape: {weights.shape}") if len(conv_layer.blobs) > 1: biases = conv_layer.blobs[1].data print(f"Biases shape: {biases.shape}") ``` ### 注意事项: - **层索引**:`net.layers` 返回的是网络中所有层的列表,索引顺序与 `prototxt` 文件中定义的顺序一致。 - **参数访问**:每层的参数(如权重和偏置)存储在 `blobs` 中,`blobs[0]` 通常表示权重,`blobs[1]` 表示偏置(如果存在)。 - **数据类型**:参数数据以 `numpy.ndarray` 形式存储,可以直接用于进一步处理或可视化[^4]。 ### 获取指定名称层的参数 如果已知层的名称,可以通过以下方式获取: ```python # 获取指定名称的层 layer_name = 'conv1_1' conv_layer = net.layers[list(net._layer_names).index(layer_name)] # 打印该层的权重和偏置 if len(conv_layer.blobs) > 0: weights = conv_layer.blobs[0].data print(f"Weights shape: {weights.shape}") if len(conv_layer.blobs) > 1: biases = conv_layer.blobs[1].data print(f"Biases shape: {biases.shape}") ``` 该方法通过 `net._layer_names` 获取所有层名称,并通过 `index` 找到指定名称层的索引,从而访问该层的参数[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值