目錄
第一步:下載YOLOv5代碼,并配置環(huán)境,測(cè)試一下是否有無環(huán)境問題
第二步:數(shù)據(jù)集格式的轉(zhuǎn)換與劃分
第三步:修改配置文件,準(zhǔn)備訓(xùn)練
第一步:下載YOLOv5代碼,并配置環(huán)境,測(cè)試一下是否有無環(huán)境問題
yolov5_5.0源代碼開源地址:
1.源代碼下載后右擊選擇PyCharm打開,等待一會(huì),會(huì)彈出一個(gè)要你配置環(huán)境的對(duì)話框,關(guān)掉它,我們自己配置環(huán)境。
配置環(huán)境: 點(diǎn)擊【文件】->【設(shè)置】->【Python解釋器】,選擇相應(yīng)的環(huán)境【我的是:pytorch】
配置要有個(gè)一兩分鐘,等待一下
2.找到【detect.py】,右擊運(yùn)行,報(bào)錯(cuò)如下:
原因:沒有放權(quán)重文件進(jìn)去,下載地址:權(quán)重地址
解決方法:下載后放到項(xiàng)目文件中去。
3.再次運(yùn)行【detect.py】結(jié)果:在D:\DeepLearning\projectone\yolov5-5.0\runs\detect\exp2中生成了預(yù)測(cè)的結(jié)果圖片,但是沒有預(yù)測(cè)框?????
?解決方法1:在53行添加如下代碼:
cudnn.benchmark = True
添加后就是這樣的:
然后再運(yùn)行【detect.py】,結(jié)果如下,有了預(yù)測(cè)框,但是出現(xiàn)的問題就是檢測(cè)速度變慢了,具體也不知道為啥,有懂得小伙伴可以留言吶!!
解決方法2:在【detect.py】,定位到32行,注釋掉原來的代碼,更改為
half = False
結(jié)果:預(yù)測(cè)框就出現(xiàn)了?。。?!
經(jīng)過以上測(cè)試,說明我們得環(huán)境是沒有問題的,可以進(jìn)行下一步啦。
---------------------------------------------------手動(dòng)分割線---------------------------------------------------------------
第二步:數(shù)據(jù)集格式的轉(zhuǎn)換與劃分
參考博客:
(70條消息) 使用Yolov5訓(xùn)練自己制作的數(shù)據(jù)集,快速上手_佐咖的博客-CSDN博客_yolov5數(shù)據(jù)集格式
我使用的是Make Sense在線標(biāo)注,還是挺方便的,不過要注意一次不要導(dǎo)入太多圖片,因?yàn)槭窃诰€網(wǎng)頁上的操作,要是誤關(guān)了網(wǎng)頁,就要重來了。。。使用很簡(jiǎn)單,這里不贅述。
標(biāo)注完后得到一份壓縮包:
解壓后得到xml文件:
下面開始對(duì)標(biāo)注的格式進(jìn)行轉(zhuǎn)換,并劃分?jǐn)?shù)據(jù)集,這里是VOC格式
1.首先創(chuàng)建VOC格式數(shù)據(jù)集文件格式:先對(duì)格式做個(gè)說明:這里是劃分前的文件夾形式,自己按照如下格式創(chuàng)建好文件夾:(說明:dataset是我D盤下的一個(gè)文件夾,這里自己定,我沒有把VOCdevkit數(shù)據(jù)集文件夾放到y(tǒng)olo項(xiàng)目根目錄中)
?在創(chuàng)建好文件夾后,將圖片放入JPEGImages,將makesense導(dǎo)出的xml文件放入Annotations。
2.轉(zhuǎn)換腳本:主要修改一下路徑
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copyfile
classes = ["cat","dog","fox"] ## 這里要寫好標(biāo)簽對(duì)應(yīng)的類
TRAIN_RATIO = 80 # 表示將數(shù)據(jù)集劃分為訓(xùn)練集和驗(yàn)證集,按照2:8比例來的
def clear_hidden_files(path):
dir_list = os.listdir(path)
for i in dir_list:
abspath = os.path.join(os.path.abspath(path), i)
if os.path.isfile(abspath):
if i.startswith("._"):
os.remove(abspath)
else:
clear_hidden_files(abspath)
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('D:/DeepLearning/dataset/VOCdevkit/VOC2022/Annotations/%s.xml' % image_id)
out_file = open('D:/DeepLearning/dataset/VOCdevkit/VOC2022/YOLOLabels/%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')
in_file.close()
out_file.close()
wd = os.getcwd()
wd = os.getcwd()
data_base_dir = os.path.join(wd, "D:/DeepLearning/dataset/VOCdevkit/")
if not os.path.isdir(data_base_dir):
os.mkdir(data_base_dir)
work_sapce_dir = os.path.join(data_base_dir, "D:/DeepLearning/dataset/VOCdevkit/VOC2022/")
if not os.path.isdir(work_sapce_dir):
os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "D:/DeepLearning/dataset/VOCdevkit/VOC2022/Annotations/")
if not os.path.isdir(annotation_dir):
os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "D:/DeepLearning/dataset/VOCdevkit/VOC2022/JPEGImages/")
if not os.path.isdir(image_dir):
os.mkdir(image_dir)
clear_hidden_files(image_dir)
yolo_labels_dir = os.path.join(work_sapce_dir, "D:/DeepLearning/dataset/VOCdevkit/VOC2022/YOLOLabels/")
if not os.path.isdir(yolo_labels_dir):
os.mkdir(yolo_labels_dir)
clear_hidden_files(yolo_labels_dir)
yolov5_images_dir = os.path.join(data_base_dir, "images/")
if not os.path.isdir(yolov5_images_dir):
os.mkdir(yolov5_images_dir)
clear_hidden_files(yolov5_images_dir)
yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
if not os.path.isdir(yolov5_labels_dir):
os.mkdir(yolov5_labels_dir)
clear_hidden_files(yolov5_labels_dir)
yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
if not os.path.isdir(yolov5_images_train_dir):
os.mkdir(yolov5_images_train_dir)
clear_hidden_files(yolov5_images_train_dir)
yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
if not os.path.isdir(yolov5_images_test_dir):
os.mkdir(yolov5_images_test_dir)
clear_hidden_files(yolov5_images_test_dir)
yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
if not os.path.isdir(yolov5_labels_train_dir):
os.mkdir(yolov5_labels_train_dir)
clear_hidden_files(yolov5_labels_train_dir)
yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
if not os.path.isdir(yolov5_labels_test_dir):
os.mkdir(yolov5_labels_test_dir)
clear_hidden_files(yolov5_labels_test_dir)
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'w')
train_file.close()
test_file.close()
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'a')
list_imgs = os.listdir(image_dir) # list image files
prob = random.randint(1, 100)
print("Probability: %d" % prob)
for i in range(0, len(list_imgs)):
path = os.path.join(image_dir, list_imgs[i])
if os.path.isfile(path):
image_path = image_dir + list_imgs[i]
voc_path = list_imgs[i]
(nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
annotation_name = nameWithoutExtention + '.xml'
annotation_path = os.path.join(annotation_dir, annotation_name)
label_name = nameWithoutExtention + '.txt'
label_path = os.path.join(yolo_labels_dir, label_name)
prob = random.randint(1, 100)
print("Probability: %d" % prob)
if (prob < TRAIN_RATIO): # train dataset
if os.path.exists(annotation_path):
train_file.write(image_path + '\n')
convert_annotation(nameWithoutExtention) # convert label
copyfile(image_path, yolov5_images_train_dir + voc_path)
copyfile(label_path, yolov5_labels_train_dir + label_name)
else: # test dataset
if os.path.exists(annotation_path):
test_file.write(image_path + '\n')
convert_annotation(nameWithoutExtention) # convert label
copyfile(image_path, yolov5_images_test_dir + voc_path)
copyfile(label_path, yolov5_labels_test_dir + label_name)
train_file.close()
test_file.close()
轉(zhuǎn)換后結(jié)果:
1)在YOLOLabels中生成了txt格式的標(biāo)注文件,完成了xml到txt格式的轉(zhuǎn)換;
2)在VOCdevkit下生成了兩個(gè)文件夾Images和labels,分別存放圖片和txt格式的標(biāo)注文件
Images下存放訓(xùn)練集和驗(yàn)證集圖片,比例8:2,labels下存放訓(xùn)練集和驗(yàn)證集的標(biāo)注文件,比例8:2,完成了訓(xùn)練集與驗(yàn)證集的劃分。
具體格式如下:
?第三步:修改配置文件,準(zhǔn)備訓(xùn)練
1.在【data】中,復(fù)制coco.yaml,直接ctrl+c,然后ctrl+v,重命名為【my_obstacle.yaml】,名字可任意,然后保存在【data】下
修改【my_obstacle.yaml】文件內(nèi)容,我這里的訓(xùn)練集驗(yàn)證集路徑不在yolo根目錄下,所以都是寫的絕對(duì)路徑。
2.在【models】文件夾下復(fù)制yolov5s.yaml,重命名為【yolov5s_obstacle.yaml】,名字可任意,然后保存在【models】下
修改類別為3
3.修改【train.py】
1)weights----模型權(quán)重在yolo項(xiàng)目根目錄下
2)cfg----在pycharm左邊目錄欄,右擊【yolov5s_obstacle.yaml】,復(fù)制路徑,來自內(nèi)容根的路徑,粘貼即可
3)data----在pycharm左邊目錄欄,右擊【my_obstacle.yaml】,復(fù)制路徑,來自內(nèi)容根的路徑,粘貼即可
4)epochs----訓(xùn)練輪數(shù),我這里是100輪
5)batch_size----根據(jù)電腦性能修改,我這里只能是1才能跑起來(我的顯卡GTX1650)
6)workers----我一直用的是1,這里我不確定,可以自己嘗試一下
?然后開始運(yùn)行【train.py】
訓(xùn)練過程中輸出的參數(shù):剛好在訓(xùn)練時(shí)寫這篇博客,貼個(gè)圖吧
每次訓(xùn)練都要網(wǎng)上找個(gè)博客看步驟,這次索性自己寫一個(gè),記錄一下
如有錯(cuò)誤請(qǐng)指正!
2022.11.28補(bǔ)充:
今天在改網(wǎng)絡(luò)結(jié)構(gòu),開始訓(xùn)練的時(shí)候會(huì)報(bào)錯(cuò):
RuntimeError: result type Float can‘t be cast to the desired output type long int
解決方法參考:文章來源:http://www.zghlxwxcb.cn/news/detail-666796.html
(75條消息) 一步真實(shí)解決RuntimeError: result type Float can‘t be cast to the desired output type long int_藍(lán)胖胖?的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-666796.html
到了這里,關(guān)于YOLOv5_5.0訓(xùn)練自己的數(shù)據(jù)集 RuntimeError: result type Float can‘t be cast to the desired output type long int的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!