1 분 소요


YOLO (You Only Look Once)

YOLOv3 모델 (PyTorch)

  • https://github.com/ultralytics/yolov3

  • https://www.ultralytics.com

모델 다운로드

In [1]:
%cd /content
!git clone https://github.com/ultralytics/yolov3
%cd yolov3
!pip install -qr requirements.txt
Out [1]:
/content
Cloning into 'yolov3'...
remote: Enumerating objects: 10065, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 10065 (delta 21), reused 31 (delta 13), pack-reused 10017
Receiving objects: 100% (10065/10065), 9.38 MiB | 7.79 MiB/s, done.
Resolving deltas: 100% (6779/6779), done.
/content/yolov3
     |████████████████████████████████| 1.6 MB 15.0 MB/s 

In [2]:
import torch
from IPython.display import Image

# GPU 사용 체크
print(f'Torch:{torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else "CPU"})')
Out [2]:
Torch:1.13.0+cu116 (Tesla T4)

추론(Inference)

detect.py: YOLOv3 모델을 이용한 추론

In [3]:
!ls data/images
Out [3]:
bus.jpg  zidane.jpg

In [4]:
Image(filename='data/images/bus.jpg', width=600)
Out [4]:

img

In [5]:
Image(filename='data/images/zidane.jpg', width=600)
Out [5]:

img

In [6]:
!python detect.py --weights yolov3.pt --img 640 --conf 0.25 --source data/images
Out [6]:
Downloading https://ultralytics.com/assets/Arial.ttf to /root/.config/Ultralytics/Arial.ttf...
detect: weights=['yolov3.pt'], source=data/images, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv3 🚀 v9.6.0-29-ga441ab1 torch 1.13.0+cu116 CUDA:0 (Tesla T4, 15110MiB)

Downloading https://github.com/ultralytics/yolov3/releases/download/v9.6.0/yolov3.pt to yolov3.pt...
100% 119M/119M [00:03<00:00, 36.1MB/s]

Fusing layers... 
Model Summary: 261 layers, 61922845 parameters, 0 gradients
image 1/2 /content/yolov3/data/images/bus.jpg: 640x480 4 persons, 1 bicycle, 1 bus, Done. (0.054s)
image 2/2 /content/yolov3/data/images/zidane.jpg: 384x640 2 persons, 2 ties, Done. (0.034s)
Speed: 0.6ms pre-process, 44.1ms inference, 20.5ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp

In [7]:
Image(filename='runs/detect/exp/bus.jpg', width=600)
Out [7]:

img

In [8]:
Image(filename='runs/detect/exp/zidane.jpg', width=600)
Out [8]:

img

YOLOv5 모델 (PyTorch)

  • https://github.com/ultralytics/yolov5
  • https://www.ultralytics.com

모델 다운로드

In [9]:
%cd /content
!git clone https://github.com/ultralytics/yolov5
%cd yolov5
!pip install -qr requirements.txt
Out [9]:
/content
Cloning into 'yolov5'...
remote: Enumerating objects: 14566, done.
remote: Counting objects: 100% (88/88), done.
remote: Compressing objects: 100% (63/63), done.
remote: Total 14566 (delta 44), reused 48 (delta 25), pack-reused 14478
Receiving objects: 100% (14566/14566), 13.74 MiB | 6.51 MiB/s, done.
Resolving deltas: 100% (10017/10017), done.
/content/yolov5
     |████████████████████████████████| 182 kB 13.9 MB/s 
     |████████████████████████████████| 62 kB 1.7 MB/s 

추론(Inference)

detect.py: YOLOv3 모델을 이용한 추론

In [10]:
from IPython.display import Image
import torch
In [11]:
!ls data/images
Out [11]:
bus.jpg  zidane.jpg

In [12]:
Image(filename='data/images/bus.jpg', width=600)
Out [12]:

img

In [13]:
Image(filename='data/images/zidane.jpg', width=600)
Out [13]:

img

In [14]:
!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images
Out [14]:
detect: weights=['yolov5s.pt'], source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1
YOLOv5 🚀 v7.0-37-gb2f94e8 Python-3.8.16 torch-1.13.0+cu116 CUDA:0 (Tesla T4, 15110MiB)

Downloading https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt to yolov5s.pt...
100% 14.1M/14.1M [00:01<00:00, 11.4MB/s]

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
image 1/2 /content/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, 12.7ms
image 2/2 /content/yolov5/data/images/zidane.jpg: 384x640 2 persons, 2 ties, 14.2ms
Speed: 0.5ms pre-process, 13.5ms inference, 1.2ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp

In [15]:
Image(filename='runs/detect/exp/bus.jpg', width=600)
Out [15]:

img

In [16]:
Image(filename='runs/detect/exp/zidane.jpg', width=600)
Out [16]:

img

Reference

  • 이 포스트는 SeSAC 인공지능 자연어처리, 컴퓨터비전 기술을 활용한 응용 SW 개발자 양성 과정 - 심선조 강사님의 강의를 정리한 내용입니다.

댓글남기기