【深度学习】安全帽检测,目标检测,Faster RCNN训练

文章目录

资料

依据这个进行训练:

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:

dart 复制代码
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 环境:

dart 复制代码
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说的一些权重:

dart 复制代码
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 镜像:

dart 复制代码
docker commit 74d9893ccb29 kevinchina/deeplearning:fasterrcnn_train_v1

docker push kevinchina/deeplearning:fasterrcnn_train_v1

重启容器:

dart 复制代码
docker run --gpus all -it -v $PWD:/wkp --shm-size=64g kevinchina/deeplearning:fasterrcnn_train_v1 bash

训练:

dart 复制代码
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

加入安全帽数据,小小修改一下源代码的一些小的东西:




启动训练:

dart 复制代码
python train_mobilenetv2.py

训练完一轮:

训练结束:

dart 复制代码
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中测试准确率

dart 复制代码
python validation.py

预测

dart 复制代码
(py38) root@f3661bce90f6:/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

全部数据、代码、训练完的权重等资料见:

dart 复制代码
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2
相关推荐
AI算力5 小时前
让安全驾驶有“AI”相伴|腾视科技DMS视频监控一体机,守护每一次出行
人工智能·科技·ai·车载终端·ai算力·车载智能终端·车载dvr
泯泷5 小时前
超越基准:Claude Opus 4.5 的 4 个惊人洞察
人工智能
果果软件6 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源
Csvn6 小时前
刚开始用 WorkBuddy,第一步该做什么?
人工智能
一水鉴天6 小时前
同一与唯一:AppBus 外壳与 DevOps 内核 之1 20261121(元宝)
人工智能·机器学习
Wellijohn6 小时前
一个 Android 老兵的十年开源路:从 167 star 的滑动控件,到本地优先的 AI Agent
android·人工智能·开源
ZONGYUAN-ROOT6 小时前
【API配额】API 配额监控告警不生效?用 Python 监控读数变化量,抓住静态阈值漏掉的「假重置」
人工智能
Wellijohn7 小时前
我把公司内部的 12 个 CRUD 接口交给了 AI Agent,然后省出了两周
人工智能·python·mcp
伞伞悦读9 小时前
【第92期】逻辑回归的分数概率与阈值的实验与排查
人工智能·python·深度学习·机器学习