forked from ardeal/yolo_nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_yolonano.py
165 lines (136 loc) · 6.42 KB
/
test_yolonano.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import division
from network import *
from utils.utils import *
from utils.datasets import *
from utils.parse_config import *
import os
import sys
import time
import datetime
import argparse
import tqdm
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
from torch.autograd import Variable
import torch.optim as optim
import cv2
import copy
from PIL import Image, ImageDraw
# as pil
# import PIL as pil
from network.yolo_nano_network import YOLONano
from opt import opt
import datetime
def evaluate(model, path, iou_thres, conf_thres, nms_thres, img_size, batch_size):
model.eval()
dataset = ListDataset(path, img_size=img_size, augment=False, multiscale=False)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False, num_workers=1, collate_fn=dataset.collate_fn)
Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
labels = []
sample_metrics = [] # List of tuples (TP, confs, pred)
counter = 0
for batch_i, (_, imgs, targets) in enumerate(tqdm.tqdm(dataloader, desc="Detecting objects")):
# Extract labels
labels += targets[:, 1].tolist()
# Rescale target
targets[:, 2:] = xywh2xyxy(targets[:, 2:])
targets[:, 2:] *= img_size
"""
# ------------------------------------use opencv to show image and result -------------------------------------------------------
imagei = imgs.mul(255).byte()
imagei = imagei.cpu().numpy().squeeze(0).transpose((1, 2, 0))
image_cpu = copy.deepcopy(np.array(imagei).astype(np.uint8)) #copy.deepcopy(imagei)
image_cpu = cv2.cvtColor(np.asarray(image_cpu), cv2.COLOR_RGB2BGR)
# --------show image and ground truth(label) to confirm whether the label and label format is correct or no --------------------------
image_tt = copy.deepcopy(image_cpu)
for i in range(len(targets)):
pt1 = (int(targets[i][2].numpy()), int(targets[i][3].numpy()))
pt2 = (int(targets[i][4].numpy()), int(targets[i][5].numpy()))
cv2.rectangle(image_tt, pt1, pt2, (0, 0, 255), 1)
cv2.imshow("ground truth", image_tt)
# cv2.waitKey(0)
"""
# --------------------------------done----------------------------------------------outputs[..., 5:].detach().cpu().numpy()----------------------
imgs = Variable(imgs.type(Tensor), requires_grad=False)
# t0 = datetime.datetime.utcnow()
with torch.no_grad():
outputs = model(imgs)
outputs = non_max_suppression(outputs, conf_thres=conf_thres, nms_thres=nms_thres)
# t1 = datetime.datetime.utcnow()
# tt = t1 - t0
# tum = tt.seconds * 1000000 + tt.microseconds
# print()
# print('time needed for 1 image == {:.5f} ms'.format(tum))
"""
# ------------------------------------use opencv to show image and result -------------------------------------------------------
output = outputs[0]
if output is None:
continue
output_cpu = output.numpy()
pred_boxes = output_cpu[:, :4]
pred_scores = output_cpu[:, 4]
pred_labels = output_cpu[:, -1]
for i in range(pred_boxes.shape[0]):
if pred_scores[i] > 0.5:
pt1 = (int(pred_boxes[i][0]), int(pred_boxes[i][1]))
pt2 = (int(pred_boxes[i][2]), int(pred_boxes[i][3]))
cv2.rectangle(image_cpu, pt1, pt2, (0, 255, 0), 1)
cv2.imshow("predicting output", image_cpu)
counter += 1
imagename = 'image_{}.jpg'.format(counter)
savedimagepath = os.path.join(r'C:\doc\code_python\yolo\yolo_nano\images_output', imagename)
# cv2.imwrite(savedimagepath, image_cpu)
cv2.waitKey(0)
"""
# -------------------------------------use PIL functions to draw rect and show image -------------->
# image_ndarray = np.squeeze(imgs.cpu())
# image = transforms.ToPILImage()(image_ndarray).convert('RGB')
# drawimage = ImageDraw.Draw(image)
# output = outputs[0]
# if output is None:
# continue
# output_cpu = output.numpy()
# pred_boxes = output_cpu[:, :4]
# pred_scores = output_cpu[:, 4]
# pred_labels = output_cpu[:, -1]
#
# for i in range(pred_boxes.shape[0]):
# if pred_scores[i] > 0.5:
# pt1 = (int(pred_boxes[i][0]), int(pred_boxes[i][1]))
# pt2 = (int(pred_boxes[i][2]), int(pred_boxes[i][3]))
# drawimage.rectangle((pt1[0], pt1[1], pt2[0], pt2[1]), fill=None, outline='red')
# image.show()
# --------------------------------done--------------------------------------------------------------------
sample_metrics += get_batch_statistics(outputs, targets, iou_threshold=iou_thres)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 0
# Concatenate sample statistics
if len(sample_metrics) != 0:
true_positives, pred_scores, pred_labels = [np.concatenate(x, 0) for x in list(zip(*sample_metrics))]
precision, recall, AP, f1, ap_class = ap_per_class(true_positives, pred_scores, pred_labels, labels)
else:
precision = np.array([0.0, 0])
recall = np.array([0.0,0])
AP = np.array([0.0,0])
f1 = np.array([0.0,0])
ap_class = np.array([1,2]) #np.unique(labels).astype('int32')
return precision, recall, AP, f1, ap_class
if __name__ == "__main__":
print(opt)
print('cuda is available == {}'.format(torch.cuda.is_available()))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
opt.cpu_or_gpu = device
os.makedirs("output", exist_ok=True)
os.makedirs("checkpoints", exist_ok=True)
data_config = parse_data_config(opt.data_config)
valid_path = data_config["valid"]
class_names = load_classes(data_config["names"])
model = YOLONano(opt.num_classes, opt.image_size).to(device)
model.load_state_dict(torch.load(opt.pth_path))
evaluate(model, valid_path, 0.5, opt.conf_thres, opt.nms_thres, opt.image_size, batch_size=1)
# evaluate(model, path=valid_path, iou_thres=0.5, conf_thres=0.5, nms_thres=0.5, img_size=opt.img_size, batch_size=1, )
# with torch.no_grad():
# outputs = model(imgs)
# outputs = non_max_suppression(outputs, conf_thres=conf_thres, nms_thres=nms_thres)
aaaaaaaaaaaaaaaa=0