Tutorial: Yolov5 in Pytorch (VS code)

A simple tutorial for YOLOv5 Testing in Local PC

λ³Έ νŠœν† λ¦¬μ–Όμ—μ„œλŠ” local PC에 μ„€μΉ˜ν•œ py39ν™˜κ²½μœΌλ‘œ YOLO v5λ₯Ό μ‹€ν–‰ν•˜λŠ” 두가지 λ°©λ²•μ˜ κ°„λ‹¨ν•œ 예제λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.

  1. λͺ…령창을 ν™œμš©ν•΄ κ΅¬λ™ν•˜λŠ” 방법

  2. VS code와 같은 IDE둜 torch hubλ₯Ό ν™œμš©ν•˜μ—¬ κ΅¬λ™ν•˜λŠ” 방법

⚠️ λ°˜λ“œμ‹œ Installation Guide for DLIP 을 λ¨Όμ € μ™„λ£Œν•œ ν›„ μ‹€ν–‰ν•˜μ‹­μ‹œμ˜€. μ„€μΉ˜ 라이브러리 버전이 λͺ¨λ‘ λ§žμ•„μ•Ό ν•©λ‹ˆλ‹€.

Part 1. YOLOv5 Installation

YOLOv5 github(https://github.com/ultralytics/yolov5)에 μ ‘μ†ν•˜μ—¬ μ•„λž˜μ™€ 같이 Repositoryλ₯Ό λ‹€μš΄λ‘œλ“œν•©λ‹ˆλ‹€.

image

μ••μΆ•ν•΄μ œ ν›„ 폴더λͺ…을 yolov5-master β†’ yolov5둜 λ³€κ²½ ν›„ μ›ν•˜λŠ” μœ„μΉ˜μ— λΆ™μ—¬λ„£μŠ΅λ‹ˆλ‹€.

/yolov5 폴더에 μ§„μž… ν›„ μ•„λž˜ κ·Έλ¦Όκ³Ό 같이 경둜 μ£Όμ†Œλ₯Ό 볡사(ctrl+C)ν•©λ‹ˆλ‹€.

image

Anaconda promptλ₯Ό κ΄€λ¦¬μžλͺ¨λ“œλ‘œ μ‹€ν–‰ ν›„ μ•„λž˜ μ½”λ“œ 순차적으둜 μ‹€ν–‰ν•©λ‹ˆλ‹€ (μ•„λž˜ κ·Έλ¦Ό μ°Έμ‘°)

conda activate py39
cd $YOLOv5PATH$ // [ctrl+V] ν•˜μ—¬ λ³΅μ‚¬ν•œ yolov5 경둜 λΆ™μ—¬λ„£κΈ° (κ·Έλ¦Ό μ°Έμ‘°)
pip install -r requirements.txt
image

이후 μ„€μΉ˜κ°€ μ™„λ£Œλ©λ‹ˆλ‹€. μ•„λž˜μ™€ 같이 κ²½κ³ κ°€ λœ¨μ§€λ§Œ λ¬΄μ‹œν•΄λ„ λ©λ‹ˆλ‹€.

image

Part 2. Run YOLOv5 in Local PC with CLI

λͺ…λ Ήμ°½(command line, CLI)둜 YOLO v5 μ‹€ν–‰μ‹œ detect.py, val.py, train.py와 같이 gitμ—μ„œ 제곡된 νŒŒμΌμ„ λΉŒλ“œν•©λ‹ˆλ‹€.

  • λ”°λΌμ„œ CLI둜 λΉŒλ“œμ‹œ, λ°˜λ“œμ‹œ git을 local PC에 μ €μž₯ν•˜κ³  폴더 κ²½λ‘œμ— μ§„μž…ν•˜λŠ” 과정이 μ„ ν–‰λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

  • 즉, μœ„μ˜ Part 1을 μˆ˜ν–‰ν•΄μ„œ Yolov5 repositoryλ₯Ό local PC에 μ €μž₯ν•˜λ©΄ λ©λ‹ˆλ‹€.

1. Inference

yolov5 폴더 κ²½λ‘œμ— μ§„μž…ν•œ μƒνƒœμ—μ„œ μ•„λž˜ μ½”λ“œ μž…λ ₯ν•˜μ—¬ detect.pyλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

python detect.py --weights yolov5n.pt --img 640 --conf 0.25 --source data/images

κ²°κ³Όμ°½. μ•„λž˜μ™€ 같이 κ°μ²΄κ²€μΆœ μ €μž₯된 폴더가 μƒλŒ€κ²½λ‘œλ‘œ ν‘œμ‹œλ©λ‹ˆλ‹€.

image

μ‹€μ œλ‘œ ν•΄λ‹Ή μƒλŒ€κ²½λ‘œλ‘œ μ§„μž…ν•˜λ©΄ κ²°κ³Όλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

image

2. Train (Opendataset COCO)

COCO128 ν•™μŠ΅ μ˜€ν”ˆλ°μ΄ν„°μ…‹μœΌλ‘œ YOLOv5 ν•™μŠ΅ν•˜λŠ” νŠœν† λ¦¬μ–Όμž…λ‹ˆλ‹€.

yolov5 폴더 κ²½λ‘œκ°€ μœ μ§€λœ μƒνƒœμ—μ„œ μ•„λž˜ μ½”λ“œ μž…λ ₯ν•˜μ—¬ train.pyλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

python train.py --img 640 --batch 1 --epochs 1 --data coco128.yaml --weights yolov5n.pt

coco128.yml νŒŒμΌμ—μ„œ μ§€μ •λœ 바와 같이 workspace의 μƒμœ„ 폴더 \dataset 에 coco 데이터셋 (이미지 및 라벨)이 μžλ™μœΌλ‘œ λ‹€μš΄λ‘œλ“œκ°€ λ©λ‹ˆλ‹€.

데이터셋 μžλ™ λ‹€μš΄λ‘œλ“œ ν›„ ν•™μŠ΅μ„ μ§„ν–‰ν•˜λ©° μ•„λž˜μ™€ 같이 결과창이 λ³΄μ—¬μ§‘λ‹ˆλ‹€.

그리고 ν•™μŠ΅κ²°κ³Όκ°€ μ €μž₯된 폴더가 μƒλŒ€κ²½λ‘œλ‘œ ν‘œμ‹œλ©λ‹ˆλ‹€.

image

μ‹€μ œλ‘œ ν•΄ μƒλŒ€κ²½λ‘œλ‘œ μ§„μž…ν•˜λ©΄ κ²°κ³Όλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

image

3. Train with custom dataset

See Tutorial: Yolov5 with custom dataset

-----------------------------------------------------------------------------------------------------

Run YOLOv5 Inference in Local PC with PyTorch Hub

  • VS code와 같은 IDEμ—μ„œλ„ YOLO v5 결과에 μ ‘κ·Όν•˜μ—¬ ν”„λ‘œκ·Έλž˜λ°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • ν•„μš”ν•œ λͺ¨λ“ˆ(requirements)만 μ„€μΉ˜ν•˜λ©΄, git을 local PC에 μ €μž₯ν•˜λŠ” 선행과정이 ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

  • 사전에 VS code μ„€μΉ˜ 및 μ‚¬μš©λ²• μˆ™μ§€λ₯Ό μ„ ν–‰ν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€.

Procedure

μž„μ˜μ˜ workspace(μž‘μ—…) 폴더λ₯Ό 생성 ν›„ 우클릭 β†’ Code둜 μ—΄κΈ°λ₯Ό ν΄λ¦­ν•©λ‹ˆλ‹€.

ν˜Ήμ€ VS Code μ‹€ν–‰ ν›„ μž‘μ—… 폴더λ₯Ό 열어도 λ©λ‹ˆλ‹€.

파이썬 파일 YOLOv5_run_test.py 을 μƒμ„±ν•˜λ“€κ³  μ•„λž˜μ˜ μ½”λ“œλ₯Ό λΆ™μ—¬ λ„£μŠ΅λ‹ˆλ‹€.

import torch
import cv2
import random
from PIL import Image

# Load the Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5n', pretrained=True)

# Image preparation from URL . images link
img_URL = [           
           "https://user-images.githubusercontent.com/23421059/168719874-be48ef28-954c-4a4c-a048-1e11699e0b56.png",
           ]

imgs = []
img_L = len(img_URL)

# Append these 3 images and save 
for i in range(img_L):
  imgName = f"{i}.jpg"
  torch.hub.download_url_to_file(img_URL[i],imgName)  # download 2 images
  # imgs.append(Image.open(fileName))  # PIL image
  imgs.append(cv2.imread(imgName)[:,:,::-1]) # OpenCV image (BGR to RGB)

# Run Inference
results = model(imgs)

# Print Results
results.print()

# Save Result images with bounding box drawn
results.save()  # or .show()

# Select a random test image
randNo = random.choice(range(img_L))
print(f"Selected Image No = {randNo}\n\n")

# Print the Bounding Box result:  6 columns
# Column (1~4) Coordinates of TL, BR corners (5) Confidence (6) Class ID
print(results.xyxy[randNo],'\n')  # imgs predictions (tensor)

# Print the Bounding Box result using Pandas
print(results.pandas().xyxy[randNo],'\n')  # imgs predictions (pandas)

# Show result image
cv2.imshow("result", (results.imgs[randNo])[:,:,::-1])
cv2.waitKey(0)

μ•„λž˜ κ·Έλ¦Όκ³Ό 같이 μœ„ μ½”λ“œκ°€ μƒˆ python νŒŒμΌμ— μž‘μ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

image

F1ν‚€λ₯Ό 눌러 select interpreterλ₯Ό 검색 ν›„ 클릭 β†’ py39λ₯Ό μ„ νƒν•©λ‹ˆλ‹€.

DLIP 2022 μ—μ„œλŠ” py39 μ‚¬μš©

image

F1ν‚€λ₯Ό 눌러 select default profile을 검색 ν›„ 클릭 β†’ command promptλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.

image

F5 λ˜λŠ” ctrl+F5λ₯Ό 눌러 λΉŒλ“œν•˜μ—¬ μ•„λž˜μ™€ 같이 결과창이 λœ¨λŠ” 것을 ν™•μΈν•©λ‹ˆλ‹€

image

μ•„λž˜μ™€ 같이 ν•™μŠ΅κ²°κ³Όκ°€ μ €μž₯된 폴더가 μƒλŒ€κ²½λ‘œλ‘œ ν‘œμ‹œλ©λ‹ˆλ‹€.

image

μ‹€μ œλ‘œ ν•΄λ‹Ή μƒλŒ€κ²½λ‘œλ‘œ μ§„μž…ν•˜λ©΄ κ²°κ³Όλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

image

Last updated

Was this helpful?