새로나온 YOLOv7을 colab에서 간단하게 사용할 수 있는 코드입니다~~
사람과 오토바이가 같이 있는 이미지를 넣었는데, 잘되네요ㅎㅎ real time에서도 얼마나 빠르게 잘되는지 확인해봐야겠어요!!
In [1]:
import sys
import torch
print(f"Python version: {sys.version}, {sys.version_info} ")
print(f"Pytorch version: {torch.__version__} ")
Python version: 3.7.13 (default, Apr 24 2022, 01:04:09)
[GCC 7.5.0], sys.version_info(major=3, minor=7, micro=13, releaselevel='final', serial=0)
Pytorch version: 1.12.0+cu113
In [2]:
!nvidia-smi
Fri Jul 29 08:13:09 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 39C P8 9W / 70W | 3MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
In [3]:
!# Download YOLOv7 code
!git clone https://github.com/WongKinYiu/yolov7
%cd yolov7
!ls
fatal: destination path 'yolov7' already exists and is not an empty directory.
/content/yolov7
cfg export.py LICENSE.md requirements.txt tools utils
data figure models runs traced_model.pt yolov7
deploy hubconf.py paper scripts train_aux.py yolov7.pt
detect.py inference README.md test.py train.py
In [4]:
!# Download trained weights
!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
--2022-07-29 08:13:10-- https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
Resolving github.com (github.com)... 192.30.255.112
Connecting to github.com (github.com)|192.30.255.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/b0243edf-9fb0-4337-95e1-42555f1b37cf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220729%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220729T081144Z&X-Amz-Expires=300&X-Amz-Signature=02eb868b18874f53cbfbfbca7ed154eca7f88e8549b1431d7bbabd4f3fb85b05&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=511187726&response-content-disposition=attachment%3B%20filename%3Dyolov7.pt&response-content-type=application%2Foctet-stream [following]
--2022-07-29 08:13:10-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/b0243edf-9fb0-4337-95e1-42555f1b37cf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220729%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220729T081144Z&X-Amz-Expires=300&X-Amz-Signature=02eb868b18874f53cbfbfbca7ed154eca7f88e8549b1431d7bbabd4f3fb85b05&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=511187726&response-content-disposition=attachment%3B%20filename%3Dyolov7.pt&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75587165 (72M) [application/octet-stream]
Saving to: ‘yolov7.pt.1’
yolov7.pt.1 100%[===================>] 72.08M 30.8MB/s in 2.3s
2022-07-29 08:13:12 (30.8 MB/s) - ‘yolov7.pt.1’ saved [75587165/75587165]
In [5]:
!# Detection
!python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.25, device='', exist_ok=False, img_size=640, iou_thres=0.45, name='exp', no_trace=False, nosave=False, project='runs/detect', save_conf=False, save_txt=False, source='inference/images/horses.jpg', update=False, view_img=False, weights=['yolov7.pt'])
YOLOR 🚀 v0.1-78-g6baceff torch 1.12.0+cu113 CUDA:0 (Tesla T4, 15109.75MB)
Fusing layers...
RepConv.fuse_repvgg_block
RepConv.fuse_repvgg_block
RepConv.fuse_repvgg_block
Model Summary: 306 layers, 36905341 parameters, 6652669 gradients
Convert model to Traced-model...
traced_script_module saved!
model is traced!
/usr/local/lib/python3.7/dist-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2894.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
The image with the result is saved in: runs/detect/exp4/horses.jpg
Done. (0.127s)
In [6]:
# define helper functions to show images
def imShow(path):
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
image = cv2.imread(path)
height, width = image.shape[:2]
resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC)
fig = plt.gcf()
fig.set_size_inches(18, 10)
plt.axis("off")
plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB))
plt.show()
In [7]:
imShow("runs/detect/exp/horses.jpg")
In [8]:
!# Detection
!python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/maksssksksss97.png
Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.25, device='', exist_ok=False, img_size=640, iou_thres=0.45, name='exp', no_trace=False, nosave=False, project='runs/detect', save_conf=False, save_txt=False, source='inference/images/maksssksksss97.png', update=False, view_img=False, weights=['yolov7.pt'])
YOLOR 🚀 v0.1-78-g6baceff torch 1.12.0+cu113 CUDA:0 (Tesla T4, 15109.75MB)
Fusing layers...
RepConv.fuse_repvgg_block
RepConv.fuse_repvgg_block
RepConv.fuse_repvgg_block
Model Summary: 306 layers, 36905341 parameters, 6652669 gradients
Convert model to Traced-model...
traced_script_module saved!
model is traced!
/usr/local/lib/python3.7/dist-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2894.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
The image with the result is saved in: runs/detect/exp5/maksssksksss97.png
Done. (0.132s)
In [10]:
imShow('runs/detect/exp5/maksssksksss97.png')