Visual Studio: 因项目开发需求笔者所用电脑装有vs2013和vs2015两个版本(vs2015为后安装的,打开工程时只能用vs2015打开),MS Caffe编译必须采用vs2013。在打开caffe.sln时,会有图1提示,选择"取消"即可,打开后如图2,即虽然用的是vs2015,但工程配置仍为2013版.
Anaconda:caffe支持python2.7。笔者安装的是Anaconda3(默认python3.7),因此需要在Anaconda Navigator中创建新环境。如图3-4,创建的新环境基于python2.7,名为"caffe",后续配置python路径需要配置该环境下python2.7的路径。
下载MS Caffe,将caffe-master/windows/CommonSettings.props.example复制一份,存为caffe-master/windows/CommonSettings.prop。打开CommonSettings.props进行配置,如图2-1,注意修改CuDNN和Python2.7的路径。
配置完成后,用vs打开caffe-master/windows/Caffe.sln。由于faster-rcnn中使用了roi-pooling-layer层,而微软版本编译时未添加roi_pooling_layer,因此需要将roi_pooling_layer的.cu/.hpp/.cpp分别添加libcaffe项目的cu/include/src文件下的layer中,如图2-2。
添加完成后,配置为Release模式先编译libcaffe,成功后再编译整个解决方案即可。
(1)下载py-faster-rcnn和py-faster-rcnn-windows,将后者中的文件复制到py-faster-rcnn中替换。
修改1:lib/rpn/proposal_layer.py line 64:
修改前:cfg_key = str(self.phase)
修改后:cfg_key = str('TRAIN' if self.phase == 0 else 'TEST')
修改2:将以下文件中的“param_str_”替换为“param_str”
lib/rpn/anchor_target_layer.py line 27
lib/rpn/proposal_layer.py line 26
lib/rpn/proposal_target_layer.py line 25
lib/roi_data_layer/layer.py line 87
(2)将编译好的Caffe根目录下 caffe-master\Build\x64\Release\pycaffe中的caffe文件夹复制到py-faster-rcnn根目录py-faster-rcnn\caffe-fast-rcnn\python中,如图
(3)修改py-faster-rcnn根目录下py-faster-rcnn/lib/setup_cuda.py line 33,配置为本机的CUDA/include路径。
(1)运行py-faster-rcnn/data/scripts下的fetch_faster_rcnn_models.sh获取model,或者直接从网站下载faster_rcnn_models.tgz,下载后解压到py-faster-rcnn/data下,如图
(2)打开Anaconda Prompt,激活caffe环境,切换到py-faster-rcnn根目录下,执行
python ./tools/demo.py
错误1:db_lmdb.hpp Check failed: mdb_status == 0 (3 vs. 0) 系统找不到指定的路径
解决:路径问题,mnist数据集下载后压缩包名称和解压缩后的文件名称有差别。如压缩包名:train-images-idx3-ubyte.gz,内部文件名:train-images.idx3-ubyte。数据转换时数据集名称一定是解压缩后内部的文件名。
错误2:Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe’ failed with exit status 2
解决:下载并安装VCForPython27 Microsoft Visual C++ Compiler for Python 2.7
错误3:安装VCForPython27后,Cannot open include file: ‘stdbool.h’
解决:环境变量添加 VS90COMNTOOLS,并将其指给 D:\Program Files\Microsoft Visual Studio 12.0\Common7\Tools
错误4:layer_factory.hpp Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: ROIPooling
解决:faster-rcnn中使用了roi-pooling-layer层,而微软版本caffe编译时未添加roi_pooling_layer,需要将roi_pooling_layer的.cu/.hpp/.cpp分别添加libcaffe项目的cu/include/src文件下的layer中。三个文件都要添加!如果未添加.hpp,亲测也会出现上述错误。
错误5:IndentationError
解决:python代码对齐问题。
错误6: No module named xxx
解决:安装相应包,pip install xxx
错误7:has no attribute ‘param_str_’
解决:将’param_str_‘更改为’param_str’,'param_str_'存在于以下4个位置:lib/rpn/anchor_target_layer.py、lib/rpn/proposal_layer.py、lib/rpn/proposal_target_layer.py、lib/roi_data_layer/layer.py
错误8:TypeError: slice indices must be integers or None or have an _ index_ method
Solving...
Traceback (most recent call last):
File "./tools/train_net.py", line 140, in
max_iters=args.max_iters)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\fast_rcnn\train.py", line 160, in train_net
model_paths = sw.train_model(max_iters)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\fast_rcnn\train.py", line 101, in train_model
self.solver.step(1)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\rpn\proposal_target_layer.py", line 67, in forward
rois_per_image, self._num_classes)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\rpn\proposal_target_layer.py", line 192, in _sample_rois
_get_bbox_regression_labels(bbox_target_data, num_classes)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\rpn\proposal_target_layer.py", line 128, in _get_bbox_regression_labels
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
TypeError: slice indices must be integers or None or have an __index__ method
解决:修改lib/rpn/proposal_target_layer.py line 123
更改前:
for ind in inds:
cls = clss[ind]
start = 4 * cls
end = start + 4
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
return bbox_targets, bbox_inside_weights
更改后:
for ind in inds:
ind = int(ind) #cwr
cls = clss[ind]
start = int(4 * cls) #cwr
end = int(start + 4) #cwr
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
return bbox_targets, bbox_inside_weights
错误9:训练过程中出现CURAND_STATUS_LAUNCH_FAILURE,如下
Solving...
F0509 13:49:04.570956 14628 math_functions.cu:375] Check failed: status == CURAND_STATUS_SUCCESS (201 vs. 0) CURAND_STATUS_LAUNCH_FAILURE
*** Check failure stack trace: ***
解决:笔者最初环境配置为win10 + CUDA7.5,出现该问题,看有人说是CUDA7.5对于10系列的显卡支持不是很完善。尝试用CUDA8.0 for win10 + cudnn 5.0 for win10,重新编译caffe,配置后测试,问题解决。
错误10:TypeError: ‘numpy.float64’ object cannot be interpreted as an index
Traceback (most recent call last):
File "./tools/train_net.py", line 140, in
max_iters=args.max_iters)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\fast_rcnn\train.py", line 160, in train_net
model_paths = sw.train_model(max_iters)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\fast_rcnn\train.py", line 101, in train_model
self.solver.step(1)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\rpn\proposal_target_layer.py", line 67, in forward
rois_per_image, self._num_classes)
File "G:\DL\py-faster-rcnn-sp\FRCNN\tools\..\lib\rpn\proposal_target_layer.py", line 168, in _sample_rois
fg_inds = npr.choice(fg_inds, size=fg_rois_per_this_image, replace=False)
File "mtrand.pyx", line 1197, in mtrand.RandomState.choice
TypeError: 'numpy.float64' object cannot be interpreted as an index
解决:numpy1.12及以后版本删除了使用浮点数索引的功能,一种解决方案是采用低版本numpy(numpy 1.11.0),另一种解决方案是通过添加astype(np.int)将产生的float型数据强转为int。第一种方案笔者没测过,这里采用第二种解决方案:共有4处需修改
lib/roi_data_layer/minibatch.py line 26
更改前:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
更改后:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)
lib/datasets/ds_utils.py line 12
更改后:hashes = np.round(boxes * scale).dot(v).astype(np.int)
lib/fast_rcnn/test.py line 129
更改后:hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v).astype(np.int)
lib/rpn/proposal_target_layer.py line 60
更改后:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)
参考资料: