一、代码结构展示
昨天的笔记中讲解了csv_utils.py文件的内容,今天的笔记主要讲解接下来的eval_utils.py文件。
二、代码功能介绍
这个文件的代码包含了几个函数,用于图像处理和结果保存,其中包括调整分辨率、归一化得分以及保存分类和分割结果。
-
specify_resolution(image_list, score_list, mask_list, resolution=(400,400))
:-
这个函数用于调整输入的图像、得分和掩码的分辨率。它接受四个参数:图像列表、得分列表、掩码列表和分辨率(可选参数,默认为(400, 400))。
-
在函数内部,使用
zip()
函数将图像、得分和掩码逐个取出,并使用 OpenCV 的cv2.resize()
函数将它们调整到指定的分辨率。插值方法为cv2.INTER_CUBIC
(用于图像和得分)和cv2.INTER_NEAREST
(用于掩码)。 -
调整后的图像、得分和掩码分别存储在
resize_image
、resize_score
和resize_mask
列表中,并最终返回这三个列表。
-
-
normalize(scores)
:-
这个函数用于对输入的得分进行归一化处理。它接受一个得分列表作为参数。
-
在函数内部,首先计算得分列表中的最大值和最小值,然后使用这两个值将得分列表进行归一化处理,计算公式为
(scores - min_value) / (max_value - min_value)
。 -
归一化后的得分列表被返回。
-
-
save_single_result(classification_score, segmentation_score, root_dir, shot_name, experiment_indx, subset_name, defect_type, name, use_defect_type)
:-
这个函数用于保存单个结果,包括分类得分和分割得分。
-
它接受了分类得分、分割得分、保存根目录、抽样名称、实验索引、子集名称、缺陷类型、结果名称和是否使用缺陷类型作为参数。
-
在函数内部,根据是否使用缺陷类型,构建保存结果的文件路径,并创建相应的目录。然后,将分类得分保存为
.txt
文件,将分割得分保存为.npz
文件。
-
-
save_results(classification_score_list, segmentation_score_list, root_dir, shot_name, experiment_indx, name_list, use_defect_type)
:-
这个函数用于保存多个结果,循环遍历分类得分、分割得分和结果名称列表,并调用
save_single_result
函数保存每个结果。
-
三、代码逐行注释
# 这段代码包含了几个函数,用于图像处理和结果保存。
# 这些函数主要用于图像处理和结果保存,其中包括调整分辨率、归一化得分以及保存分类和分割结果
import cv2
import os
import numpy as np
def specify_resolution(image_list, score_list, mask_list, resolution: tuple=(400,400)):# 这个函数接受三个参数:图像列表、得分列表、掩码列表、分辨率(作为可选参数,默认值为(400,400))
resize_image = []
resize_score = []
resize_mask = []
# print(resolution)
for image, score, mask in zip(image_list, score_list, mask_list): # 使用 zip() 函数将图像、得分和掩码逐个取出
image = cv2.resize(image, (resolution[0], resolution[1]), interpolation=cv2.INTER_CUBIC) # 使用 OpenCV 的 cv2.resize() 函数将它们调整到指定的分辨率
score = cv2.resize(score, (resolution[0], resolution[1]), interpolation=cv2.INTER_CUBIC) # 插值方法为 cv2.INTER_CUBIC(用于图像和得分)
mask = cv2.resize(mask, (resolution[0], resolution[1]), interpolation=cv2.INTER_NEAREST) # cv2.INTER_NEAREST(用于掩码)
resize_image += [image] # 调整后的图像、得分和掩码分别存储在 resize_image、resize_score 和 resize_mask 列表中,并最终返回这三个列表
resize_score += [score]
resize_mask += [mask]
return resize_image, resize_score, resize_mask
def normalize(scores): # 这个函数用于对输入的得分进行归一化处理。它接受一个得分列表作为参数
max_value = np.max(scores) # 首先计算得分列表中的最大值和最小值
min_value = np.min(scores)
norml_scores = (scores - min_value) / (max_value - min_value) # 然后使用这两个值将得分列表进行归一化处理
return norml_scores # 归一化后的得分列表被返回
def save_single_result(classification_score, segmentation_score, root_dir, shot_name, experiment_indx, subset_name, defect_type, name, use_defect_type):
# 这个函数用于保存单个结果,包括分类得分和分割得分。它接受了分类得分、分割得分、保存根目录、抽样名称、实验索引、子集名称、缺陷类型、结果名称和是否使用缺陷类型作为参数。
if use_defect_type: # 根据是否使用缺陷类型,构建保存结果的文件路径,并创建相应的目录。
# mvtec2d mvtec3d
save_dir = os.path.join(root_dir, shot_name, experiment_indx, subset_name, defect_type)
else:
# visa
save_dir = os.path.join(root_dir, shot_name, experiment_indx, subset_name)
os.makedirs(save_dir, exist_ok=True)
classification_dir = os.path.join(save_dir, 'classification')
segmentation_dir = os.path.join(save_dir, 'segmentation')
os.makedirs(classification_dir, exist_ok=True) # 构建保存结果的文件路径
os.makedirs(segmentation_dir, exist_ok=True)
# 将分类得分保存为 .txt 文件,将分割得分保存为 .npz 文件。
classification_path = os.path.join(classification_dir, f'{name}.txt')
segmentation_path = os.path.join(segmentation_dir, f'{name}.npz')
with open(classification_path, "w") as f:
f.write(f'{classification_score:.5f}')
segmentation_score = np.round(segmentation_score * 255).astype(np.uint8)
np.savez_compressed(segmentation_path, img=segmentation_score)
def save_results(classification_score_list, segmentation_score_list, root_dir, shot_name, experiment_indx, name_list, use_defect_type):
# 这个函数用于保存多个结果,循环遍历分类得分、分割得分和结果名称列表,并调用 save_single_result 函数保存每个结果
for classification_score, segmentation_score, full_name in zip(classification_score_list,
segmentation_score_list,
name_list):
subset_name, defect_type, name = full_name.split('-')
save_single_result(classification_score, segmentation_score, root_dir, shot_name, experiment_indx, subset_name, defect_type, name, use_defect_type)