Targets scaling bug fix (#1529)
Browse files- test.py +6 -5
- utils/plots.py +3 -3
test.py
CHANGED
@@ -15,7 +15,7 @@ from utils.general import coco80_to_coco91_class, check_dataset, check_file, che
|
|
15 |
non_max_suppression, scale_coords, xyxy2xywh, xywh2xyxy, set_logging, increment_path
|
16 |
from utils.loss import compute_loss
|
17 |
from utils.metrics import ap_per_class, ConfusionMatrix
|
18 |
-
from utils.plots import plot_images, output_to_target
|
19 |
from utils.torch_utils import select_device, time_synchronized
|
20 |
|
21 |
|
@@ -102,7 +102,6 @@ def test(data,
|
|
102 |
img /= 255.0 # 0 - 255 to 0.0 - 1.0
|
103 |
targets = targets.to(device)
|
104 |
nb, _, height, width = img.shape # batch size, channels, height, width
|
105 |
-
targets[:, 2:] *= torch.Tensor([width, height, width, height]).to(device)
|
106 |
|
107 |
with torch.no_grad():
|
108 |
# Run model
|
@@ -115,8 +114,9 @@ def test(data,
|
|
115 |
loss += compute_loss([x.float() for x in train_out], targets, model)[1][:3] # box, obj, cls
|
116 |
|
117 |
# Run NMS
|
118 |
-
|
119 |
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_txt else [] # for autolabelling
|
|
|
120 |
output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres, labels=lb)
|
121 |
t1 += time_synchronized() - t
|
122 |
|
@@ -324,8 +324,9 @@ if __name__ == '__main__':
|
|
324 |
y = [] # y axis
|
325 |
for i in x: # img-size
|
326 |
print('\nRunning %s point %s...' % (f, i))
|
327 |
-
r, _, t = test(opt.data, weights, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json
|
|
|
328 |
y.append(r + t) # results and times
|
329 |
np.savetxt(f, y, fmt='%10.4g') # save
|
330 |
os.system('zip -r study.zip study_*.txt')
|
331 |
-
|
|
|
15 |
non_max_suppression, scale_coords, xyxy2xywh, xywh2xyxy, set_logging, increment_path
|
16 |
from utils.loss import compute_loss
|
17 |
from utils.metrics import ap_per_class, ConfusionMatrix
|
18 |
+
from utils.plots import plot_images, output_to_target, plot_study_txt
|
19 |
from utils.torch_utils import select_device, time_synchronized
|
20 |
|
21 |
|
|
|
102 |
img /= 255.0 # 0 - 255 to 0.0 - 1.0
|
103 |
targets = targets.to(device)
|
104 |
nb, _, height, width = img.shape # batch size, channels, height, width
|
|
|
105 |
|
106 |
with torch.no_grad():
|
107 |
# Run model
|
|
|
114 |
loss += compute_loss([x.float() for x in train_out], targets, model)[1][:3] # box, obj, cls
|
115 |
|
116 |
# Run NMS
|
117 |
+
targets[:, 2:] *= torch.Tensor([width, height, width, height]).to(device) # to pixels
|
118 |
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_txt else [] # for autolabelling
|
119 |
+
t = time_synchronized()
|
120 |
output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres, labels=lb)
|
121 |
t1 += time_synchronized() - t
|
122 |
|
|
|
324 |
y = [] # y axis
|
325 |
for i in x: # img-size
|
326 |
print('\nRunning %s point %s...' % (f, i))
|
327 |
+
r, _, t = test(opt.data, weights, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json,
|
328 |
+
plots=False)
|
329 |
y.append(r + t) # results and times
|
330 |
np.savetxt(f, y, fmt='%10.4g') # save
|
331 |
os.system('zip -r study.zip study_*.txt')
|
332 |
+
plot_study_txt(f, x) # plot
|
utils/plots.py
CHANGED
@@ -140,7 +140,7 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
|
|
140 |
labels = image_targets.shape[1] == 6 # labels if no conf column
|
141 |
conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred)
|
142 |
|
143 |
-
if boxes.max() <= 1: # if normalized
|
144 |
boxes[[0, 2]] *= w # scale to pixels
|
145 |
boxes[[1, 3]] *= h
|
146 |
boxes[[0, 2]] += block_x
|
@@ -224,7 +224,7 @@ def plot_study_txt(f='study.txt', x=None): # from utils.plots import *; plot_st
|
|
224 |
ax = ax.ravel()
|
225 |
|
226 |
fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True)
|
227 |
-
for f in ['study/
|
228 |
y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T
|
229 |
x = np.arange(y.shape[1]) if x is None else np.array(x)
|
230 |
s = ['P', 'R', '[email protected]', '[email protected]:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)']
|
@@ -368,7 +368,7 @@ def plot_results(start=0, stop=0, bucket='', id=(), labels=(), save_dir=''):
|
|
368 |
y[y == 0] = np.nan # don't show zero loss values
|
369 |
# y /= y[0] # normalize
|
370 |
label = labels[fi] if len(labels) else f.stem
|
371 |
-
ax[i].plot(x, y, marker='.', label=label, linewidth=
|
372 |
ax[i].set_title(s[i])
|
373 |
# if i in [5, 6, 7]: # share train and val loss y axes
|
374 |
# ax[i].get_shared_y_axes().join(ax[i], ax[i - 5])
|
|
|
140 |
labels = image_targets.shape[1] == 6 # labels if no conf column
|
141 |
conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred)
|
142 |
|
143 |
+
if boxes.shape[1] and boxes.max() <= 1: # if normalized
|
144 |
boxes[[0, 2]] *= w # scale to pixels
|
145 |
boxes[[1, 3]] *= h
|
146 |
boxes[[0, 2]] += block_x
|
|
|
224 |
ax = ax.ravel()
|
225 |
|
226 |
fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True)
|
227 |
+
for f in ['study/study_coco_%s.txt' % x for x in ['yolov5s', 'yolov5m', 'yolov5l', 'yolov5x']]:
|
228 |
y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T
|
229 |
x = np.arange(y.shape[1]) if x is None else np.array(x)
|
230 |
s = ['P', 'R', '[email protected]', '[email protected]:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)']
|
|
|
368 |
y[y == 0] = np.nan # don't show zero loss values
|
369 |
# y /= y[0] # normalize
|
370 |
label = labels[fi] if len(labels) else f.stem
|
371 |
+
ax[i].plot(x, y, marker='.', label=label, linewidth=2, markersize=8)
|
372 |
ax[i].set_title(s[i])
|
373 |
# if i in [5, 6, 7]: # share train and val loss y axes
|
374 |
# ax[i].get_shared_y_axes().join(ax[i], ax[i - 5])
|