根据官网的install.md教程并结合自己电脑配置环境安装
电脑配置
Ubuntu 16.04
RTX2070
环境主要配置版本:
python3.7
cuda 10.0
cudnn 7.6.4
pytorch 1.1.0
环境里缺的包自己在终端用pip安装上。
安装mmcv
git clone https://github.com/open-mmlab/mmcv.git
cd mmcv
pip install . # 此处有个 .
将整个mmdetection项目 clone 到电脑上
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
之后,需要进行编译,切勿忘记这一步编译。
即
pip install -r requirements/build.txt
pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI"
pip install -v -e . # or "python setup.py develop"
参考博客:mmdetection工具网上博客整理
mmdetection源码笔记(一):train.py解读
mmdetection源码阅读笔记
mmdetection - 基于PyTorch的开源目标检测系统
将coco模型转化为voc模型:参考链接
环境配置及编译结束后,开始进行demo的测试。
demo测试-前提条件是需要有模型文件,在mmdetection文件夹下创建checkpoints文件夹放置模型文件。模型文件下载处。
模型配置文件在configs
文件下。其各个参数的含义参考:
博客:mmdetection的configs中的各项参数具体解释
在 tools 文件夹下 新建demo.py
demo.py测试代码如下:
from mmdet.apis import init_detector, inference_detector, show_result
#
config_file = '../configs/rpn_r50_fpn_1x.py'
checkpoint_file = '../checkpoints/rpn_r50_fpn_1x_20181010-4a9c0712.pth'
model = init_detector(config_file, checkpoint_file)
img = '../demo/demo.jpg'
result = inference_detector(model, img)
# show_result(img, result, model.CLASSES)
# show_result(img, result, model.CLASSES, out_file='result.jpg')
show_result(img, result, model.CLASSES)
报错:
Traceback (most recent call last):
File "/home/ouc/6_objectdet/URPC2020/mmdetection/tools/demo.py", line 43, in <module>
show_result(img, result, model.CLASSES)
File "/home/ouc/6_objectdet/URPC2020/mmdetection/mmdet/apis/inference.py", line 179, in show_result
out_file=out_file)
File "/home/ouc/anaconda3/envs/mmlab/lib/python3.7/site-packages/mmcv/visualization/image.py", line 112, in imshow_det_bboxes
assert bboxes.shape[0] == labels.shape[0]
AssertionError
debug发现:bboxes.shape[0] = 2000 , 而labels.shape[0] = 10000.当然会出现assertionerror。出现问题原因是什么?
建立软连接,将VOC2007的数据集链接到data文件夹下
cd mmdetection
mkdir data
ln -s /home/ouc/4_data/VOCdevkit2007 data/VOCdevkit
建立软连接可以节省内存空间。但是注意若改变软连接过来的数据集,源数据集也会改变。
终端执行代码测试VOC2007数据集:
python tools/test.py configs/rpn_r50_fpn_1x_mod_voc.py checkpoints/rpn_r50_fpn_1x_20181010-4a9c0712.pth --show
原本mmdetection中没有rpn对voc2007的测试py文件。我是根据configs/pascal_voc
中的py文件更改的configs下的.py文件。主要是读取的路径格式。
执行代码后报错:
File "/home/ouc/anaconda3/envs/mmlab/lib/python3.7/site-packages/mmcv/visualization/image.py", line 17, in imshow
cv2.imshow(win_name, imread(img))
cv2.error: OpenCV(4.2.0) /io/opencv/modules/highgui/src/window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
参考链接
卸载opencv重新安装:
pip uninstall opencv-python
sudo apt-get remove libopencv-dev python-opencv
sudo apt-get install libopencv-dev python-opencv
执行代码:
python tools/test.py configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth --eval bbox --out work_dirs
报错:
Traceback (most recent call last):
File "tools/test.py", line 282, in <module>
main()
File "tools/test.py", line 197, in main
raise ValueError('The output file must be a pkl file.')
ValueError: The output file must be a pkl file.
故更改执行代码:
python tools/test.py configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth --eval bbox --out ./result/result_100.pkl
可以执行,但是提醒:
The model and loaded state dict do not match exactly
size mismatch for bbox_head.fc_cls.weight: copying a param with shape torch.Size([81, 1024]) from checkpoint, the shape in current model is torch.Size([21, 1024]).
size mismatch for bbox_head.fc_cls.bias: copying a param with shape torch.Size([81]) from checkpoint, the shape in current model is torch.Size([21]).
size mismatch for bbox_head.fc_reg.weight: copying a param with shape torch.Size([324, 1024]) from checkpoint, the shape in current model is torch.Size([84, 1024]).
size mismatch for bbox_head.fc_reg.bias: copying a param with shape torch.Size([324]) from checkpoint, the shape in current model is torch.Size([84]).
应该是,下载的pth文件与其不是很匹配。
然后出现错误:
[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 4952/4952, 15.0 task/s, elapsed: 331s, ETA: 0s
writing results to ./result/result_100.pkl
Starting evaluate bbox
Traceback (most recent call last):
File "tools/test.py", line 282, in <module>
main()
File "tools/test.py", line 259, in main
result_files = results2json(dataset, outputs, args.out)
File "/home/ouc/6_objectdet/URPC2020/mmdetection/mmdet/core/evaluation/coco_utils.py", line 204, in results2json
json_results = det2json(dataset, results)
File "/home/ouc/6_objectdet/URPC2020/mmdetection/mmdet/core/evaluation/coco_utils.py", line 149, in det2json
img_id = dataset.img_ids[idx]
AttributeError: 'VOCDataset' object has no attribute 'img_ids'
原因参考: issue
我们想要测试voc的数据集,但是调用了coco_utils.py, 这是用来eval coco数据集的。故出现此错误。
解决方法:是先使用--out results.pkl
将结果写入pkl文件。 然后使用tools / voc_eval.py
评估结果。 此处参考链接
即:
python tools/test.py configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth --out ./result/result_100.pkl
python tools/voc_eval.py result/result_100.pkl configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py
然后报错:
Traceback (most recent call last):
File "tools/voc_eval.py", line 47, in <module>
main()
File "tools/voc_eval.py", line 43, in main
voc_eval(args.result, test_dataset, args.iou_thr, args.nproc)
File "tools/voc_eval.py", line 23, in voc_eval
nproc=nproc)
File "/home/ouc/6_objectdet/URPC2020/mmdetection/mmdet/core/evaluation/mean_ap.py", line 385, in eval_map
mean_ap, eval_results, dataset, area_ranges, logger=logger)
File "/home/ouc/6_objectdet/URPC2020/mmdetection/mmdet/core/evaluation/mean_ap.py", line 455, in print_map_summary
print_log('\n' + table.table, logger=logger)
File "/home/ouc/6_objectdet/URPC2020/mmdetection/mmdet/utils/logger.py", line 66, in print_log
'"silent" or None, but got {}'.format(logger))
TypeError: logger should be either a logging.Logger object, "root", "silent" or None, but got print
代码链接
跑 x101_64x4d (htc pretrained)时:
出现RuntimeError: all tensors must be on devices[0]
原因为mmcv的版本不合适,应换为0.2.16版本