【深度學習】YOLOv8訓練,交通燈目標檢測

文章目錄

  • 一、數據處理
  • 二、環境
  • 三、訓練

一、數據處理

在這里插入圖片描述

import traceback
import xml.etree.ElementTree as ET
import os
import shutil
import random
import cv2
import numpy as np
from tqdm import tqdmdef convert_annotation_to_list(xml_filepath, size_width, size_height, classes):in_file = open(xml_filepath, encoding='UTF-8')tree = ET.parse(in_file)root = tree.getroot()# size = root.find('size')# size_width = int(size.find('width').text)# size_height = int(size.find('height').text)yolo_annotations = []# if size_width == 0 or size_height == 0:for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continuecls_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)]# 標注越界修正if b[1] > size_width:b[1] = size_widthif b[3] > size_height:b[3] = size_heighttxt_data = [((b[0] + b[1]) / 2.0) / size_width, ((b[2] + b[3]) / 2.0) / size_height,(b[1] - b[0]) / size_width, (b[3] - b[2]) / size_height]# 標注越界修正if txt_data[0] > 1:txt_data[0] = 1if txt_data[1] > 1:txt_data[1] = 1if txt_data[2] > 1:txt_data[2] = 1if txt_data[3] > 1:txt_data[3] = 1yolo_annotations.append(f"{cls_id} {' '.join([str(round(a, 6)) for a in txt_data])}")in_file.close()return yolo_annotationsdef main():classes = ["red", "green", "yellow", "off"]root = r"/ssd/xiedong/lightyolov5"img_path_1 = os.path.join(root, "Traffic-Lights-Dataset-Domestic/JPEGImages")xml_path_1 = os.path.join(root, "Traffic-Lights-Dataset-Domestic/Annotations")img_path_2 = os.path.join(root, "Traffic-Lights-Dataset-Foreign/JPEGImages")xml_path_2 = os.path.join(root, "Traffic-Lights-Dataset-Foreign/Annotations")dst_yolo_root = os.path.join(root, "Traffic-Lights-Dataset-YOLO")dst_yolo_root_img = os.path.join(dst_yolo_root, "images")os.makedirs(dst_yolo_root_img, exist_ok=True)dst_yolo_root_txt = os.path.join(dst_yolo_root, "labels")os.makedirs(dst_yolo_root_txt, exist_ok=True)index = 0img_path_1_files = os.listdir(img_path_1)xml_path_1_files = os.listdir(xml_path_1)for img_id in tqdm(img_path_1_files):# 右邊的.之前的部分xml_id = img_id.split(".")[0] + ".xml"if xml_id in xml_path_1_files:try:new_name = f"{index:06d}.jpg"img = cv2.imdecode(np.fromfile(os.path.join(img_path_1, img_id), dtype=np.uint8), 1)  # img是矩陣cv2.imwrite(os.path.join(dst_yolo_root_img, new_name), img)new_txt_name = f"{index:06d}.txt"yolo_annotations = convert_annotation_to_list(os.path.join(xml_path_1, img_id[:-4] + ".xml"),img.shape[1],img.shape[0],classes)with open(os.path.join(dst_yolo_root_txt, new_txt_name), 'w') as f:f.write('\n'.join(yolo_annotations))index += 1except:traceback.print_exc()img_path_1_files = os.listdir(img_path_2)xml_path_1_files = os.listdir(xml_path_2)for img_id in tqdm(img_path_1_files):# 右邊的.之前的部分xml_id = img_id.split(".")[0] + ".xml"if xml_id in xml_path_1_files:try:new_name = f"{index:06d}.jpg"img = cv2.imdecode(np.fromfile(os.path.join(img_path_2, img_id), dtype=np.uint8), 1)  # img是矩陣cv2.imwrite(os.path.join(dst_yolo_root_img, new_name), img)new_txt_name = f"{index:06d}.txt"yolo_annotations = convert_annotation_to_list(os.path.join(xml_path_2, img_id[:-4] + ".xml"),img.shape[1],img.shape[0],classes)with open(os.path.join(dst_yolo_root_txt, new_txt_name), 'w') as f:f.write('\n'.join(yolo_annotations))index += 1except:traceback.print_exc()if __name__ == '__main__':main()

二、環境

conda create -n py310_yolo8 python=3.10 -yconda activate py310_yolo8conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=11.8 -c pytorch -c nvidiapip install ultralytics

data.yaml

path: /ssd/xiedong/lightyolov5/Traffic-Lights-Dataset-YOLO/
train: images
val: images
test: # test images (optional)# Classes
names:0: 'red'1: 'green'2: 'yellow'3: 'off'

三、訓練

