merge_imgs

This commit is contained in:
2025-06-23 11:29:52 +08:00
parent a17373d576
commit 56b161dc49
8 changed files with 182 additions and 48 deletions

View File

@ -3,6 +3,7 @@
import os
from pathlib import Path
import cv2
import numpy as np
import torch
@ -10,8 +11,13 @@ from ultralytics.data import build_dataloader, build_yolo_dataset, converter
from ultralytics.engine.validator import BaseValidator
from ultralytics.utils import LOGGER, ops
from ultralytics.utils.checks import check_requirements
from ultralytics.utils.metrics import ConfusionMatrix, DetMetrics, box_iou
from ultralytics.utils.plotting import output_to_target, plot_images
# from ultralytics.utils.metrics import ConfusionMatrix, DetMetrics, box_iou
from ultralytics.utils.metrics_confusion_visual import ConfusionMatrix, DetMetrics, box_iou
from ultralytics.utils.plotting import output_to_target, plot_images, Colors
### val时可视化图片增加
from ultralytics.utils.plotting import Annotator, Colors
colors = Colors()
class DetectionValidator(BaseValidator):
@ -146,8 +152,40 @@ class DetectionValidator(BaseValidator):
# Evaluate
if nl:
stat["tp"] = self._process_batch(predn, bbox, cls)
# ####===========增加匹配结果返回==================
# stat["tp"], matches, iou_list = self._process_batch(predn, bbox, cls) ### 生成gt和pred box匹配
# colors = Colors()
# if len(matches) > 0: ## 有匹配结果
# print('len(match)', len(matches))
# indl = matches[:, 0] ## label index
# indp = matches[:, 1] ## pred index
# # print('img', img)
# # img_name = batch['im_file']
# # print('img_name', img_name[0])
# # img = cv2.imread(img_name[0])
# img = cv2.imread(batch['im_file'][0])
# # annotator = Annotator(img, line_width=3)
# annotator = Annotator(img, line_width=3, font_size=3, pil=True, example=self.names)
# for ind, (*xyxy, conf, p_cls) in enumerate(predn):
# if ind in indp:
# p_ind = list(indp).index(ind) ## ind在match中的索引
# t_ind = indl[p_ind]
# iou = iou_list[t_ind, p_ind]
# conf_c = conf.cpu().item()
# label = self.names[int(p_cls)] + str(conf_c) + '_iou' + str(f'{iou:.2f}')
# annotator.box_label(xyxy, label, color=(128, 0, 128))
#
# img = annotator.result()
# path_save = 'tp'
# os.makedirs(path_save, exist_ok=True)
# save_path1 = os.path.join(path_save, batch['im_file'][0].split('/')[-1])
# print('save_path', save_path1)
# cv2.imwrite(save_path1, img)
####==================================
if self.args.plots:
self.confusion_matrix.process_batch(predn, bbox, cls)
###=======修改可视化匹配框=============
# self.confusion_matrix.process_batch(predn, bbox, cls)
self.confusion_matrix.process_batch(predn, bbox, cls, batch['im_file'][0], self.names, Annotator, colors)
for k in self.stats.keys():
self.stats[k].append(stat[k])

View File

@ -3,7 +3,6 @@
import os
from pathlib import Path
import cv2
import numpy as np
import torch
@ -11,13 +10,8 @@ from ultralytics.data import build_dataloader, build_yolo_dataset, converter
from ultralytics.engine.validator import BaseValidator
from ultralytics.utils import LOGGER, ops
from ultralytics.utils.checks import check_requirements
# from ultralytics.utils.metrics import ConfusionMatrix, DetMetrics, box_iou
from ultralytics.utils.metrics_confusion_visual import ConfusionMatrix, DetMetrics, box_iou
from ultralytics.utils.plotting import output_to_target, plot_images, Colors
### val时可视化图片增加
from ultralytics.utils.plotting import Annotator, Colors
colors = Colors()
from ultralytics.utils.metrics import ConfusionMatrix, DetMetrics, box_iou
from ultralytics.utils.plotting import output_to_target, plot_images
class DetectionValidator(BaseValidator):
@ -152,40 +146,8 @@ class DetectionValidator(BaseValidator):
# Evaluate
if nl:
stat["tp"] = self._process_batch(predn, bbox, cls)
# ####===========增加匹配结果返回==================
# stat["tp"], matches, iou_list = self._process_batch(predn, bbox, cls) ### 生成gt和pred box匹配
# colors = Colors()
# if len(matches) > 0: ## 有匹配结果
# print('len(match)', len(matches))
# indl = matches[:, 0] ## label index
# indp = matches[:, 1] ## pred index
# # print('img', img)
# # img_name = batch['im_file']
# # print('img_name', img_name[0])
# # img = cv2.imread(img_name[0])
# img = cv2.imread(batch['im_file'][0])
# # annotator = Annotator(img, line_width=3)
# annotator = Annotator(img, line_width=3, font_size=3, pil=True, example=self.names)
# for ind, (*xyxy, conf, p_cls) in enumerate(predn):
# if ind in indp:
# p_ind = list(indp).index(ind) ## ind在match中的索引
# t_ind = indl[p_ind]
# iou = iou_list[t_ind, p_ind]
# conf_c = conf.cpu().item()
# label = self.names[int(p_cls)] + str(conf_c) + '_iou' + str(f'{iou:.2f}')
# annotator.box_label(xyxy, label, color=(128, 0, 128))
#
# img = annotator.result()
# path_save = 'tp'
# os.makedirs(path_save, exist_ok=True)
# save_path1 = os.path.join(path_save, batch['im_file'][0].split('/')[-1])
# print('save_path', save_path1)
# cv2.imwrite(save_path1, img)
####==================================
if self.args.plots:
###=======修改可视化匹配框=============
# self.confusion_matrix.process_batch(predn, bbox, cls)
self.confusion_matrix.process_batch(predn, bbox, cls, batch['im_file'][0], self.names, Annotator, colors)
self.confusion_matrix.process_batch(predn, bbox, cls)
for k in self.stats.keys():
self.stats[k].append(stat[k])