Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 代码编译

本文详细记录了在Ubuntu16.04上编译Faster R-CNN时遇到的问题及解决办法,包括cudnn版本必须为v4、编译过程中出现的protobuf错误以及Makefile配置问题。同时提醒读者避免不必要的protobuf重装,并提供了编译所需库的安装步骤。

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

看到faster rcnn官方说明其实配置起来很简单,但是... 还是遇到了一堆的问题。有几个重要的地方还记得就强调一下。

1. faster rcnn的官方第一句话是改Makefile.config的配置:使用cudnn以及python layer。所以是需要cudnn的,而且,(划重点)。只能使用cudnn v4。 如果看我以前的文章就知道,我刚开始安装的时候看别人的教程,就安装的cudnn v5。 但是,跑这个的时候需要用的v4版本。不然会出现下面的问题:

.build_release/lib/libcaffe.so: undefined reference to `cudnnConvolutionBackwardData_v3'
.build_release/lib/libcaffe.so: undefined reference to `cudnnConvolutionBackwardFilter_v3'
collect2: error: ld returned 1 exit status
Makefile:626: recipe for target '.build_release/tools/convert_imageset.bin' failed
make: *** [.build_release/tools/convert_imageset.bin] Error 1

或者是

In file included from ./include/caffe/util/cudnn.hpp:5:0,
from ./include/caffe/util/device_alternate.hpp:40,
from ./include/caffe/common.hpp:19,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/layer.hpp:8,
from src/caffe/layer.cpp:2:
/usr/include/cudnn.h:803:27: note: declared here
cudnnStatus_t CUDNNWINAPI cudnnSetPooling2dDescriptor(
^
Makefile:563: recipe for target '.build_release/src/caffe/layer.o' failed
make: *** [.build_release/src/caffe/layer.o] Error 1


2.还有,官方说在make 的时候是make -j8 && make pycaffe。但是,如果你出现了以下的问题

In file included from ./include/caffe/util/device_alternate.hpp:40:0,
                 from ./include/caffe/common.hpp:19,
                 from src/caffe/net.cpp:10:
./include/caffe/util/cudnn.hpp:8:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory
compilation terminated.
Makefile:582: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
make: *** Waiting for unfinished jobs....

是因为运行速度太快,只要分开先make,完成之后再make pycaffe就没有问题。


3.刚开始还碰到一个脑残的问题

Makefile:6: *** Makefile.config not found. See Makefile.config.example.. Stop.

这是因为直接扒下来的东西里面是没有Makefile.config的,你需要加入Makefile.config。当然,有人是cp Makefile.config.example Makefile.config 来重新设置,如果你之前配成功过caffe,直接拷过来也是没问题的。最好把Makefile文件一起拷过来,如果你之前像我一样也改动过Makefile文件的话。

我的Makefile文件如下:

PROJECT := caffe

CONFIG_FILE := Makefile.config
# Explicitly check for the config file, otherwise make -k will proceed anyway.
ifeq ($(wildcard $(CONFIG_FILE)),)
$(error $(CONFIG_FILE) not found. See $(CONFIG_FILE).example.)
endif
include $(CONFIG_FILE)

BUILD_DIR_LINK := $(BUILD_DIR)
ifeq ($(RELEASE_BUILD_DIR),)
RELEASE_BUILD_DIR := .$(BUILD_DIR)_release
endif
ifeq ($(DEBUG_BUILD_DIR),)
DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug
endif

DEBUG ?= 0
ifeq ($(DEBUG), 1)
BUILD_DIR := $(DEBUG_BUILD_DIR)
OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)
else
BUILD_DIR := $(RELEASE_BUILD_DIR)
OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)
endif

# All of the directories containing code.
SRC_DIRS := $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \
\( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print)

# The target shared library name
LIBRARY_NAME := $(PROJECT)
LIB_BUILD_DIR := $(BUILD_DIR)/lib
STATIC_NAME := $(LIB_BUILD_DIR)/lib$(LIBRARY_NAME).a
DYNAMIC_VERSION_MAJOR := 1
DYNAMIC_VERSION_MINOR := 0
DYNAMIC_VERSION_REVISION := 0-rc5
DYNAMIC_NAME_SHORT := lib$(LIBRARY_NAME).so
#DYNAMIC_SONAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR)
DYNAMIC_VERSIONED_NAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)
DYNAMIC_NAME := $(LIB_BUILD_DIR)/$(DYNAMIC_VERSIONED_NAME_SHORT)
COMMON_FLAGS += -DCAFFE_VERSION=$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)

