【深度学习】安全帽检测,目标检测,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
相关推荐
千天夜7 分钟前
激活函数解析:神经网络背后的“驱动力”
人工智能·深度学习·神经网络
大数据面试宝典8 分钟前
用AI来写SQL:让ChatGPT成为你的数据库助手
数据库·人工智能·chatgpt
封步宇AIGC13 分钟前
量化交易系统开发-实时行情自动化交易-3.4.1.2.A股交易数据
人工智能·python·机器学习·数据挖掘
m0_5236742115 分钟前
技术前沿:从强化学习到Prompt Engineering,业务流程管理的创新之路
人工智能·深度学习·目标检测·机器学习·语言模型·自然语言处理·数据挖掘
HappyAcmen25 分钟前
IDEA部署AI代写插件
java·人工智能·intellij-idea
噜噜噜噜鲁先森1 小时前
看懂本文,入门神经网络Neural Network
人工智能
InheritGuo1 小时前
It’s All About Your Sketch: Democratising Sketch Control in Diffusion Models
人工智能·计算机视觉·sketch
weixin_307779132 小时前
证明存在常数c, C > 0,使得在一系列特定条件下,某个特定投资时刻出现的概率与天数的对数成反比
人工智能·算法·机器学习
封步宇AIGC2 小时前
量化交易系统开发-实时行情自动化交易-3.4.1.6.A股宏观经济数据
人工智能·python·机器学习·数据挖掘
Jack黄从零学c++2 小时前
opencv(c++)图像的灰度转换
c++·人工智能·opencv