|
|
|
@ -71,7 +71,7 @@ class DetectionValidator(BaseValidator):
|
|
|
|
|
|
|
|
|
|
def update_metrics(self, preds, batch):
|
|
|
|
|
# Metrics
|
|
|
|
|
for si, (pred) in enumerate(preds):
|
|
|
|
|
for si, pred in enumerate(preds):
|
|
|
|
|
labels = self.targets[self.targets[:, 0] == si, 1:]
|
|
|
|
|
nl, npr = labels.shape[0], pred.shape[0] # number of labels, predictions
|
|
|
|
|
shape = batch["ori_shape"][si]
|
|
|
|
@ -103,11 +103,11 @@ class DetectionValidator(BaseValidator):
|
|
|
|
|
self.confusion_matrix.process_batch(predn, labelsn)
|
|
|
|
|
self.stats.append((correct_bboxes, pred[:, 4], pred[:, 5], labels[:, 0])) # (conf, pcls, tcls)
|
|
|
|
|
|
|
|
|
|
# TODO: Save/log
|
|
|
|
|
'''
|
|
|
|
|
if self.args.save_txt:
|
|
|
|
|
save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
|
|
|
|
|
'''
|
|
|
|
|
# Save
|
|
|
|
|
if self.args.save_json:
|
|
|
|
|
self.pred_to_json(predn, batch["im_file"][si])
|
|
|
|
|
# if self.args.save_txt:
|
|
|
|
|
# save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
|
|
|
|
|
|
|
|
|
|
def get_stats(self):
|
|
|
|
|
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*self.stats)] # to numpy
|
|
|
|
@ -197,13 +197,12 @@ class DetectionValidator(BaseValidator):
|
|
|
|
|
fname=self.save_dir / f'val_batch{ni}_pred.jpg',
|
|
|
|
|
names=self.names) # pred
|
|
|
|
|
|
|
|
|
|
def pred_to_json(self, preds, batch):
|
|
|
|
|
for i, f in enumerate(batch["im_file"]):
|
|
|
|
|
stem = Path(f).stem
|
|
|
|
|
def pred_to_json(self, predn, filename):
|
|
|
|
|
stem = Path(filename).stem
|
|
|
|
|
image_id = int(stem) if stem.isnumeric() else stem
|
|
|
|
|
box = ops.xyxy2xywh(preds[i][:, :4]) # xywh
|
|
|
|
|
box = ops.xyxy2xywh(predn[:, :4]) # xywh
|
|
|
|
|
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
|
|
|
|
|
for p, b in zip(preds[i].tolist(), box.tolist()):
|
|
|
|
|
for p, b in zip(predn.tolist(), box.tolist()):
|
|
|
|
|
self.jdict.append({
|
|
|
|
|
'image_id': image_id,
|
|
|
|
|
'category_id': self.class_map[int(p[5])],
|
|
|
|
|