??????本文摘要:基于YOLOv8的鐵路工人安全作業(yè)檢測系統(tǒng),屬于小目標檢測范疇,并闡述了整個數據制作和訓練可視化過程,
?
博主簡介
AI小怪獸,YOLO骨灰級玩家,1)YOLOv5、v7、v8優(yōu)化創(chuàng)新,輕松漲點和模型輕量化;2)目標檢測、語義分割、OCR、分類等技術孵化,賦能智能制造,工業(yè)項目落地經驗豐富;
原創(chuàng)自研系列,?2024年計算機視覺頂會創(chuàng)新點
《YOLOv8原創(chuàng)自研》
《YOLOv5原創(chuàng)自研》
《YOLOv7原創(chuàng)自研》
23年最火系列,內涵80+優(yōu)化改進篇,漲點小能手,助力科研,好評率極高
《YOLOv8魔術師》
?《YOLOv7魔術師》
《YOLOv5/YOLOv7魔術師》
《RT-DETR魔術師》
應用系列篇:
《YOLO小目標檢測》
《深度學習工業(yè)缺陷檢測》
《YOLOv8-Pose關鍵點檢測》
1.YOLOv8介紹
?????????Ultralytics YOLOv8是Ultralytics公司開發(fā)的YOLO目標檢測和圖像分割模型的最新版本。YOLOv8是一種尖端的、最先進的(SOTA)模型,它建立在先前YOLO成功基礎上,并引入了新功能和改進,以進一步提升性能和靈活性。它可以在大型數據集上進行訓練,并且能夠在各種硬件平臺上運行,從CPU到GPU。
具體改進如下:
-
Backbone:使用的依舊是CSP的思想,不過YOLOv5中的C3模塊被替換成了C2f模塊,實現了進一步的輕量化,同時YOLOv8依舊使用了YOLOv5等架構中使用的SPPF模塊;
-
PAN-FPN:毫無疑問YOLOv8依舊使用了PAN的思想,不過通過對比YOLOv5與YOLOv8的結構圖可以看到,YOLOv8將YOLOv5中PAN-FPN上采樣階段中的卷積結構刪除了,同時也將C3模塊替換為了C2f模塊;
-
Decoupled-Head:是不是嗅到了不一樣的味道?是的,YOLOv8走向了Decoupled-Head;
-
Anchor-Free:YOLOv8拋棄了以往的Anchor-Base,使用了Anchor-Free的思想;
-
損失函數:YOLOv8使用VFL Loss作為分類損失,使用DFL Loss+CIOU Loss作為分類損失;
-
樣本匹配:YOLOv8拋棄了以往的IOU匹配或者單邊比例的分配方式,而是使用了Task-Aligned Assigner匹配方式
?
框架圖提供見鏈接:Brief summary of YOLOv8 model structure · Issue #189 · ultralytics/ultralytics · GitHub
2.鐵路工人安全作業(yè)檢測數據集介紹
該數據集用于正確檢測工人、他們的反光背心和安全帽。該數據集有3222張圖片,其中包含三個標簽:工人、反光背心和安全帽。
用途舉例:
- 可以判斷是否有工人正在鐵路上作業(yè);
- 可以判斷工人是否正確佩戴反光背心和安全帽規(guī)范作業(yè)。
部分數據集圖片如下:
下圖可以看出識別對象為小目標檢測?
?
2.1?split_train_val.py
# coding:utf-8
import os
import random
import argparse
parser = argparse.ArgumentParser()
#xml文件的地址,根據自己的數據進行修改 xml一般存放在Annotations下
parser.add_argument('--xml_path', default='Annotations', type=str, help='input xml label path')
#數據集的劃分,地址選擇自己數據下的ImageSets/Main
parser.add_argument('--txt_path', default='ImageSets/Main', type=str, help='output txt label path')
opt = parser.parse_args()
trainval_percent = 0.9
train_percent = 0.8
xmlfilepath = opt.xml_path
txtsavepath = opt.txt_path
total_xml = os.listdir(xmlfilepath)
if not os.path.exists(txtsavepath):
os.makedirs(txtsavepath)
num = len(total_xml)
list_index = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list_index, tv)
train = random.sample(trainval, tr)
file_trainval = open(txtsavepath + '/trainval.txt', 'w')
file_test = open(txtsavepath + '/test.txt', 'w')
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/val.txt', 'w')
for i in list_index:
name = total_xml[i][:-4] + '\n'
if i in trainval:
file_trainval.write(name)
if i in train:
file_train.write(name)
else:
file_val.write(name)
else:
file_test.write(name)
file_trainval.close()
file_train.close()
file_val.close()
file_test.close()
2.2?voc_label.py生成適合YOLOv8訓練的txt
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train','val','test']
classes = ['vest','helmet','worker']
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open('Annotations/%s.xml' % (image_id))
out_file = open('labels/%s.txt' % (image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
print(wd)
for image_set in sets:
if not os.path.exists('labels/'):
os.makedirs('labels/')
image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
list_file = open('%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write('images/%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
3.如何訓練YOLOv8
3.1 配置Railroad.yaml
ps:建議填寫絕對路徑
path: F:/ultralytics-RailroadWorkerDetection/data/Railroad # dataset root dir
train: train.txt # train images (relative to 'path') 118287 images
val: val.txt # val images (relative to 'path') 5000 images
# number of classes
nc: 3
# class names
names:
0: vest
1: helmet
2: worker
3.2 如何訓練
import warnings
warnings.filterwarnings('ignore')
from ultralytics import YOLO
if __name__ == '__main__':
model = YOLO('ultralytics/cfg/models/v8/yolov8.yaml')
#model.load('yolov8n.pt') # loading pretrain weights
model.train(data='data/Railroad/Railroad.yaml',
cache=False,
imgsz=640,
epochs=100,
batch=32,
workers=0,
device='0',
optimizer='SGD', # using SGD
project='runs/train',
name='exp',
)
3.3 訓練可視化結果
F1_curve.png:F1分數與置信度(x軸)之間的關系。F1分數是分類的一個衡量標準,是精確率和召回率的調和平均函數,介于0,1之間。越大越好。
TP:真實為真,預測為真;
FN:真實為真,預測為假;
FP:真實為假,預測為真;
TN:真實為假,預測為假;
精確率(precision)=TP/(TP+FP)
召回率(Recall)=TP/(TP+FN)
F1=2*(精確率*召回率)/(精確率+召回率)
?
?PR_curve.png :PR曲線中的P代表的是precision(精準率),R代表的是recall(召回率),其代表的是精準率與召回率的關系。
?
預測結果:?
?
文章來源:http://www.zghlxwxcb.cn/news/detail-847768.html
關注下方名片點擊關注,源碼獲取途徑。??文章來源地址http://www.zghlxwxcb.cn/news/detail-847768.html
到了這里,關于基于YOLOv8的攝像頭下鐵路工人安全作業(yè)檢測(工人、反光背心和安全帽)系統(tǒng)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!