-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyolo5.py
executable file
·50 lines (35 loc) · 963 Bytes
/
yolo5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# An example script of running ultralytics/yolov5 inference on an image
###
# Load image
from PIL import Image
input_image = Image.open('wolf.jpg')
imgs = [
input_image
]
###
# Import torch and print debug about GPU
import torch
print('torch.cuda.is_available()', torch.cuda.is_available())
print('torch.cuda.device_count()', torch.cuda.device_count())
print('torch.cuda.current_device()', torch.cuda.current_device())
print('torch.cuda.get_device_name(0)', torch.cuda.get_device_name(0))
###
# Load/download the model
model = torch.hub.load('ultralytics/yolov5', 'yolov5n', pretrained=True)
###
# Run inference
results = model(imgs)
# Save results
results.print()
results.save()
###
# Small benchmark
import time
from statistics import median
timings = []
for i in range(0,10):
t0 = time.time()
results2 = model(imgs)
t1 = time.time()
timings.append(t1 - t0)
print(f'Benchmark: inference min {min(timings)}, median {median(timings)}')