本人寫(xiě)了一鍵制作三種數(shù)據(jù)集的代碼,還帶數(shù)據(jù)增強(qiáng)哦,可聯(lián)系QQ:1781419402獲取,小償!
Yolov8下載地址:GitHub - ultralytics/ultralytics: YOLOv8 ?? in PyTorch > ONNX > CoreML > TFLitexx
下載完成后 按照YOLOv8教程系列:一、使用自定義數(shù)據(jù)集訓(xùn)練YOLOv8模型(詳細(xì)版教程,你只看一篇->調(diào)參攻略),包含環(huán)境搭建/數(shù)據(jù)準(zhǔn)備/模型訓(xùn)練/預(yù)測(cè)/驗(yàn)證/導(dǎo)出等_Zhijun.li@Studio的博客-CSDN博客
所寫(xiě)的進(jìn)行目標(biāo)檢測(cè)的自己數(shù)據(jù)集的訓(xùn)練即可。
但是網(wǎng)上對(duì)于yolov8進(jìn)行自己數(shù)據(jù)集的分割訓(xùn)練的教程較少?,不會(huì)的可以看本文。
首先就是數(shù)據(jù)集的格式:(我自己使用的是這樣,可以訓(xùn)練,參照coco數(shù)據(jù)集)
其中images/train2017中是訓(xùn)練圖片原圖,labels/train2017中是轉(zhuǎn)換出來(lái)的yolo格式txt標(biāo)簽文本文件,train2017.cache是自動(dòng)生成的,不用管。
其次,就是配置文件 ,在下圖所示位置,并且按照右下所示進(jìn)行修改:
?
此處修改完后,就是修改參數(shù)配置文件?(cdg里的default.yaml),要做分割任務(wù)首先是將task設(shè)置為segment,其實(shí)就是model改為yolov8n-seg.py(分割預(yù)訓(xùn)練權(quán)重),最后就是將data修改為自己數(shù)據(jù)集所在文件夾的路徑就可以開(kāi)始訓(xùn)練。
?如果報(bào)錯(cuò)runtimeerror: sizes of tensors must match except in dimension 1. expected size 2 but got size 0 for tensor number 1 in the list.:
? ? ? ? 原因是?torch.cat()函數(shù)中參數(shù)的維度不對(duì)應(yīng),如果數(shù)據(jù)集沒(méi)有問(wèn)題,那就是丟失信息了。
? ? ? ? 那你使用的數(shù)據(jù)集中l(wèi)abel里的txt文件的內(nèi)容,大概率是轉(zhuǎn)出來(lái)的是只有對(duì)應(yīng)類(lèi)別索引和四個(gè)點(diǎn)坐標(biāo)(也就是rectangle格式),只能做檢測(cè),不可做分割,會(huì)丟失segment信息。
? ? ? ? 可使用以下代碼進(jìn)行轉(zhuǎn)換數(shù)據(jù)集:
# 處理labelme多邊形矩陣的標(biāo)注 json轉(zhuǎn)化txt,提取點(diǎn)
import json
import os
name2id = {'chip': 0} # 修改你的類(lèi)別并且賦與index
def decode_json(json_floder_path, txt_outer_path, json_name):
txt_name = txt_outer_path + json_name[:-5] + '.txt'
with open(txt_name, 'a') as f:
json_path = os.path.join(json_floder_path, json_name)
data = json.load(open(json_path, 'r', encoding='gb2312', errors='ignore'))
img_w = data['imageWidth']
img_h = data['imageHeight']
isshape_type = data['shapes'][0]['shape_type']
print(isshape_type)
dw = 1. / (img_w)
dh = 1. / (img_h)
for i in data['shapes']:
label_name = i['label']
if (i['shape_type'] == 'polygon'):
point = []
for lk in range(len(i['points'])):
x = float(i['points'][lk][0])
y = float(i['points'][lk][1])
point_x = x * dw
point_y = y * dh
point.append(point_x)
point.append(point_y)
f.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in point]) + '\n')
f.close()
if __name__ == "__main__":
json_floder_path = r'F:\Workprojects\front_corner_drop\train_data\json' # 存放json的文件夾的絕對(duì)路徑
txt_outer_path = r'F:\Workprojects\front_corner_drop\labels\segment/' # 存放txt的文件夾絕對(duì)路徑
json_names = os.listdir(json_floder_path)
flagcount = 0
for json_name in json_names:
decode_json(json_floder_path, txt_outer_path, json_name)
flagcount += 1
print('-----------轉(zhuǎn)化完畢------------')
要是做分類(lèi)訓(xùn)練的話(huà),自制數(shù)據(jù)集格式如圖所示:
????????????????????????????????????????????????
其中,train和val的里面只用放所有的訓(xùn)練集圖片,error和normal是我當(dāng)前需要的分類(lèi)類(lèi)別,其中放置對(duì)應(yīng)的類(lèi)別的圖片,根據(jù)自身需求創(chuàng)建類(lèi)別文件夾,放入對(duì)應(yīng)圖片即可。
接著修改yaml文件:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-415990.html
?將類(lèi)別和類(lèi)別數(shù)量和數(shù)據(jù)集路徑修改好即可訓(xùn)練。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-415990.html
到了這里,關(guān)于YOLOv8檢測(cè)、分割和分類(lèi)訓(xùn)練自己數(shù)據(jù)集的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!