教程:

https://docs.ultralytics.com/modes/train/#comet

新建訓練代碼文件train.py:

from ultralytics import YOLO# Load a model
model = YOLO("yolov8s.pt")  # load a pretrained model (recommended for training)# Train the model with 2 GPUs
results = model.train(data="data.yaml", epochs=100, imgsz=640, device=[0, 1, 2, 3], batch=128)

開啟訓練:

python -m torch.distributed.run --nproc_per_node 4 train.py

結果會存在這里:

在這里插入圖片描述
訓練截圖:

在這里插入圖片描述
數據分布:

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/13973.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/13973.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/13973.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

海山數據庫(He3DB)代理ProxySQL使用詳解:(二)功能實測

讀寫分離實測 ProxySQL官方demo演示了三種讀寫分離的方式:使用不同的端口進行讀寫分離、使用正則表達式進行通用的讀寫分離、使用正則和digest進行更智能的讀寫分離。最后一種是針對特定業務進行的優化調整,也可將其歸結為第二種方式,下邊分…

MySQL備份與日志練習

1、創建對mysql數據庫test1的定時備份任務,頻率是每周一的2點 create database test1;crond -e0 2 * * 1 mysqldump -u root -pAdmin123 --databases test1 > /opt/test1.sql2、test1中有t1、t2、t3三張表,要求只備份t2這張表 mysqldump -u root -pA…

Python 機器學習 基礎 之 數據表示與特征工程 【單變量非線性變換 / 自動化特征選擇/利用專家知識】的簡單說明

Python 機器學習 基礎 之 數據表示與特征工程 【單變量非線性變換 / 自動化特征選擇/利用專家知識】的簡單說明 目錄 Python 機器學習 基礎 之 數據表示與特征工程 【單變量非線性變換 / 自動化特征選擇/利用專家知識】的簡單說明 一、簡單介紹 二、單變量非線性變換 三、自…

知識圖譜數據預處理筆記

知識圖譜數據預處理筆記 0. 引言1. 筆記1-1. \的轉義1-2. 特殊符號的清理1-3. 檢查結尾是否正常1-4. 檢查<>是否存在1-5. 兩端空格的清理1-6. 檢查object內容長時是否以<開始 0. 引言 最近學習知識圖譜&#xff0c;發現數據有很多問題&#xff0c;這篇筆記記錄遇到的…

軟件設計師備考筆記(九):數據庫技術基礎

文章目錄 一、基本概念二、數據模型&#xff08;一&#xff09;基本概念&#xff08;二&#xff09;E-R模型&#xff08;三&#xff09;數據模型 三、關系代數&#xff08;一&#xff09;關系數據庫的基本概念&#xff08;二&#xff09;五種基本的關系代數運算&#xff08;三&…

React hooks - forwardRef+useImperativeHandle

forwardRefuseImperativeHandle React.forwardRef用法useImperativeHandle用法第三個參數的用法 React.forwardRef與useImperativeHandle配合使用注意事項 React.forwardRef用法 1.創建一個 能夠接受到ref屬性的React 組件。 ref 用來獲取實例&#xff0c;但函數組件不存在實例…

bugku 網絡安全事件應急響應

開啟靶場&#xff1a; 開始實驗&#xff1a; 使用Xshell登錄服務器&#xff0c;賬號及密碼如上圖。 1、提交攻擊者的IP地址 WP: 找到服務器日志路徑&#xff0c;通常是在/var/log/&#xff0c;使用cd /var/log/&#xff0c;ls查看此路徑下的文件. 找到nginx文件夾。 進入ng…

hyperopt、optuna、gridsearch、randomsearch自動調參

開始使?hyperopt進??動調參 algo partial(tpe.suggest, n_startup_jobs1) best fmin(lightgbm_factory, space, algoalgo, max_evals20, pass_expr_memo_ctrlNone) RMSE lightgbm_factory(best) print(‘best :’, best) print(‘best param after transform :’) argsD…

【Jenkins】Centos7安裝Jenkins(環境:JDK11,tomcat9,maven3.8)

目錄 Jenkins部署環境Maven安裝1.上傳安裝包2.解壓3.配置Maven環境變量4.使配置文件立即生效5.校驗Maven安裝6.Maven配置阿里云倉庫7.Maven配置依賴下載位置 Git安裝安裝監測安裝 JDK17安裝1.查看舊版本JDK2.卸載舊版本JDK3.查看是否卸載干凈4.創建java目錄5.下載JDK11安裝包6.…

“開源與閉源大模型:數據隱私、商業應用與社區參與的多維比較“

