最近由于工程需要,研究學(xué)習(xí)了一下windows下如何配置pytorch和yolov8,并自己搜集數(shù)據(jù)進(jìn)行訓(xùn)練和預(yù)測,預(yù)測使用usb攝像頭進(jìn)行實時預(yù)測。在此記錄一下全過程
一、軟件安裝和配置
1. vscode安裝
windows平臺開發(fā)python,我采用vscode作為基礎(chǔ)開發(fā)平臺,點擊 https://code.visualstudio.com/進(jìn)入vscode官網(wǎng),下載對應(yīng)的穩(wěn)定版本即可。
下載安裝完成后,在插件界面搜索 python,找到第一個即可安裝好python開發(fā)環(huán)境。
python我安裝的是3.10版本
2. miniconda3安裝
miniconda3可以用于配置pytorch的開發(fā)環(huán)境,https://docs.conda.io/en/latest/miniconda.html在官網(wǎng)下載對應(yīng)版本即可,我的是python3.10,windows64,所以下載的是如圖所示的安裝包
配置path變量:找到miniconda3安裝路徑,參照下圖設(shè)置環(huán)境變量
設(shè)置完成后,在開始菜單中找到Anaconda Prompt(miniconda3),打開。
- 添加清華鏡像源,提高軟件下載速度
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
- 配置miniconda開發(fā)環(huán)境
使用一下命令,創(chuàng)建一個pytorch虛擬環(huán)境
conda create -n pytorch python=3.10
-n 后面跟虛擬環(huán)境名稱
python=設(shè)置python版本
- 通過以下命令激活虛擬環(huán)境
conda activate pytorch
之后,可以在該環(huán)境下配置pytorch
3. pytorch安裝
進(jìn)入pytorch官網(wǎng)https://pytorch.org/,查找對應(yīng)的下載命令,我的是windows、CPU、python版本,所以選擇如下圖所示配置
復(fù)制圖中對應(yīng)的安裝命令,即可安裝pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
注意,此命令在激活的pytorch環(huán)境下安裝
- 打開vscode,配置pytorch環(huán)境
在vscode中輸入ctrl+shif+p打開命令行界面,輸入Python:Select Interpreter選擇python的開發(fā)編譯環(huán)境
選擇Python 3.10.12(‘pytorch’)作為當(dāng)前環(huán)境即可
新建test.py文件,輸入如下代碼,按F5執(zhí)行,若成功,則pytorch配置成功。
import torch
print(torch.__version__)
print(torch.cuda.is_available())
或者執(zhí)行
python test.py
4. yolov8安裝配置
在pytorch環(huán)境激活的情況下,使用一下命令安裝yolov8
pip install ultralytics
安裝完成后,通過命令進(jìn)行測試
yolo task=detect mode=predict model=yolov8n.pt imgsz=640 show=True save=True
命令會自己到github下載對應(yīng)的測試數(shù)據(jù)bus.jpg和zidane.jpg兩張圖片 和默認(rèn)的權(quán)重文件yolov8n.pt
如果無法下載yolov8n.pt,可以自己下載后,復(fù)制到對應(yīng)的路徑下也可以。
D:\Install\miniconda3\envs\pytorch\Lib\site-packages\ultralytics\yolo\v8\detect
如果缺少環(huán)境,下載ultralytics源碼,執(zhí)行下面命令安裝依賴
pip install -r requirements.txt
二、yolov8自定義數(shù)據(jù)集
- 數(shù)據(jù)采集,根據(jù)自己項目需要,采集場景圖片,圖像大小沒關(guān)系,后期yolo會根據(jù)配置,自動調(diào)整大小
- 對數(shù)據(jù)進(jìn)行標(biāo)注
安裝labelImg數(shù)據(jù)標(biāo)注工具,之后對數(shù)據(jù)進(jìn)行標(biāo)注
1、打開標(biāo)注前圖像數(shù)據(jù)所在文件夾
2、設(shè)置標(biāo)注后標(biāo)簽文件保存位置,該位置下僅保存txt類型的標(biāo)簽數(shù)據(jù)
類別 box中心x box中心y box寬 box高
0 0.044531 0.735417 0.023438 0.048611
0 0.084766 0.625694 0.025781 0.054167
0 0.154297 0.620833 0.022656 0.058333
0 0.275000 0.584028 0.037500 0.076389
0 0.289844 0.868750 0.034375 0.070833
3、數(shù)據(jù)分類
按照圖像、標(biāo)簽新建文件夾,并在每個文件夾下新建train和val文件夾,注意路徑中不能有中文,路徑如下所示
使用如下代碼,將原始圖像數(shù)據(jù)和標(biāo)注數(shù)據(jù)進(jìn)行分類
import sys,os
import shutil
imgPath = "F:/images/"
labelPath = "F:/data/Annotation"
yoloImagePath = "F:/data/images"
yoloLabelsPath = "F:/data/labels"
# labels = os.listdir(labelPath)
f=os.walk(labelPath)
#80%數(shù)據(jù)做訓(xùn)練,20%做矯正
persent=0.8
for dirpath,dirNames,filenames in f:
trainNum = int(len(filenames)*persent)
for i,filename in enumerate(filenames):
name=filename.split('.')[0]
if name=='classes':
continue
imgSrc=imgPath+name+".jpg"
imgDst=''
labelSrc=labelPath+"/"+filename
labelDst=''
# 訓(xùn)練數(shù)據(jù)
if i<trainNum:
imgDst = yoloImagePath+"/train"
labelDst = yoloLabelsPath+"/train"
else:
#矯正數(shù)據(jù)
imgDst = yoloImagePath+"/val"
labelDst = yoloLabelsPath+"/val"
shutil.copy(imgSrc,imgDst)
shutil.copy(labelSrc,labelDst)
4.新建配置文件.yaml,用于指定訓(xùn)練數(shù)據(jù)、驗證數(shù)據(jù)的路徑
#訓(xùn)練數(shù)據(jù)路徑
train: F:/data/images/train
#驗證數(shù)據(jù)路徑
val: F:/data/images/val
# number of classes
nc: 1
# class names
names: ['acupoint']
自此,自定義數(shù)據(jù)的搜集和標(biāo)注完成
三、自定義數(shù)據(jù)訓(xùn)練
接下來開始進(jìn)行訓(xùn)練,在開始菜單打開anaconda prompt(miniconda3),使用conda activate pytorch后,使用以下命令
yolo task=detect mode=train model=yolov8n.pt data=F:/data/acupoint.yaml epochs=100 batch=16
task:指定運行的任務(wù)類型,有detect\segment\classify\init
mode:指定是train、predict、val
model:選擇配置值權(quán)重模型
data:指定.yaml所在位置,
epochs:迭代次數(shù)
batch:一次加載多少張圖片后更新權(quán)重
訓(xùn)練后,結(jié)果默認(rèn)保存在以下目錄中,
D:\Install\miniconda3\envs\pytorch\Lib\site-packages\ultralytics\yolo\v8\detect\runs\detect\train2
其中best.pt即為后續(xù)預(yù)測需要使用的模型文件
四、攝像頭數(shù)據(jù)預(yù)測
訓(xùn)練結(jié)束后,使用
yolo task=detect mode=predict model=./runs/detect/train2/weights/best.pt source=0 show=True save=True
打開攝像頭開始進(jìn)行預(yù)測,默認(rèn)圖像大小為480x640.
其中model:自定義數(shù)據(jù)訓(xùn)練的模型結(jié)果
source:指定預(yù)測數(shù)據(jù),可以是圖片路徑或視頻路徑,0表示使用usb攝像頭0實時讀取數(shù)據(jù)
show:是否實時顯示結(jié)果
save:是否保存結(jié)果文章來源:http://www.zghlxwxcb.cn/news/detail-563910.html
以上是經(jīng)過幾天摸索,并經(jīng)過實踐驗證的,可以行的通。后續(xù)會接著研究如何使用代碼獲取預(yù)測結(jié)果。文章來源地址http://www.zghlxwxcb.cn/news/detail-563910.html
到了這里,關(guān)于windows下配置pytorch + yolov8+vscode,并自定義數(shù)據(jù)進(jìn)行訓(xùn)練、攝像頭實時預(yù)測的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!