-
Notifications
You must be signed in to change notification settings - Fork 30
/
yolo_detect.py
92 lines (71 loc) · 2.62 KB
/
yolo_detect.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import glob
import torch
import numpy as np
from scipy.spatial import distance
import yolo.yolov5.detect as yolo_eval
def compute_iou_bb(box_a, box_b):
# determine the (x, y)-coordinates of the intersection rectangle
x_a = max(box_a[0], box_b[0])
y_a = max(box_a[1], box_b[1])
x_b = min(box_a[2], box_b[2])
y_b = min(box_a[3], box_b[3])
# compute the area of intersection rectangle
inter_area = abs(max((x_b - x_a, 0)) * max((y_b - y_a), 0))
if not inter_area:
return 0
# compute the area of both the prediction and ground-truth
# rectangles
box_a_area = abs((box_a[2] - box_a[0]) * (box_a[3] - box_a[1]))
box_b_area = abs((box_b[2] - box_b[0]) * (box_b[3] - box_b[1]))
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the intersection area
iou = inter_area / float(box_a_area + box_b_area - inter_area)
return iou
def compute_jaccard(box_a, box_b):
return distance.jaccard(box_a, box_b)
def compute_labels_accuracy():
real_labels = glob.glob('yolov5/data/solar_panels/labels/test/*.txt')
predicted_labels = glob.glob('results/yolov5l_100/labels/*.txt')
# predicted_labels = glob.glob('results/*/labels/*.txt')
r_sum, p_sum = 0, 0
for label in real_labels:
for plabel in predicted_labels:
if label.split('/')[-1] == plabel.split('/')[-1]:
real, pred = np.loadtxt(label), np.loadtxt(plabel)
print(f'R:{len(real)} P:{len(pred)} -> {label.split("/")[-1]} {"!!" if len(real) != len(pred) else ""}')
r_sum += len(real)
p_sum += len(pred)
print(r_sum)
print(p_sum)
print(p_sum/r_sum)
def yolo_testing(**kwargs):
params = get_yolo_params(**kwargs)
yolo_eval.run(**params)
def get_yolo_params(name, source, project='results', model='best.pt', device='cuda:0'):
return {
'source': source,
'weights': model,
'imgsz': [256, 256],
'save_txt': True,
'project': project,
'name': name,
'evolove': 300,
'line_thickness': 2,
'hide_labels': True,
'device': device
}
def main():
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
mname = 'yolov5s_100'
yolo_testing(
name=f'{mname}_tiles2',
source='yolo/yolov5/data/solar_panels/images/test', # yolov5/data/solar_panels/tiles2
project='results',
model=f'models/{mname}/weights/best.pt',
device=device
)
# compute_labels_accuracy()
if __name__ == '__main__':
main()