【深度学习】安全帽检测,目标检测,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
相关推荐
tangjunjun-owen6 分钟前
第四节:GLM-4v-9b模型的tokenizer源码解读
人工智能·glm-4v-9b·多模态大模型教程
冰蓝蓝10 分钟前
深度学习中的注意力机制:解锁智能模型的新视角
人工智能·深度学习
橙子小哥的代码世界18 分钟前
【计算机视觉基础CV-图像分类】01- 从历史源头到深度时代:一文读懂计算机视觉的进化脉络、核心任务与产业蓝图
人工智能·计算机视觉
新加坡内哥谈技术1 小时前
苏黎世联邦理工学院与加州大学伯克利分校推出MaxInfoRL:平衡内在与外在探索的全新强化学习框架
大数据·人工智能·语言模型
fanstuck2 小时前
Prompt提示工程上手指南(七)Prompt编写实战-基于智能客服问答系统下的Prompt编写
人工智能·数据挖掘·openai
lovelin+v175030409662 小时前
安全性升级:API接口在零信任架构下的安全防护策略
大数据·数据库·人工智能·爬虫·数据分析
wydxry2 小时前
LoRA(Low-Rank Adaptation)模型微调
深度学习
唐小旭2 小时前
python3.6搭建pytorch环境
人工智能·pytorch·python
数据分析能量站2 小时前
目标检测-R-CNN
目标检测·r语言·cnn
洛阳泰山2 小时前
MaxKB基于大语言模型和 RAG的开源知识库问答系统的快速部署教程
人工智能·语言模型·开源·rag·maxkb