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])

View File

@ -0,0 +1,50 @@
from PIL import Image
import os
def merge_imgs(path1, path2, save_path):
for img_name in os.listdir(path1):
# img_dir1 = os.path.join(path1, img_dir)
# if os.path.isdir(img_dir1):
# img_dir2 = os.path.join(path2, img_dir)
# for img_name in os.listdir(img_dir1):
try:
img1_path = os.path.join(path1, img_name)
img2_path = os.path.join(path2, img_name)
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
print('img1_path',img1)
print('img2_path', img2)
assert img1.height == img2.height
new_img = Image.new('RGB', (img1.width + img2.width+10, img1.height))
# print('new_img', new_img)
new_img.paste(img1, (0, 0))
new_img.paste(img2, (img1.width+10, 0))
# save_dir = os.path.join(save_path, img_name)
os.makedirs(save_path, exist_ok=True)
img_save = os.path.join(save_path, img_name)
# print('img-save', img_save)
new_img.save(img_save)
except Exception as e:
print(e)
#print(img_name)
#path1 = '/home/yujia/yj/yolov5-6.1/0518_cls10_v5s_new_delTP0.5/labelFn_5/'
# path1 = 'predict5_ori_v10s/'
# path1 = 'predict_best0524/'
# path2 = 'predict_0613_epoch27/'
# path1 = 'predict_labels110_0524/'
# path2 = 'predict_labels110_0613/'
path1 = '/home/lc/ieemoo-ai-gift/confusion_gift_cls4_0.45/FN/FN_3'
path2 = '/home/lc/ieemoo-ai-gift/confusion_gift_cls4_0.45/allBox/allBox_1'
#save_path = '/home/yujia/yj/yolov5-6.1/0518_cls10_v5s_new_delTP0.5/labelFn_5_allBox_merge/'
save_path = '/home/lc/ieemoo-ai-gift/confusion_gift_cls4_0.45/FN/FN_3_joint/'
# os.makedirs(save_path, exist_ok=True)
merge_imgs(path1, path2, save_path)

View File

@ -389,6 +389,7 @@ class ConfusionMatrix:
cls = detect_cur_cpu[5]
c = int(cls)
label = f'{names[c]} {conf:.2f} iou:{float(iou):.2f}'
print(">>>>>>>>>>>>>>>>>>> label: {} C: {}".format(label, c))
if fp_flag:
annotator.box_label(xyxy, label, color=(125, 0, 125)) ##fp iou匹配上类别错误 紫色框
else:
@ -465,7 +466,7 @@ class ConfusionMatrix:
annotators_tp = self.create_annotator(images_tp, Annotator, names)
annotators_all = self.create_annotator(images_all, Annotator, names)
### 新建不同检测类别保存的文件夹,文件夹名称为类别索引
date_path = 'confusion_0717_cls10_' + str(self.iou_thres)
date_path = 'confusion_gift_cls4_' + str(self.iou_thres)
paths_fn = self.makdirs_file(date_path, img_name, self.nc, str_c='FN')
paths_fp = self.makdirs_file(date_path, img_name, self.nc, str_c='FP')
paths_fp_bg = self.makdirs_file(date_path, img_name, self.nc, str_c='FP_bg')
@ -485,7 +486,7 @@ class ConfusionMatrix:
cls_flag_list_tp = [False for _ in range(self.nc)] ###混淆矩阵斜对角线上类别flag
## 混淆矩阵可视化 flag 设置
save_oneImg = False ### 将所有pred_box与gt_box匹配结果画在一张图片上
save_oneImg = True #pred_box与gt_box匹配结果画在一张图片上
save_byClass = True ### 将所有pred_box与gt_box匹配结果按box类别分类保存其中tp、fn、fp按gt_box类别划分fp_bg按pred_box划分
cls_flag_list = []
###=========================

View File

@ -84,6 +84,7 @@ class Colors:
def __call__(self, i, bgr=False):
"""Converts hex color codes to RGB values."""
i=0
c = self.palette[int(i) % self.n]
return (c[2], c[1], c[0]) if bgr else c