1、安装
准备环境
-
创建一个 conda 虚拟环境并激活它。
conda create -n openmmlab python=3.7 -y conda activate openmmlab
-
按照官方说明安装 PyTorch 和 torchvision,例如,
conda install pytorch torchvision -c pytorch 注意:请确保编译 CUDA 版本和运行时 CUDA 版本匹配。 您可以在 PyTorch 网站上查看预编译包支持的 CUDA 版本。
2、安装 MMDetection
建议使用 MIM 安装 MMDetection, 自动处理 OpenMMLab 项目的依赖项,包括 mmcv 和其他 python 包。
pip install openmim
mim install mmdet
3、下载mmdetection代码
在github官网下载mmdetection代码。
下载压缩包后,解压好之后用pycharm打开mmdetection项目。
1.在demo目录下创建Images和outputs文件夹,并在outputs文件夹下创建result文件夹。再将demo文件夹下的demo.jpg图片放入Images文件夹中.
2.在根目录下创建checkpoints文件,在本文资源中下载预训练模型。
3.在demo文件夹下创建demo.py文件,将下述代码复制到demo.py文件中。
import os
from mmdet.apis import init_detector, inference_detector
from mmdet.registry import VISUALIZERS
import mmcv
# 使用前修改下面的路径
image_path = r'demo/Images' #此处就写刚刚创建的Images路径
savePath = r'E:\WorkSpace\mmdetection-main\demo\outputs\result' #此处换成result文件夹在自己电脑下文件的绝对路径
config_file = 'E:/WorkSpace/mmdetection-main/work_dirs/faster-rcnn_r50_fpn_1x_coco.py' #此处换成自己盘下的绝对路径,如果需要其他的配置文件可以换
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# gpu 不行 会 Out of memory
device = 'cuda:0'
model = init_detector(config_file, checkpoint_file, device=device)
visualizer = VISUALIZERS.build(model.cfg.visualizer)
# the dataset_meta is loaded from the checkpoint and
# then pass to the model in init_detector
visualizer.dataset_meta = model.dataset_meta
for filename in os.listdir(image_path):
imgPath = os.path.join(image_path, filename)
print('process ', imgPath)
img = mmcv.imread(imgPath)
result = inference_detector(model, img)
out_file = os.path.join(savePath, filename)
### old implementation invalid
# show_result_pyplot(model, img, result, out_file, score_thr=0.3)
###
# show the results
visualizer.add_datasample('result',
img, data_sample=result,
draw_gt=False,
wait_time=0,
out_file=out_file,
pred_score_thr=0.3
)
# vis
# visualizer.show()
# break
到此就部署完成,并训练好了。