1、找到一个基础镜像(包含tocrh等所需基础环境)
2、利用docker run构建并开启一个docker容器
docker run --gpus all -itd -v /home/wzx:/workspace --name wzx-openmmlab-v1 pytorch/pytorch:1.6.0-cuda10.1-cudnn7-devel
其中,pytorch/pytorch代表REPOSITORY,1.6.0-cuda10.1-cudnn7-devel代表Tag。
3、安装mmcv-full
pip install mmcv-full==1.3.17 -i https://pypi.tuna.tsinghua.edu.cn/simple
4、将mmdetection2.25.0包下载后放入/workspace目录下
5、安装mmdetection的依赖(包括cython、numpy)
cd workspace/mmdetection-2.25.0
pip install -r requirements/build
6、执行mmdetection的配置文件
pip install --no-cache-dir -e .
其中,--no-cache-dir选项可使pip命令在安装工具包时不使用缓存目录。
7、创建目录datasets,将所用数据集放入其中,目录如下:
--datasets
--mine_safety_helmet_data2023_coco
--annotations
--train2017
--val2017
8、执行tools/train.py文件,生成work_dirs/deformable_detr_r50_16x2_50e_coco/deformable _detr_ r50_16x2_50e_coco.py生成训练所需的配置文件。
将所生成的配置文件deformable _detr_ r50_16x2_50e_coco.py重命名为my_deformable_detr_r50_16x2_50e_coco.py,并将其拷贝到configs/deformable_detr中。
9、修改上述生成的配置文件
1)将configs/deformable_detr/deformable_detr_r50_16x2_50e_coco.py文件中的num_classes设置为正确数值:
bbox_head=dict(
type='DeformableDETRHead',
num_query=300,
num_classes=1, # 检测类别数
in_channels=2048,
......
data = dict(
samples_per_gpu=1, # batch_size
workers_per_gpu=2,
dist_params = dict(backend='nccl')
log_level = 'INFO'
# 预训练权重
load_from = './work_dirs/deformable_detr_r50_16x2_50e_coco/deformable_detr_r50_16x2_50e_coco_20210419_220030-a12b9512.pth'
resume_from = None
其中,samples_per_gpu为batch_size,workers_per_gpu代表dataloader加载数据时创建的进程数。当显存和内存较小时,可设置为samples_per_gpu=1,workers_per_gpu=0(意味着每一轮迭代时,dataloader不再自主加载数据到RAM,而是在RAM中找batch,找不到时再加载相应的batch)。
load_from设置为对应的预训练权重路径(若需要)。
2)修改configs/_base_/datasets/coco_detection.py文件中的data_root(数据集根目录),mmdet/datasets/coco.py中的CLASSES(当只有一个类别时,在标签后加一个“,”),例如:
CLASSES = ('Mine_Safety_Helmet',) # 类别
PALETTE = [(220, 20, 60),] # 颜色
3)修改mmdet/core/evaluation/class_names.py中coco_classes方法中的return类别。
10、训练
1)单卡训练:
python tools/train.py configs/deformable_detr/deformable_detr_r50_16x2_50e_coco.py
2)多卡训练:
./tools/dist_train.sh ./configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py 4
4代表使用的gpu数量。
11、测试模型推理效果
测试模型对所有待处理图像的检测效果,测试代码地址:inference_dir.py下载地址
将该代码下载后存放到mmdetection的demo/下,并执行python inference_dir.py执行检测。