🙋‍♀️ AI

[YOLOv5] - custom 데이터 훈련, 결과 (리눅스 , 윈도우)

수댕ʕت̫͡ʔ 2023. 5. 15. 22:49

졸업프로젝트를 하면서 무인매장 진열대의 객체를 인식하기 위하여 최종적으로 YOLOv5를 사용하였다.

1. YOLOv5

1-1. YOLOv5란?

실시간 객체 감지를 위한 딥러닝 모델로, "You Only Look Once"의 약자이다. 이미지나 비디오에서 다양한 객체를 식별하고 bounding box를 표시하는 기술을 제공한다.

 

1-2. YOLOv5의 특징

1. 높은 정확성 : YOLOv5는 높은 객체 감지 정확도를 가지고 있음.

2. 실시간 처리 : 실시간 환경에서 객체 감지를 수행할 수 있음. 

3. 다양한 객체 감지 

4. 경량화된 구조 : YOLOv5는 경량화된 딥러닝 모델 구조를 가지고 있음. 이를 통해 모델 크기를 줄이고 더 빠른 실행 속도를 제공

 

 

2. 라벨링

나는 darkLabel를 사용하여 라벨링을 진행하였다.

<darklabel>

이미지와 label파일은 png와 txt파일로 이루어져있다. 각각의 폴더를 따로 만들어서 저장하였다.

<images>
<labels>

3. 훈련 데이터

labels : 29,938장

images : 29,938장

classes : "kan", "chap", "jin", "shrimp", "fried", "can", "pring", "postick", "sweet potato", "potato” 총 10개

 

4. 개발환경

- 사용언어 : Python

- 사용환경 : Pycharm, CUDA - v11.8 / cuDDN v8.7.0 / opencv v4.6.0 , 리눅스 /윈도우 둘다 사용

 

5. Train

5-1. 훈련 방법

1. git clone

https://github.com/ultralytics/yolov5

 

GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite. Contribute to ultralytics/yolov5 development by creating an account on GitHub.

github.com

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

2. 데이터 세팅

  • Dsmart.yaml파일 구성
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../data0517  # dataset root dir
train: images  # train images (relative to 'path')
val: images # val images (relative to 'path')
test:  # test images (optional)

# Classes (10 classes)
names:
  0: kan
  1: chap
  2: jin
  3: shrimp
  4: fried
  5: can
  6: pring
  7: postick
  8: sweet potato
  9: potato

5-2. 훈련 

나는 YOLOv5의 모델 중 가장 크고 정확도가 좋은 x모델을 사용하였다.

  • 총 소요시간 : 24.248 hours
  • 연산량 : 203.9 GFLOPs
  • model 파일 : 173.1MB
# 윈도우 환경
python train.py --data ./data.yaml --cfg ./models/yolov5x.yaml --weights yolov5x.pt --batch 8 --epochs 200 --name model_x  --project model_result
# 리눅스 환경
python3 train.py --data ./data.yaml --cfg ./models/yolov5x.yaml --weights yolov5x.pt --batch 32 --epochs 300 --name model_x2  --project model_result2

5-3. 파라미터

  • 사용한 모델 : YOLOv5 - X 모델
  • epochs : 300
  • lr : 0.01
  • parameters : 86233975
  • layers : 322

6. Result, Test

6-1. Result

나는 Wandb를 사용하였다.

Class     Images  Instances          P          R      mAP50   mAP50-95: 100%
all      12768     130820          1      0.999      0.995      0.983

300 epochs completed in 24.248 hours.
Optimizer stripped from model_x/model_x6/weights/last.pt, 173.1MB
Optimizer stripped from model_x/model_x6/weights/best.pt, 173.1MB

Validating model_x/model_x6/weights/best.pt...
Fusing layers...
YOLOv5x summary: 322 layers, 86233975 parameters, 0 gradients, 203.9 GFLOPs
Class     Images  Instances          P          R      mAP50   mAP50-95: 100%
all      12768     130820          1      0.999      0.995      0.983
kan      12768      17713      0.999      0.993      0.995      0.965
chap      12768      16419          1          1      0.995       0.98
jin      12768      29856          1          1      0.995      0.982
shrimp      12768      17113          1          1      0.995      0.981
fried      12768      14045          1          1      0.995      0.989
can      12768      11230          1          1      0.995       0.98
pring      12768       7851          1          1      0.995      0.981
postick      12768       4477          1          1      0.995      0.986
sweet potato      12768       4823          1          1      0.995      0.994
potato      12768       7293          1          1      0.995       0.99
Results saved to model_x/model_x6
wandb: Waiting for W&B process to finish... (success).
wandb:
wandb: Run history:
wandb:      metrics/mAP_0.5 ▁▆▇▇▇███████████████████████████████████
wandb: metrics/mAP_0.5:0.95 ▁▃▅▅▆▆▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇█████████████████
wandb:    metrics/precision ▁▄▆▇▇▇▇▇▇██▇████████████████████████████
wandb:       metrics/recall ▃▁▃▅▆▆▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇███████████████
wandb:       train/box_loss █▅▄▄▃▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁
wandb:       train/cls_loss █▅▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁
wandb:       train/obj_loss █▆▅▅▅▄▄▄▄▄▄▄▄▄▃▃▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▁▁▁▁
wandb:         val/box_loss █▆▄▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
wandb:         val/cls_loss █▅▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
wandb:         val/obj_loss █▆▅▄▄▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁
wandb:                x/lr0 ████▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▁▁▁
wandb:                x/lr1 ████▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▁▁▁
wandb:                x/lr2 ████▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▁▁▁
wandb:
wandb: Run summary:
wandb:           best/epoch 299
wandb:         best/mAP_0.5 0.995
wandb:    best/mAP_0.5:0.95 0.98292
wandb:       best/precision 0.99976
wandb:          best/recall 0.99921
wandb:      metrics/mAP_0.5 0.995
wandb: metrics/mAP_0.5:0.95 0.9829
wandb:    metrics/precision 0.99976
wandb:       metrics/recall 0.99921
wandb:       train/box_loss 0.01125
wandb:       train/cls_loss 0.00052
wandb:       train/obj_loss 0.01414
wandb:         val/box_loss 0.01091
wandb:         val/cls_loss 0.00032
wandb:         val/obj_loss 0.0146
wandb:                x/lr0 0.00017
wandb:                x/lr1 0.00017
wandb:                x/lr2 0.00017

<Confusion Matrix>
<result>

6-2. 저장된 데이터로 Test

나는 이미지와 동영상을 사용하여 test하였다.

python detect.py --img 1280 --conf 0.25 --iou 0.45 --weights "./yolov5\model_x\model_x\weights\best.pt" --source "./yolov5\data\images\test1.jpg" --save-txt

 

<동영상으로 test>

 

6-3. 실시간 웹캠을 사용하여 Test

- 실시간으로 모델을 돌려보니 모든 객체가 정확하게 인식하는 결과를 볼 수 있었다.

python detect.py --weights (모델 웨이트 파일 경로) --source 0 --agnostic-nms --iou-thres 0.2

7. 나의 Git 링크

https://github.com/Soojin-Lee-01/Admin_Unmanned-Store-Management-Solution/tree/YOLO