文章目录
- 资料
- 环境
- 尝试训练
- 安全帽数据训练
- 测试
- 预测
- 全部数据、代码、训练完的权重等资料见:
资料
依据这个进行训练:
https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/tree/master/pytorch_object_detection/faster_rcnn
├── backbone: 特征提取网络,可以根据自己的要求选择
├── network_files: Faster R-CNN网络(包括Fast R-CNN以及RPN等模块)
├── train_utils: 训练验证相关模块(包括cocotools)
├── my_dataset.py: 自定义dataset用于读取VOC数据集
├── train_mobilenet.py: 以MobileNetV2做为backbone进行训练
├── train_resnet50_fpn.py: 以resnet50+FPN做为backbone进行训练
├── train_multi_GPU.py: 针对使用多GPU的用户使用
├── predict.py: 简易的预测脚本,使用训练好的权重进行预测测试
├── validation.py: 利用训练好的权重验证/测试数据的COCO指标,并生成record_mAP.txt文件
└── pascal_voc_classes.json: pascal_voc标签文件
环境
dockerfile:
FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04
ENV DEBIAN_FRONTEND=noninteractive
# 安装基本软件包
RUN apt-get update && \
apt-get upgrade -y && \
apt-get -y --no-install-recommends install vim wget curl build-essential python3.10-dev python3.10 python3-pip sudo && \
update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1 && \
apt-get install -y libgl1 libglib2.0-0 ffmpeg tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone
RUN apt-get -y --no-install-recommends install vim wget curl git build-essential python3.10 python3-pip python3.10-venv sudo
RUN apt-get install -y libgl1 libglib2.0-0 iputils-ping python3.10-dev libgoogle-perftools-dev nginx
# 更改默认Shell为bash
SHELL ["/bin/bash", "-c"]
python 环境:
git clone https://github.com/WZMIAOMIAO/deep-learning-for-image-processing.git
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
conda create -n py38 python=3.8 -y
conda activate py38
# CUDA 11.0
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c pytorch -y
cd /deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn
pip install -r requirements.txt
得到readme.md说的一些权重:
cd /deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn/backbone
wget https://download.pytorch.org/models/mobilenet_v2-b0353104.pth
--2024-06-05 13:50:21-- https://download.pytorch.org/models/mobilenet_v2-b0353104.pth
mv mobilenet_v2-b0353104.pth mobilenet_v2.pth
wget https://download.pytorch.org/models/resnet50-0676ba61.pth
--2024-06-05 13:50:46-- https://download.pytorch.org/models/resnet50-0676ba61.pth
mv resnet50-0676ba61.pth resnet50.pth
wget https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth
mv fasterrcnn_resnet50_fpn_coco-258fb6c6.pth fasterrcnn_resnet50_fpn_coco.pth
mkdir /deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn/data
cd /deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn/data
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
尝试训练
反向commit 镜像:
docker commit 74d9893ccb29 kevinchina/deeplearning:fasterrcnn_train_v1
docker push kevinchina/deeplearning:fasterrcnn_train_v1
重启容器:
docker run --gpus all -it -v $PWD:/wkp --shm-size=64g kevinchina/deeplearning:fasterrcnn_train_v1 bash
训练:
conda activate py38
cd /deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn/
python train_mobilenetv2.py
启动成功:
一轮训练完成后的验证:
安全帽数据训练
安全帽佩戴检测
数据集:https://github.com/njvisionpower/Safety-Helmet-Wearing-Dataset
加入安全帽数据,小小修改一下源代码的一些小的东西:
启动训练:
python train_mobilenetv2.py
训练完一轮:
训练结束:
Test: Total time: 0:00:52 (0.0858 s / it)
Averaged stats: model_time: 0.0590 (0.0440) evaluator_time: 0.2436 (0.0344)
Accumulating evaluation results...
DONE (t=1.42s).
IoU metric: bbox
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.412
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.695
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.425
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.171
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.540
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.669
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.169
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.395
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.466
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.262
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.602
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.714
successful save loss curve!
successful save mAP curve!
测试
用test.txt中测试准确率
python validation.py
预测
(py38) root:/deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn# python predict.py
using cuda:0 device.
inference+NMS time: 0.018668174743652344
docker push kevinchina/deeplearning:fasterrcnn_train_v2
全部数据、代码、训练完的权重等资料见:
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2