Hồ Chí Minh, 17-08-2023
Nguyễn Hữu Lợi, UIT-Together Research Group

YOLO

Mục Lục

Step 1. Tạo thư mục lưu trữ

Truy cập vào thư mục cvpr2023, tạo thư mục LuuTru

cd /home/cvpr2023/
mkdir LuuTru

Tạo thư mục dataset

cd /home/cvpr2023/LuuTru/ mkdir dataset

Step 2. Tổ chức dữ liệu

Trong thư mục dataset, tạo các thư mục con như sau:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Convert dữ liệu theo đúng định dạng yolo:
https://github.com/UIT-Together-Research-Group/OD_ConvertDataset

Lưu ý: Định dạng của bounding box trong file json có nhiều loại (pascal voc, coco, yolo,) và cần chuyển bounding box của nhãn về định dạng yolo [x, y, w, h]. Tham khảo thêm tại trang sau: https://haobin-tan.netlify.app/ai/computer-vision/object-detection/coco-json-to-yolo-txt/

Step 3. Cài đặt môi trường, bao gồm môi trường Python>=3.8.0 và PyTorch>=1.8

Tải source code yolov5

cd /home/cvpr2023/LuuTru/ git clone https://github.com/ultralytics/yolov5.git

Cài đặt các yêu cầu trong requirements.txt bằng cách:

cd /home/cvpr2023/LuuTru/yolov5
pip install -r requirements.txt

Cấu hình file coco.yaml liên kết thông tin đến bộ dữ liệu

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Step 4. Train model trên bộ dữ liệu

Câu lệnh thực hiện train mô hình:

cd /home/cvpr2023/LuuTru/yolov5 python train.py --data ./data/coco.yaml --epochs 300 --weights yolov5x.pt --batch-size 8 --device 0

Trong đó:
data : đường dẫn đến file coco.yaml
epochs 300 : thực hiện train 300 epochs
weight : lấy trọng số từ fine-tuning model yolo5x.pt (file này được tự động download về nếu chưa có)
batch-size 8 : số lượng mẫu dữ liệu trong 1 lần huần luyện (8 ảnh/ lần)
device 0: thực hiện trên gpu có id = 0

Step 5. Chạy kiểm thử

Câu lệnh thực hiện kiểm thử trên tập test:

cd /home/cvpr2023/LuuTru/yolov5 python val.py --task 'test' --weights ./runs/train/exp/weights/best.pt --data ./data/coco.yaml --img 640 --verbose

Câu lệnh thực hiện kiểm thử trên tập val:

cd /home/cvpr2023/LuuTru/yolov5 python val.py --task 'val' --weights ./runs/train/exp/weights/best.pt --data ./data/coco.yaml --img 640 --verbose

Trong đó:
task 'test' : thực hiện kiểm thử trên tập test
weight ./runs/train/exp/weights/best.pt đường dẫn đến mô hình tốt nhất mà ta huấn luyện được sau 300 epochs ở bước 4
verbose : hiển thị bảng kết quả theo tiêu chuẩn MS CoCo

Step 6. Chạy kiểm chứng

Tạo thư mục chứa ảnh/video cần phát hiện đối tượng

cd /home/cvpr2023/LuuTru
mkdir uit_together

Câu lệnh thực hiện detect :

cd /home/cvpr2023/LuuTru/yolov5 python detect.py --weights ./runs/train/exp/weights/best.pt --img 640 --conf 0.25 --source ../uit_together/

Trong đó:
conf 0.25 : ngưỡng tin cậy (confidence threshold) = 0.25
source uit_together: đường dẫn đến thư mục chứa ảnh/video cần phát hiện

Để xem kết quả phát hiện, vào thư mục:
/home/cvpr2023/LuuTru/yolov5/runs/detect/