##############################
# Get all source files
##############################
# CXX_SRCS are the source files excluding the test ones.
CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
# CU_SRCS are the cuda source files
CU_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cu" -name "*.cu")
# TEST_SRCS are the test source files
TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
TEST_CU_SRCS := $(shell find src/$(PROJECT) -name "test_*.cu")
GTEST_SRC := src/gtest/gtest-all.cpp
# TOOL_SRCS are the source files for the tool binaries
TOOL_SRCS := $(shell find tools -name "*.cpp")
# EXAMPLE_SRCS are the source files for the example binaries
EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
# BUILD_INCLUDE_DIR contains any generated header files we want to include.
BUILD_INCLUDE_DIR := $(BUILD_DIR)/src
# PROTO_SRCS are the protocol buffer definitions
PROTO_SRC_DIR := src/$(PROJECT)/proto
PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)
# PROTO_BUILD_DIR will contain the .cc and obj files generated from
# PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)
PROTO_BUILD_INCLUDE_DIR := $(BUILD_INCLUDE_DIR)/$(PROJECT)/proto
# NONGEN_CXX_SRCS includes all source/header files except those generated
# automatically (e.g., by proto).
NONGEN_CXX_SRCS := $(shell find \
src/$(PROJECT) \
include/$(PROJECT) \
python/$(PROJECT) \
matlab/+$(PROJECT)/private \
examples \
tools \
-name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")
LINT_SCRIPT := scripts/cpp_lint.py
LINT_OUTPUT_DIR := $(BUILD_DIR)/.lint
LINT_EXT := lint.txt
LINT_OUTPUTS := $(addsuffix .$(LINT_EXT), $(addprefix $(LINT_OUTPUT_DIR)/, $(NONGEN_CXX_SRCS)))
EMPTY_LINT_REPORT := $(BUILD_DIR)/.$(LINT_EXT)
NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT)
# PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp
PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so
PY$(PROJECT)_HXX := include/$(PROJECT)/layers/python_layer.hpp
# MAT$(PROJECT)_SRC is the mex entrance point of matlab package for $(PROJECT)
MAT$(PROJECT)_SRC := matlab/+$(PROJECT)/private/$(PROJECT)_.cpp
ifneq ($(MATLAB_DIR),)
MAT_SO_EXT := $(shell $(MATLAB_DIR)/bin/mexext)
endif
MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)

