VIDEO
一、 Yolov5 安裝教學
1. 安裝 Anaconda https://www.anaconda.com/products/individual
2. 建立 Anaconda 虛擬環境
*YOLOV5 訓練一定要下載 python3.8 以上
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
https://developer.nvidia.com/cuda-10.2-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
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 torch
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 程式 ( 貼上標籤 )
(1) conda install pyqt=5
(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()