開源大模型和閉源大模型各有其優勢和局限&#xff0c;它們在數據隱私、商業應用和社區參與方面的表現也各有不同。以下是對這三個方面進行的分析&#xff1a; 方向一&#xff1a;數據隱私 開源大模型&#xff1a; 優點&#xff1a;開源模型通常允許用戶和開發者查看和修改代…

Excel中Lookup函數

#Excel查找函數最常用的是Vlookup&#xff0c;而且是經常用其精確查找。Lookup函數的強大之處在于其“二分法”的原理。 LOOKUP&#xff08;查找值&#xff0c;查找區域&#xff08;Vector/Array&#xff09;&#xff0c;[返回結果區域]&#xff09; 為什么查找區域必須升序/…

一種處理checked exception的方法

一種處理checked exception的方法 在網上看到的一種處理異常的方法 public abstract class Try<V> {private Try() {}public abstract Boolean isSuccess();public abstract Boolean isFailure();public abstract void throwException();public abstract Throwable getMe…

【UE HTTP】“BlueprintHTTP Server - A Web Server for Unreal Engine”插件使用記錄

1. 在商城中下載“BlueprintHTTP Server - A Web Server for Unreal Engine”插件 該插件的主要功能有如下3點&#xff1a; &#xff08;1&#xff09;監聽客戶端請求。 &#xff08;2&#xff09;可以將文件直接從Unreal Engine應用程序提供到Web。 &#xff08;3&#xff…

Antd Vue項目引入TailwindCss之后出現svg icon下移,布局中的問題解決方案

目錄 1. 現象&#xff1a; 2. 原因分析&#xff1a; 3. 解決方案&#xff1a; 寫法一&#xff1a;擴展Preflight 寫法二&#xff1a; 4. 禁用 Preflight 1. 現象&#xff1a; Antd Vue項目引入TailwindCss之后出現svg icon下移&#xff0c;不能對齊顯示的情況&#xff0…

k8s筆記 | Prometheus安裝

kube-prometheus 基于github安裝 選擇對應的版本 這里選擇 https://github.com/prometheus-operator/kube-prometheus/tree/release-0.11 下載修改為國內鏡像源 image: quay.io 改為 quay.mirrors.ustc.edu.cn image: k8s.gcr.io 改為 lank8s.cn 創建 prometheus-ingres…

在AndroidStudio創建虛擬手機DUB-AI20

1.DUB-AI20介紹 DUB-AL20是華為暢享9全網通機型。 華為暢享9采用基于Android 8.1定制的EMUI 8.2系統&#xff0c;最大的亮點是配置了1300萬AI雙攝、4000mAh大電池以及AI人臉識別功能&#xff0c;支持熄屏快拍、笑臉抓拍、聲控拍照、手勢拍照等特色的拍照功能&#xff0c;支持移…

Windows安裝mingw32/w64

1.下載 MinGW-w64 WinLibs - GCCMinGW-w64 compiler for Windows Releases niXman/mingw-builds-binaries (github.com) MinGW-w64、UCRT 和 MSVCRT 是 Windows 平臺上常用的 C/C 運行庫&#xff0c;它們有以下不同點&#xff1a; MinGW-w64&#xff1a;是一個基于 GCC 的…

Edge瀏覽器報錯:Ref A Ref B: Ref C

今天發現微軟Edge瀏覽器非常頻繁的出現以下報錯&#xff1a;Ref A: 0BF6B9E03845450C8E6A6C31006AD7B9 Ref B: BJ1EDGE1116 Ref C: 2024-05-23T12:41:30Z 通過搜索發現用如下問題解決&#xff1a; 1.打開Edge瀏覽器 2.進入設置選項 3.找到隱私、搜索和服務 4.關閉跟蹤防護后面…

【數據結構】【C語言】堆~動畫超詳細解讀!

目錄 1 什么是堆1.1 堆的邏輯結構和物理結構1.2 堆的訪問1.3 堆為什么物理結構上要用數組?1.4 堆數據上的特點 2 堆的實現2.1 堆類型定義2.2 需要實現的接口2.3 初始化堆2.4 銷毀堆2.5 堆判空2.6 交換函數2.7 向上調整(小堆)2.8 向下調整(小堆)2.9 堆插入2.10 堆刪除2.11 //堆…

微服務項目收獲和總結---第2,3天(分庫分表思想,文章業務)

①分庫分表思想 文章表一對一為什么要拆分&#xff1f;因為文章的內容會非常大&#xff0c;查詢效率會很低&#xff0c;我們經常操作文章的基本信息&#xff0c;不會很經常查詢文章內容。充分發揮高頻數據的操作效率。 ②freemarker和minIO 由于文章內容數據量過大&#xff0c…