##############################
# Derive generated files
##############################
# The generated files for protocol buffers
PROTO_GEN_HEADER_SRCS := $(addprefix $(PROTO_BUILD_DIR)/, \
$(notdir ${PROTO_SRCS:.proto=.pb.h}))
PROTO_GEN_HEADER := $(addprefix $(PROTO_BUILD_INCLUDE_DIR)/, \
$(notdir ${PROTO_SRCS:.proto=.pb.h}))
PROTO_GEN_CC := $(addprefix $(BUILD_DIR)/, ${PROTO_SRCS:.proto=.pb.cc})
PY_PROTO_BUILD_DIR := python/$(PROJECT)/proto
PY_PROTO_INIT := python/$(PROJECT)/proto/__init__.py
PROTO_GEN_PY := $(foreach file,${PROTO_SRCS:.proto=_pb2.py}, \
$(PY_PROTO_BUILD_DIR)/$(notdir $(file)))
# The objects corresponding to the source files
# These objects will be linked into the final shared library, so we
# exclude the tool, example, and test objects.
CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o})
PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}
OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
# tool, example, and test objects
TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o})
TOOL_BUILD_DIR := $(BUILD_DIR)/tools
TEST_CXX_BUILD_DIR := $(BUILD_DIR)/src/$(PROJECT)/test
TEST_CU_BUILD_DIR := $(BUILD_DIR)/cuda/src/$(PROJECT)/test
TEST_CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})
TEST_CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o})
TEST_OBJS := $(TEST_CXX_OBJS) $(TES

<think>好的,我现在需要帮助用户解决关于Faster R-CNN论文中实验部分的代码复现问题,特别是使用PyTorch和TensorFlow的情况。首先,我要回顾用户提供的引用内容,确保回答准确。 用户引用了三个来源,其中引用[1]详细解释了Faster R-CNN的结构和背景,引用[2]提到了Fast R-CNN的PyTorch实现步骤,而引用[3]则提供了一个具体的代码仓库和训练命令。这些信息都是关键,尤其是引用[3]中的代码复现步骤,用户可能已经尝试过但需要更多指导。 接下来,我需要明确用户的需求:他们希望复现Faster R-CNN的实验部分,可能包括训练、评估和结果对比。需要确认用户是否已经了解Faster R-CNN的基本结构,比如RPN网络、RoI池化等,但根据问题描述,用户可能已经具备一定基础,现在需要具体的代码实现指导。 根据引用[3],使用PyTorch复现的步骤已经存在,但用户可能需要更详细的说明,比如环境配置、数据准备、参数调整等。而TensorFlow的实现可能需要参考其他资源,用户提供的引用中没有直接提到,但可以建议官方实现或其他可靠仓库。 在回答时,需要分步骤说明PyTorch和TensorFlow的实现方法,并引用提供的资料。例如,PyTorch部分可以基于引用[3]的代码库,而TensorFlow则可能需要寻找其他资源。同时,要提醒用户注意数据集的准备,如PASCAL VOC或COCO,以及训练时的参数设置,如学习率、批次大小等。 还需要考虑用户可能遇到的常见问题,比如依赖项安装、CUDA版本兼容性、训练过程中的调试技巧。可以建议用户查看仓库的文档和issue部分,以解决特定错误。 最后,生成相关问题时要围绕复现过程中可能遇到的挑战和进一步的应用,比如性能优化、模型调整、结果可视化等。确保问题有助于用户深入理解和解决实际问题。</think>关于Faster R-CNN实验部分的代码复现,根据提供的参考资料,我为您梳理PyTorch和TensorFlow实现的关键步骤: ### 一、PyTorch复现方案 1. **代码库推荐**: simple-faster-rcnn-pytorch(引用[3])是一个轻量级实现,包含完整的训练/验证流程,支持PASCAL VOC数据集。其核心实现包括: ```python # RPN网络结构示例 class RPN(nn.Module): def __init__(self, in_channels=512, mid_channels=512): super().__init__() self.conv = nn.Conv2d(in_channels, mid_channels, 3, padding=1) self.cls_layer = nn.Conv2d(mid_channels, 18, 1) # 9 anchors * 2(fore/back) self.reg_layer = nn.Conv2d(mid_channels, 36, 1) # 9 anchors * 4(dx,dy,dw,dh) # RoI池化层 roi_pool = RoIPool(7, 7, spatial_scale=1.0/16) ``` 2. **训练配置**(引用[3]): ```bash python train.py train \ --env='fasterrcnn-caffe' \ --plot-every=100 \ --caffe-pretrain ``` 关键参数说明: - `--caffe-pretrain`: 使用Caffe预训练的VGG16权重 - `--plot-every`: 每100次迭代可视化损失和预测结果 3. **实验指标实现**: 需在`eval.py`中实现: ```python def eval_detection(pred_boxes, gt_boxes, iou_threshold=0.5): # 计算mAP的逻辑 ... return AP, precision, recall ``` ### 二、TensorFlow复现方案 推荐使用官方实现[TensorFlow Models](https://github.com/tensorflow/models/tree/master/research/object_detection),其包含: 1. **配置文件关键参数**: ```protobuf faster_rcnn { num_classes: 20 image_resizer { keep_aspect_ratio_resizer { min_dimension: 600, max_dimension: 1024 }} feature_extractor { type: 'faster_rcnn_resnet101' } first_stage_anchor_generator { ... } second_stage_box_predictor { ... } } ``` 2. **训练命令**: ```bash python object_detection/model_main.py \ --pipeline_config_path=configs/faster_rcnn_resnet101_voc.config \ --model_dir=training/ ``` ### 三、实验复现注意事项 1. **数据集准备**: - PASCAL VOC需转换为COCO格式(引用[3]的实现使用自定义数据加载器) - 数据增强建议包括:随机水平翻转、颜色抖动、缩放裁剪 2. **收敛性调试**: - 初始学习率建议设为$3\times10^{-4}$,每5个epoch衰减10% - RPN建议使用Smooth L1 Loss,分类任务使用CrossEntropy Loss 3. **硬件要求**: | 框架 | GPU显存 | 训练时间(VOC) | |---------|-------|------------| | PyTorch | 8GB | ~12小时 | | TensorFlow | 11GB | ~18小时 | ### 四、实验结果验证 应复现论文中的关键指标: $$ \text{mAP} = \frac{1}{N}\sum_{i=1}^{N} AP_i $$ 在VOC2007 test set上应达到约70.0% mAP(使用ResNet-101时)[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值