当数据集是时频图时可能有一个尴尬的问题,就是数据集制作好后,发现有白边。
其实这也不影响训练模型,可能对模型训练效果的影响也是微乎其微的,于是大多数情况我会选择直接用整张图片训练模型。但是,有的情况下,去掉白边模型训练效果好,不去白边模型某个类别效果就不好。比如图中的BPSK和Frank信号。
一开始我设置416*416的神经网络输入大小,甚至BPSK,Frank信号检测出来的概率特别低,10个中有2个的样子?上面的检测结果是640*640大小训练出来的,虽然信号检测出来的,BPSK,Frank这种特别窄的信号置信度很低,见下图。
这种情况白边就不是可留可不留的了,是必须要去掉。白边占的面积还是挺大的。
裁剪图片并不难,难的是还要对应修正labels中的数值。于是,我写了一个更正程序,既可以裁剪图片,也可以修正labels。
#作者:zhouzhichao
#时间:25年7月4日
#功能:裁剪数据集的图片和调整对应标签数值import os
from PIL import Image, ImageDraw# origin_img_path = ""
# target_img_path = ""
# origin_label_path = ""
# target_label_path = ""origin_width = 875
origin_height = 656x1 = 114
x2 = 791
y1 = 49
y2 = 583after_cut_width = x2 - x1
after_cut_height = y2 - y1def cut_img(target_path):# 设置文件夹路径folder_path = target_pathoutput_folder = target_path# 如果输出文件夹不存在,则创建if not os.path.exists(output_folder):os.makedirs(output_folder)# 获取文件夹中的所有图片for filename in os.listdir(folder_path):if filename.endswith(('.png', '.jpg', '.jpeg')): # 根据需要修改图片格式img_path = os.path.join(folder_path, filename)img = Image.open(img_path)# 裁剪区域:左上角(114, 49)到右下角(791, 583)cropped_img = img.crop((x1, y1, x2, y2))# 保存裁剪后的图片到输出文件夹output_path = os.path.join(output_folder, filename)cropped_img.save(output_path)print("裁剪完成!")def modify_label(labels_folder):output_folder = labels_folderfor filename in os.listdir(labels_folder):if filename.endswith('.txt'):txt_path = os.path.join(labels_folder, filename)with open(txt_path, 'r') as file:lines = file.readlines()# 修改后的数据将存储在这里modified_lines = []for line in lines:parts = line.strip().split() # 拆分每一行class_id = parts[0]x_center = float(parts[1])y_center = float(parts[2])width = float(parts[3])height = float(parts[4])# 修改第2列为0.5,第4列为1modified_y_center = (y_center*origin_height-y1)/after_cut_heightmodified_y_height = height*origin_height/after_cut_height# 拼接修改后的行modified_line = f"{class_id} {0.5:.3f} {modified_y_center:.3f} {1:.3f} {modified_y_height:.3f}\n"modified_lines.append(modified_line)# 保存修改后的文件output_txt_path = os.path.join(output_folder, filename)with open(output_txt_path, 'w') as output_file:output_file.writelines(modified_lines)def watch(images_folder,labels_folder,output_folder):for filename in os.listdir(labels_folder):if filename.endswith('.txt'):txt_path = os.path.join(labels_folder, filename)# 获取对应的图片文件名img_filename = filename.replace('.txt', '.jpg')img_path = os.path.join(images_folder, img_filename)# 打开图片img = Image.open(img_path)draw = ImageDraw.Draw(img)# 读取标签文件with open(txt_path, 'r') as file:lines = file.readlines()# 遍历每一行标签,绘制矩形框for line in lines:parts = line.strip().split() # 拆分每一行class_id = int(parts[0])x_center = float(parts[1]) * img.widthy_center = float(parts[2]) * img.heightwidth = float(parts[3]) * img.widthheight = float(parts[4]) * img.height# 计算矩形框的左上角和右下角坐标x1 = x_center - width / 2y1 = y_center - height / 2x2 = x_center + width / 2y2 = y_center + height / 2# 绘制矩形框,使用红色边框draw.rectangle([x1, y1, x2, y2], outline="white", width=2)# 保存带框的图片output_img_path = os.path.join(output_folder, img_filename)img.save(output_img_path)if __name__ == "__main__":img_path = "D:\english\yolov7\datasets_higher_cut\images\\train"label_path = "D:\english\yolov7\datasets_higher_cut\labels\\train"output_img_path = "D:\english\yolov7\datasets_higher_cut\watch"# cut_img(img_path)# modify_label(label_path)watch(img_path, label_path, output_img_path)
其中,cut_img是裁剪图片的,modify_label是更正标签的,watch是检测更正结果的。
①裁剪效果:
每张图片原本大小是875*656,把左上角114,49到右下角的791,583,覆盖原图。
②label更正效果,原标签:
更正后标签:
主要是把这些数字中的第2列都改成0.5,第4列改成1,第3列改成原来的数值称原图片高度减去y1后除以裁剪后的图片高度。最后一列改成原值乘以原图高度除以裁剪后的图片高度。
③检查效果
最后,用这个程序需要注意,对一个文件架不能使用两次,那图片不就被裁了2次嘛,label的数值不就改乱了嘛。