Yolov7 based Object Detection
가장 널리 사용되는 ML Tasks 중 하나로 Object detection이 있다.
그리고 Object detection task를 위하여,
다양한 Tool 중에서 Darknet 기반의 Yolo (You only look once)가 Version upgrade나 성능이 준수하여 많이 사용되고 있다.
오늘은 Custom dataset을 사용하여 Yolov7 기반의 Object detection을 수행해본다. 참고로 Tutorial 매~~우 쉽다.
(향후 진행해볼 것 메모: Change into customized backbone in Yolov7)
현재 22년 9월 기준 Yolov7까지 나와 있으며 링크는 이곳을 참조
https://github.com/WongKinYiu/yolov7/tree/main
GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time
Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors - GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of...
github.com
(다른 repo. 들도 많이 봤지만 여기가 Official 이라서 더 잘 정리된 느낌)
참고로, 다른 task (e.g. instance segmentation, pose estimation 등)는 다른 branch에서 활용 가능
Yolov7 버전부터는 Facebook에서 개발한 detectron2를 함께 사용하고 있으니, 설치 필요
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
기타 package 설치
pip install -r requirements.txt
Training on COCO
- --device: 사용할 GPU numbers
- --batch-size: batch size
- --data: 데이터셋 관련 config (e.g. 경로 (train/val/test), #classes, labels)
- --img: image size
- --cfg: Architecture 정보 (backbone + head)
- --weights: Pretrained weight (training 과정에서는 (resume이 아니라면) 필요 없으니 공란)
- --name: project name
- --hyp: hyperparameter config (e.g. learning rate, momentum 등)
# Single GPU 경우
python train.py --workers 8 --device 0,1,2,3 --batch-size 32 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7_training --hyp data/hyp.scratch.p5.yaml
# Multiple GPU 경우 (이거 아직 안됨)
python -m torch.distributed.launch --nproc_per_node 2 --master_port 9527 train.py --workers 8 --device 0,1,2,3 --sync-bn --batch-size 128 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml
실험이 끝나고 model & config 은 runs/train/yolov7_training/ 에 저장된다
- weights/best.pt : Final (best) model
- hyp.yaml: 사용했던 hyperparameters (e.g. learning rate, momentum 등)
- opt.yaml: 코드 실행했을 때 사용했던 configuration 정보 (--worker, --batch-size, --name 등)
+ Transfer learning (training 과정과 거의 동일)
눈치 챘겠지만 큰 차이는 --weight 밖에 없음
python train.py --workers 8 --device 0,1,2,3 --batch-size 32 --data data/custom.yaml --img 640 640 --cfg cfg/training/yolov7-custom.yaml --weights 'yolov7_training.pt' --name yolov7-custom --hyp data/hyp.scratch.custom.yaml
Test on Image (큰 차이가 없어서 설명 생략)
python test.py --data data/coco.yaml --img 640 --batch 32 --conf 0.025 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val
Inference on Image
python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source your_image.jpg
Inference on Video
python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source yourvideo.mp4
Arguments
주로 사용되는 argument는 다음과 같다
1. weights: pretrained model PATH
2. source: dataset PATH
3. img-size: image size during inference step
4. conf-thres: object confidence threshold (Yolo loss function 참고)
5. iou-threshold: IOU threshold (Yolo loss function 참고)
6. device: 사용할 device (e.g. 0 or 0,1,2,3 or cpu)
7. view-img: display results
8. save-txt: 결과 txt로 저장
9. save-conf: confidence socre 저장
10. nosave: 결과 저장 안함
11. classes: class 표시할지 여부 (e.g. --class 0, or --class 0 2 3)
12. project: project 저장할 폴더 PATH
13. name: project name (뒤에 숫자가 붙는 형식: e.g. exp1, exp2, ...)
Results
영상도 제작했으나, 용량 문제로 업로드 실패
Conclusion
생각보다 Inference는 너무 쉬워서 쉽게 따라할 수 있을 것 같다.