纯小白通过服务器搭建yolov5环境训练coco数据集
后知后覺的我,終于明白了之前師哥說的簡簡單單的幾句話。
由于用的公司電腦,沒有GPU,沒有辦法搭建yolo環(huán)境,故沒法訓(xùn)練模型。
借助服務(wù)器,獲取所需的GPU。通過vs code連接服務(wù)器,剩余操作其實和本地差不多。(如果還是不懂,可以了解linux的基本結(jié)構(gòu))
?
搭環(huán)境!!簡單幾個字其實一直不太明白,知道看到上述虛擬環(huán)境的博文,才算有了一點點清晰的認(rèn)識,每個算法模型都在自己的“生存條件”,即環(huán)境
在該環(huán)境下算法才能正常運行。那訪問服務(wù)器,滿足GPU的條件后,就開始正式搭建環(huán)境,而該服務(wù)器下,其實師姐師哥們已經(jīng)搭建好了一個yolov5的虛擬環(huán)境
我只需要激活該環(huán)境,在該環(huán)境下運行yolov5算法模型即可!!!
而為了不破壞師姐師哥們的環(huán)境,我可以自己創(chuàng)建一個新的虛擬環(huán)境,在里面下載安裝包,搭建環(huán)境也可以!!!
?
#服務(wù)器
由于電腦沒有獨立顯卡,所以無法安裝cuda等相關(guān)軟件以搭建yolo環(huán)境,故需要利用公司的服務(wù)器。
?
- 服務(wù)器和電腦的差別
https://zhuanlan.zhihu.com/p/162938209
https://zhuanlan.zhihu.com/p/166335427
從用途來看;服務(wù)器是主機通過安裝各種程序,長期穩(wěn)定接受互聯(lián)網(wǎng)訪問,提供各式各樣服務(wù)的工具,長期、穩(wěn)定、開放是它顯著的特點。
從安裝系統(tǒng)來看;服務(wù)器有著專門的系統(tǒng),比如Windows系統(tǒng)有Windows2003/2008/2012/2016/2019各種版本,Linux系統(tǒng)也有Ubuntu、centos、redhat等版本,還有專門用來虛擬化的VMware、Virutozzo等,這些系統(tǒng)強化了主機作為服務(wù)器的性能,能夠更快更穩(wěn)定運行,節(jié)約系統(tǒng)資源。
?
- 如何通過vs code連接服務(wù)器
從vs code連接服務(wù)器的教程如下:
https://jingyan.baidu.com/article/ca41422ff94e1c5faf99ed7e.html
其中有一步比較疑惑,即輸入ssh連接命令接口,格式為ssh user@hostname -p port,對應(yīng)配置是
可以通過兩種方式方式連接:
連接成功后,直接選擇第一個配置,成功連接后,輸入密碼即可。
?
- 連接服務(wù)器以后如何使用
(1)了解linux系統(tǒng)的基礎(chǔ)結(jié)構(gòu)
https://www.cnblogs.com/zx125/p/11557483.html
?
補充知識點:
①root賬戶和name賬戶
root用戶是系統(tǒng)中唯一的超級管理員,它具有等同于操作系統(tǒng)的權(quán)限。一些需要root權(quán)限的應(yīng)用,譬如廣告阻擋,是需要root權(quán)限的。
root比windows的系統(tǒng)管理員的能力更大,足以把整個系統(tǒng)的大部分文件刪掉,導(dǎo)致系統(tǒng)完全毀壞,不能再次使用。用root進行不當(dāng)?shù)牟僮魇窍喈?dāng)危險的,輕微的可以死機,嚴(yán)重的甚至不能開機。
在Unix、Linux及Android中,除非確實需要,一般情況下都不推薦使用root,最好單獨建立一個普通的用戶,作為日常之用。
?
~是什么?
其實就是根目錄下的個人文件夾
如果以root賬號登陸 ,~ 是 /root/
如果以 name 登陸 ,~ 是 /home/name/
可知對應(yīng)的是/home/name/data/lbb 其中name是個人文件夾,是一個用戶
?
②鏡像(Mirroring)
鏡像是一種文件存儲形式,是冗余的一種類型,一個磁盤上的數(shù)據(jù)在另一個磁盤上存在一個完全相同的副本即為鏡像。
?
(2)了解conda的指令
https://www.jianshu.com/p/f0602213f400
?
(3)連接服務(wù)以后如何操作?
①成功連接
?
②點開文件夾,新建py文件,編寫代碼
?
③輸入命令 nvidia-smi 查看英偉達顯卡信息以及顯卡驅(qū)動和cuda信息
?
④如何將本地的文件上傳到服務(wù)器中
不用復(fù)雜的代碼,直接拖拽就行
?
#搭建yolo的環(huán)境
明白服務(wù)器為何物后,我們需要新建虛擬環(huán)境yolov5,在其中配置好yolov5環(huán)境需要的包
師姐已經(jīng)在服務(wù)器中把環(huán)境搭好了,所以我就省事了,只需要將她搭建好的環(huán)境復(fù)制到我的虛擬環(huán)境中
?
搭建yolo環(huán)境,明白何為搭建環(huán)境和虛擬環(huán)境
?
虛擬環(huán)境:https://www.cnblogs.com/yuehouse/p/10239195.html
?
了解conda,在虛擬環(huán)境中運行
由于通過了服務(wù)器,故需要掌握虛擬環(huán)境的使用。
使用conda創(chuàng)建虛擬環(huán)境:https://blog.csdn.net/qq_45154565/article/details/109326095
?
用到的指令:
查看虛擬環(huán)境:conda info --envs
刪除虛擬環(huán)境:conda remove -n env?--all (env為虛擬環(huán)境的名字)
復(fù)制虛擬環(huán)境:conda create -n env2?--clone env1?(env1是要復(fù)制的環(huán)境,env2是創(chuàng)建的環(huán)境)
查看復(fù)制后的虛擬環(huán)境安裝包:conda list
對比虛擬環(huán)境安裝包和requirement.txt的內(nèi)容,看是否需要更新或添加
ps:復(fù)制yolov5的環(huán)境至lbb虛擬環(huán)境中,檢查lbb虛擬環(huán)境是否配置完整,然后開始用正式訓(xùn)練
?
#正題:用yolov5訓(xùn)練自己的數(shù)據(jù)集
?
以下三個鏈接是同一個博主,可以快速了解全過程,并且非常詳細
https://blog.csdn.net/CSDN1621564706/article/details/115295208?spm=1001.2014.3001.5501?(搭建yolo環(huán)境)
https://blog.csdn.net/CSDN1621564706/article/details/114152179?spm=1001.2014.3001.5501
https://blog.csdn.net/CSDN1621564706/article/details/115311347?spm=1001.2014.3001.550? ??
?
以下三個鏈接是覺得非常完整,可以相互對比看的內(nèi)容,我的代碼也是參考以下三個鏈接中的。
https://blog.csdn.net/weixin_48994268/article/details/115282688
https://blog.csdn.net/a_cheng_/article/details/111401500
https://blog.csdn.net/qq_36756866/article/details/109111065?spm=1001.2014.3001.5501
?
- 制作數(shù)據(jù)集
coco文件夾是數(shù)據(jù)集制作,yolov5是源代碼,接下來講述如何制作數(shù)據(jù)集
在coco下新建images和labels兩個文件夾,分別放置圖片和標(biāo)注文件
然后同級目錄下新建兩個py文件
import os import random trainval_percent = 0.1 train_percent = 0.9 xmlfilepath = 'coco/images' txtsavepath = 'coco/ImageSets' total_xml = os.listdir(xmlfilepath) num = len(total_xml) list = range(num) tv = int(num * trainval_percent) tr = int(tv * train_percent) trainval = random.sample(list, tv) #從所有l(wèi)ist中返回tv個數(shù)量的項目 train = random.sample(trainval, tr) if not os.path.exists('coco/ImageSets/'):os.makedirs('coco/ImageSets/') ftrainval = open('coco/ImageSets/trainval.txt', 'w') ftest = open('coco/ImageSets/test.txt', 'w') ftrain = open('coco/ImageSets/train.txt', 'w') fval = open('coco/ImageSets/val.txt', 'w') for i in list:name = total_xml[i][:-4] + '\n'if i in trainval:ftrainval.write(name)if i in train:ftest.write(name)else:fval.write(name)else:ftrain.write(name) ftrainval.close() ftrain.close() fval.close() ftest.close()該代碼會生成以下文件夾
?
另一個py文件
import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from os.path import join sets = ['train', 'test','val'] classes = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light','fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow','elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee','skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard','tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple','sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch','potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone','microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear','hair drier', 'toothbrush'] def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)#若目前標(biāo)注文件已經(jīng)是txt,不是xml,可直接講以下兩處地方注釋掉 #第一處代碼 ''' def convert_annotation(image_id):in_file = open('coco/all_annotations/%s.xml' % (image_id))out_file = open('coco/all_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: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))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('coco/labels/'):os.makedirs('coco/labels/')image_ids = open('coco/ImageSets/%s.txt' % (image_set)).read().strip().split()list_file = open('coco/%s.txt' % (image_set), 'w')for image_id in image_ids:list_file.write('/home/cv/data/lbb/coco/images/%s.jpg\n' % (image_id))#convert_annotation(image_id) 第二處注釋代碼list_file.close()該代碼會生成以下文件(沒有 .cache文件,這兩個文件是后來生成的)
?
此時數(shù)據(jù)集變成如下所示(沒有 .cache文件,這兩個文件是后來生成的)
到此為止,數(shù)據(jù)集制作完畢,在這里強調(diào)一點:檢查一下train.txt里的圖片路徑是否是絕對路徑,這樣不易出錯。
?
- 訓(xùn)練數(shù)據(jù)集
訓(xùn)練數(shù)據(jù)集的操作跟以上分享的三個網(wǎng)址保持一致。
修改data里的yaml文件,models的種類數(shù)量,train.py的對應(yīng)參數(shù)
需要注意的點:提前下載好yolov5s.pt放置在weights的文件夾下,縮短訓(xùn)練時長(下載地址以上分享的三個網(wǎng)址中有)
然后輸入指令:python mytest.py --img 640 --batch 12 --epochs 100 --data data/mytest.yaml --cfg models/yolov5s.yaml --weights weights/yolov5s.pt?
?
- 測試detect.py
訓(xùn)練完以后可以看到在runs/train/exp/weights下生成了訓(xùn)練好的權(quán)重文件best.pt和last.pt,接著我們用訓(xùn)練好的權(quán)重文件進行測試,打開detect.py文件,修改對應(yīng)兩個變量(操作跟以上分享的三個網(wǎng)址保持一致)
輸入指令:python detect.py --weights runs/train/exp3/weights/best.pt --source data/images/ --device 0 --save-txt
需要注意的點:測試的圖片不要和訓(xùn)練圖片重復(fù)
?
- 查看結(jié)果
結(jié)果圖片會在run/detect下的exp文件夾中生成
?
#在該過程中的遇到的小問題
(1)代碼出現(xiàn)紅色波浪線:語法錯誤(不小心空格)或代碼不嚴(yán)謹(jǐn),一般很難檢查出來,我都是重新復(fù)制粘貼
(2)正式訓(xùn)練數(shù)據(jù)集之前,需要先制作自己的數(shù)據(jù)集,先對數(shù)據(jù)進行分類工作。
(3)yolov5的代碼放在跟數(shù)據(jù)集同級目錄下
注意:修改yolov5代碼的data下的文件時,路徑用絕對路徑,以防出錯,制作數(shù)據(jù)集時,代碼里的路徑也最好是絕對路徑
(4)可以先下載權(quán)重模型,放置weights文件夾下,減少訓(xùn)練時長
(5)用指令執(zhí)行train.py文件
python mytest.py --img 640 --batch 12 --epochs 100 --data data/mytest.yaml --cfg models/yolov5s.yaml --weights weights/yolov5s.pt?
(6)在運行過程中出現(xiàn)問題,于是pip install wandb
下載后又出現(xiàn)問題,如下:
輸入:3。
(7)測試時不要用訓(xùn)練的圖片
(8)用該地址下載的工具解析coco數(shù)據(jù)集:
https://blog.csdn.net/flyfish1986/article/details/115485209?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的纯小白通过服务器搭建yolov5环境训练coco数据集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac电脑系统如何查看文件MD5信息?
- 下一篇: 职称论文的查重率多少才算是合格?