一、Yolov5安裝教學
1. 安裝Anaconda https://www.anaconda.com/products/individual
2. 建立Anaconda虛擬環境
*YOLOV5訓練一定要下載python3.8以上
conda create -n your_env_name python=3.6
e.g.: conda create -n yolov5 python=3.7
3. 啟用Anaconda虛擬環境
conda activate your_env_name
e.g.:conda activate yolov5
---------------------------------------------------------
退出虛擬環境: conda deactivate
刪除虛擬環境:conda remove -n your_env_name --all
4. 下載需要套件 https://tw511.com/a/01/29504.html
pip install opencv-python
pip install numpy
pip install matplotlib
pip install pandas
pip install scipy
pip install seaborn
pip install tqdm
pip install pillow
pip install tensorboard
pip install pyyaml
pip install pandas
pip install scikit-image
pip install Cython
pip install thop
pip install pycocotools
5. 安裝Cuda
6. 安裝Pytorch https://pytorch.org/get-started/locally/
*要選對作業系統如果有GPU(顯示卡)要記得點選如果沒有請點選CPU
pip3 install torch==1.9.0+cu102 torchvision==0.10.0+cu102 torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
7. 安裝PyCharm
https://www.jetbrains.com/pycharm/
8. 設定PyCharm
(1) Fileà settingsà Projectà Python Interpreter
(2) Python Interpreter點開下拉框à show allà +(add)
(3) Conda Environmentà Existing environmentà Interpreter下拉à python.exe
(4) 點選剛剛的python.exeà 按okà 執行看有沒有錯誤
9. 安裝labelImg
(1)在Pycharm右下角à 點選ternimal
(2) git clone https://github.com/tzutalin/labelImg
10. 下載YOIOV5目錄
11. 套用YOLOV5套件(requirements.txt)
(1) 在Pycharm右下角à 點選ternimal
(2) pip install -r requirements.txt
12. 測試(辨識人和船) 圖片下載:在資料夾IMG_2997.JPG
import cv2
import numpy as np
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# print(model)
img=cv2.imread('IMG_2997.JPG')
results = model(img)
results.print()
print(results.xyxy)
cv2.imshow('YOLO COCO', np.squeeze(results.render()))
cv2.waitKey(0)
13. 測試(視訊)
import torch
import numpy as np
import cv2
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, frame = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
results = model(frame)
cv2.imshow('YOLO COCO 01', np.squeeze(results.render()))
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
14. 錯誤處理(沒有錯可跳過)
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.
解決方法:
在train.py加入程式
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
Yolov5訓練教學
1. 創建一個pycharm project
2. 在pycharm project下創建images, labels兩個空目錄
3. 把要訓練的圖片放到Data àimages資料夾裡面
4. 安裝labelimg程式(貼上標籤)
(2) conda install -c anaconda lxml
(3) 接著到terminal下,切(cd)到labelimg目錄下
(4) pyrcc5 -o libs/resources.py resources.qrc
5. 設定與執行labelimg程式
(1) 找到 yolococo/labelimg/data 下有一個檔案叫做predefines_classes.txt檔案
(2) 在裡面內容修改成你訂的類別
(3) 再執行:python labelImg.py (mac可能要python3)
6. 訓練設定
依照規範,我們建立 dataset.yaml如下:
就放在yolov5目錄即可
7. 開始訓練
python train.py --img 320 --batch 16 --epochs 5 --data dataset.yaml --weights yolov5s.pt
*看看是否有錯誤,沒有的話就可以將--epochs 5 改為 500 來作完整訓練 (也可將--img 640 改為320速度較快)
8. 如果有錯誤(UnicodeDecodeError: 'cp950' codec can't decode byte 0xf0 in position 9: illegal multibyte sequence)
在train.py檔案的第72行with open(hyp) as f
變成àwith open(hyp, encoding="utf-8") as f
9. 查看訓練結果
10. 執行訓練模組
import numpy as np
import cv2
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp3/weights/best.pt',force_reload=True)
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, frame = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
frame = cv2.resize(frame,(800,480))
results = model(frame)
# print(np.array(results.render()).shape)
cv2.imshow('YOLO COCO 01', np.squeeze(results.render()))
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
沒有留言:
張貼留言
喜歡我的文章嗎? 喜歡的話可以留言回應我喔! ^^