網(wǎng)站建設(shè)訂單模板下載長(zhǎng)沙seo網(wǎng)站優(yōu)化公司
深度學(xué)習(xí)訓(xùn)練營(yíng)之訓(xùn)練自己的數(shù)據(jù)集
- 原文鏈接
- 環(huán)境介紹
- 準(zhǔn)備好數(shù)據(jù)集
- 劃分?jǐn)?shù)據(jù)集
- 運(yùn)行voc_train.py
- 遇到問(wèn)題
- 完整代碼
- 創(chuàng)建new_data.yaml文件
- 模型訓(xùn)練時(shí)遇到的報(bào)錯(cuò)
- 模型訓(xùn)練
- 結(jié)果可視化
- 參考鏈接
原文鏈接
- 🍨 本文為🔗365天深度學(xué)習(xí)訓(xùn)練營(yíng) 中的學(xué)習(xí)記錄博客
- 🍦 參考文章:365天深度學(xué)習(xí)訓(xùn)練營(yíng)-第Y2周:訓(xùn)練自己的數(shù)據(jù)集
- 🍖 原作者:K同學(xué)啊|接輔導(dǎo)、項(xiàng)目定制
環(huán)境介紹
- 語(yǔ)言環(huán)境:Python3.9.13
- 編譯器:vscode
- 深度學(xué)習(xí)環(huán)境:torch
- 顯卡:NVIDIA GeForce RTX 3070 Laptop GPU
準(zhǔn)備好數(shù)據(jù)集
我這里采用的數(shù)據(jù)集是經(jīng)典的目標(biāo)檢測(cè)算法當(dāng)中的一個(gè)數(shù)據(jù)集,這里附上鏈接:
文件提取:鏈接:https://pan.baidu.com/s/1SuNxOTCgrQlqXWK_cRzCZQ
提取碼:0909
文件夾下內(nèi)容:
劃分?jǐn)?shù)據(jù)集
運(yùn)行如下命令進(jìn)行數(shù)據(jù)集的劃分
python split_train_val.py --xml_path D:\yolov5-master\yolov5-master\my_data\Annotations --txt_path D:\yolov5-master\yolov5-master\my_data\ImageSets\Main
原始應(yīng)該是這樣
python split_train_val.py --xml_path xx --txt_path xx
其中xx的地方根據(jù)相應(yīng)的路徑進(jìn)行更改,需要注意到的是運(yùn)行的路徑是函數(shù)python split_train_val.py
所在的文件夾下進(jìn)行運(yùn)行,否則會(huì)報(bào)錯(cuò)
在彈出的對(duì)話框中進(jìn)行運(yùn)行
可以看到已經(jīng)成功生成了
運(yùn)行voc_train.py
遇到問(wèn)題
遇到如下報(bào)錯(cuò)unsupported operand type(s) for +: 'builtin_function_or_method' and 'str'
這里所說(shuō)的意思應(yīng)該是指不能將type(s)類型的字符加上一個(gè)函數(shù)或者方法,我簡(jiǎn)單查看了一些,是我在定義abs_path
的時(shí)候漏加括號(hào),導(dǎo)致abs_path
的字符類型出現(xiàn)錯(cuò)誤
完整代碼
經(jīng)過(guò)調(diào)試和更改路徑之后得到的代碼如下
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train','val','test']# 20類
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog","horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
abs_path=os.getcwd()
print(abs_path)
# size w,h
# box x-min,x-max,y-min,y-max
def convert(size, box):dw = 1./size[0]dh = 1./size[1]x = (box[0] + box[1])/2.0 -1# 中心點(diǎn)位置y = (box[2] + box[3])/2.0 -1w = box[1] - box[0]h = box[3] - box[2]x = x *dww = w *dwy = y *dhh = h *dh # 全部轉(zhuǎn)化為相對(duì)坐標(biāo)return (x, y, w, h)def convert_annotation(image_id):# 找到2個(gè)同樣的文件in_file = open('Annotations/%s.xml' % (image_id),encoding='UTF-8')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').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1: #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))# 傳入的是w,h 與框框的周邊b1,b2,b3,b4=bif b2>w:b2=wif b4>h:b4=hb=(b1,b2,b3,b4)bb = convert((w, h), b)out_file.write(str(cls_id) +" "+" ".join([str(a) for a in bb]) + '\n')wd = getcwd()for image_set in sets:# ('2012', 'train') 循環(huán)5次# 創(chuàng)建目錄 一次性if not os.path.exists('labels/' ):os.makedirs('labels/')# 圖片的id數(shù)據(jù)image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split()# 結(jié)果寫入這個(gè)文件list_file = open('%s.txt' % (image_set), 'w')for image_id in image_ids:list_file.write(abs_path+'/JPEGImages/%s.jpg\n' % (image_id)) # 補(bǔ)全路徑convert_annotation(image_id)list_file.close()
(記錄一下
:一開(kāi)始這個(gè)最后的JPEGImages沒(méi)加斜杠,導(dǎo)致后續(xù)報(bào)錯(cuò),這里是添加過(guò)后的)
運(yùn)行val_voc.py
可以得到三個(gè)txt的文件,其內(nèi)容如下:
創(chuàng)建new_data.yaml文件
這個(gè)文件的名稱new_data.yaml
是我自己隨便起的一個(gè)名字,大家可以自行更改
模型訓(xùn)練時(shí)遇到的報(bào)錯(cuò)
expected '<document start>', but found '<scalar>'
這里主要的原因是在定義變量的時(shí)候我使用的是=
,但是應(yīng)該用:
錯(cuò)誤圖片:(這里突然發(fā)現(xiàn)這個(gè)mydata當(dāng)中trian和val的名字不一樣,導(dǎo)致后面一系列的錯(cuò)誤,我后面改完之后就行)
更正以后:
models
文件夾下的用于訓(xùn)練的yolov5s.yaml
把這個(gè)原本的分類從80改成20(20是我的數(shù)據(jù)集的類型)
模型訓(xùn)練
運(yùn)行如下命令,開(kāi)始訓(xùn)練
python train.py --img 928 --batch 2 --epoch 10 --data data/new_data.yaml --cfg models/yolov5s.yaml --weights weights/yolov5s.pt
出現(xiàn)以下報(bào)錯(cuò)(沒(méi)有出現(xiàn)就是成功訓(xùn)練啦)
No labels found in D:\yolov5-master\yolov5-master\my_data\train.cache.
在train.py
當(dāng)中更改成絕對(duì)路徑
new_data.yaml也進(jìn)行修改
更改D:\yolov5-master\yolov5-master\utils
當(dāng)中的sa
,sb
的路徑
開(kāi)始運(yùn)行…
結(jié)果可視化
運(yùn)行結(jié)果如下
使用wandb可以看到我們的運(yùn)行結(jié)果的一些可視化
參考鏈接
- yolov5數(shù)據(jù)讀取報(bào)錯(cuò):train: No labels found in /root/yolov5-master/VOCData/dataSet_path/train.cache
- Python編譯報(bào)錯(cuò)的自我記錄