Tutorial: Yolov5 in Pytorch (VS code)
A simple tutorial for YOLOv5 Testing in Local PC
λ³Έ νν 리μΌμμλ local PCμ μ€μΉν py39
νκ²½μΌλ‘ YOLO v5λ₯Ό μ€ννλ λκ°μ§ λ°©λ²μ κ°λ¨ν μμ λ₯Ό μ 곡ν©λλ€.
λͺ λ Ήμ°½μ νμ©ν΄ ꡬλνλ λ°©λ²
VS codeμ κ°μ IDEλ‘ torch hubλ₯Ό νμ©νμ¬ κ΅¬λνλ λ°©λ²
β οΈ λ°λμ Installation Guide for DLIP μ λ¨Όμ μλ£ν ν μ€ννμμμ€. μ€μΉ λΌμ΄λΈλ¬λ¦¬ λ²μ μ΄ λͺ¨λ λ§μμΌ ν©λλ€.
Part 1. YOLOv5 Installation
YOLOv5 github(https://github.com/ultralytics/yolov5)μ μ μνμ¬ μλμ κ°μ΄ Repositoryλ₯Ό λ€μ΄λ‘λν©λλ€.

μμΆν΄μ ν ν΄λλͺ
μ yolov5-master
β yolov5
λ‘ λ³κ²½ ν μνλ μμΉμ λΆμ¬λ£μ΅λλ€.
/yolov5
ν΄λμ μ§μ
ν μλ κ·Έλ¦Όκ³Ό κ°μ΄ κ²½λ‘ μ£Όμλ₯Ό 볡μ¬(ctrl+C)ν©λλ€.

Anaconda promptλ₯Ό κ΄λ¦¬μλͺ¨λλ‘ μ€ν ν μλ μ½λ μμ°¨μ μΌλ‘ μ€νν©λλ€ (μλ κ·Έλ¦Ό μ°Έμ‘°)
conda activate py39
cd $YOLOv5PATH$ // [ctrl+V] νμ¬ λ³΅μ¬ν yolov5 κ²½λ‘ λΆμ¬λ£κΈ° (κ·Έλ¦Ό μ°Έμ‘°)
pip install -r requirements.txt

μ΄ν μ€μΉκ° μλ£λ©λλ€. μλμ κ°μ΄ κ²½κ³ κ° λ¨μ§λ§ 무μν΄λ λ©λλ€.

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
κ²°κ³Όμ°½. μλμ κ°μ΄ κ°μ²΄κ²μΆ μ μ₯λ ν΄λκ° μλκ²½λ‘λ‘ νμλ©λλ€.

μ€μ λ‘ ν΄λΉ μλκ²½λ‘λ‘ μ§μ νλ©΄ κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.

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 λ°μ΄ν°μ
(μ΄λ―Έμ§ λ° λΌλ²¨)μ΄ μλμΌλ‘ λ€μ΄λ‘λκ° λ©λλ€.
λ°μ΄ν°μ μλ λ€μ΄λ‘λ ν νμ΅μ μ§ννλ©° μλμ κ°μ΄ κ²°κ³Όμ°½μ΄ λ³΄μ¬μ§λλ€.
κ·Έλ¦¬κ³ νμ΅κ²°κ³Όκ° μ μ₯λ ν΄λκ° μλκ²½λ‘λ‘ νμλ©λλ€.

μ€μ λ‘ ν΄ μλκ²½λ‘λ‘ μ§μ νλ©΄ κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.

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 νμΌμ μμ±λμμ΅λλ€.

F1
ν€λ₯Ό λλ¬ select interpreter
λ₯Ό κ²μ ν ν΄λ¦ β py39
λ₯Ό μ νν©λλ€.
DLIP 2022 μμλ py39 μ¬μ©

F1
ν€λ₯Ό λλ¬ select default profile
μ κ²μ ν ν΄λ¦ β command prompt
λ₯Ό μ νν©λλ€.

F5
λλ ctrl+F5
λ₯Ό λλ¬ λΉλνμ¬ μλμ κ°μ΄ κ²°κ³Όμ°½μ΄ λ¨λ κ²μ νμΈν©λλ€

μλμ κ°μ΄ νμ΅κ²°κ³Όκ° μ μ₯λ ν΄λκ° μλκ²½λ‘λ‘ νμλ©λλ€.

μ€μ λ‘ ν΄λΉ μλκ²½λ‘λ‘ μ§μ νλ©΄ κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.

Last updated
Was this helpful?