实战指南:将VDEAI多光谱数据集高效转换为YOLO格式
1. 理解VDEAI多光谱数据集与YOLO格式VDEAI数据集是一个包含可见光RGB和红外IR图像对的多光谱车辆数据集常用于自动驾驶和军事侦察等场景。每张图片都配有详细的标注文件记录着车辆的位置、类别等信息。而YOLO格式则是当前目标检测领域最流行的标注格式之一采用归一化坐标和类别ID的简洁表示。多光谱数据处理的特殊之处在于需要同时处理两种模态的图像。比如在VDEAI数据集中0000000_co.png和0000000_ir.png就是同一场景下的可见光与红外图像对。这种配对关系必须在格式转换过程中保持否则会导致训练时数据不匹配的问题。YOLO格式的标注文件是.txt文本文件每行代表一个物体包含5个数值类别ID、中心点x坐标、中心点y坐标、宽度和高度。所有坐标都是相对于图片宽高的归一化值0到1之间。例如0 0.5 0.5 0.2 0.3 1 0.3 0.7 0.1 0.12. 环境准备与文件结构首先需要确保你的开发环境已经安装以下Python库pandas用于处理表格数据Pillow用于获取图片尺寸scikit-learn用于数据集划分可以通过以下命令快速安装pip install pandas Pillow scikit-learn原始数据集的文件结构通常如下Vehicules1024/ 0000000_co.png 0000000_ir.png 0000001_co.png 0000001_ir.png ... annotation1024_cleaned.txt fold01.txt fold01test.txtannotation1024_cleaned.txt是原始标注文件包含所有车辆的边界框信息。fold01.txt和fold01test.txt则定义了训练集和验证集的图片ID。我们需要将这些原始数据转换为YOLO格式的标准结构train/ images_rgb/ 0000000.png 0000001.png images_ir/ 0000000.png 0000001.png labels/ 0000000.txt 0000001.txt val/ images_rgb/ images_ir/ labels/3. 标注文件解析与坐标转换原始标注文件annotation1024_cleaned.txt的每行包含15个字段最重要的几个是Image_ID图片IDx_centre/y_centre车辆中心坐标corner1_x到corner4_y四个角点坐标class车辆类别我们需要先将这些原始坐标转换为YOLO格式的归一化坐标。核心转换公式是x_centre_norm x_centre / image_width y_centre_norm y_centre / image_height width_norm (max_x - min_x) / image_width height_norm (max_y - min_y) / image_heightPython实现的关键函数如下def normalise_bounding_box_val(df): x_coords [corner1_x, corner2_x, corner3_x, corner4_x] y_coords [corner1_y, corner2_y, corner3_y, corner4_y] # 确保坐标不超出图片范围 for x in x_coords: df[x] np.where(df[x] df[width], df[width], df[x]) df[x] np.where(df[x] 0, 0, df[x]) # 计算归一化边界框 df[max_bbox_width_norm] (df[x_coords].max(axis1)-df[x_coords].min(axis1))/df[width] df[max_bbox_height_norm] (df[y_coords].max(axis1)-df[y_coords].min(axis1))/df[height] # 计算归一化中心点 df[x_centre_norm] df[x_centre]/df[width] df[y_centre_norm] df[y_centre]/df[height] return df4. 类别映射与数据划分VDEAI原始数据有11个车辆类别但通常需要根据实际需求进行合并。例如可以将不常见的车辆类型合并为其他类。以下是一个典型的类别映射方案原始类别映射后类别1 (Car)02 (Truck)123 (Boat)24 (Tractor)35 (Camping Van)411 (Pickup)531 (Plane)67,8,10 (其他)79 (Vans)8实现代码def apply_class_mapping(df): new_data { 1:0, 2:1, 4:3, 5:4, 7:7, 8:7, 9:8, 10:7, 11:5, 23:2, 31:6 } df[class] df[class].replace(new_data) return df数据划分可以直接使用数据集提供的fold01.txt和fold01test.txt它们已经定义了训练集和验证集的图片ID。如果没有预定义划分可以使用scikit-learn的train_test_split进行随机划分from sklearn.model_selection import train_test_split def split_data_train_val_test(df, val_split_ratio): X df.drop([class], axis1) y df[class] X_train, X_val, y_train, y_val train_test_split( X, y, test_sizeval_split_ratio, random_state42, stratifyy) return pd.concat([X_train, y_train], axis1), pd.concat([X_val, y_val], axis1)5. 生成YOLO格式标注文件为每张图片生成对应的YOLO格式.txt文件内容格式为class_id x_center y_center width height实现函数def generate_annotation_per_image(df, img_file_list, annot_output_dir): col_interest [annot_for_img_file, class, x_centre_norm, y_centre_norm, max_bbox_width_norm, max_bbox_height_norm] df df[col_interest] for img_id in img_file_list: temp_df df[df[annot_for_img_file]img_id].drop(annot_for_img_file, axis1) txt_save_path os.path.join(annot_output_dir, img_id) txt_format [%d, %f, %f, %f, %f] np.savetxt(txt_save_path, temp_df.values, fmttxt_format, delimiter )6. 多光谱图像处理技巧处理多光谱数据时需要特别注意保持图像对的对应关系。VDEAI数据集中_co后缀表示可见光图像_ir表示红外图像。在复制图像到训练目录时建议采用以下策略统一命名去掉_co和_ir后缀改用目录区分保持同步确保可见光和红外图像同时被复制相同预处理对两种图像应用相同的尺寸调整实现代码def copy_images_for_training_rgb(annot_file_list, src_img_folder, dest_img_folder): img_file_list [filename.replace(.txt,_co.png) for filename in annot_file_list] for img_file in img_file_list: source_img_path os.path.join(src_img_folder, img_file) renamed_img_file img_file.replace(_co.png,.png) destination_path os.path.join(dest_img_folder, renamed_img_file) image Image.open(source_img_path) image.thumbnail((RESIZE_RES, RESIZE_RES)) image.save(destination_path)7. 完整流程与参数配置整个转换流程的主函数如下关键参数包括输入标注文件路径输出目录结构图片缩放尺寸验证集比例def main_process_annotation_to_yolo(sys_args): # 读取原始标注 columns [Image_ID, x_centre, y_centre, orientation, corner1_x, corner2_x, corner3_x, corner4_x, corner1_y, corner2_y, corner3_y, corner4_y, class, is_contained, is_occluded] data pd.read_table(sys_args.input_annotation_file, delimiter , namescolumns) # 预处理 contained_df data[data[is_contained]1].copy() contained_df[filepath] contained_df[Image_ID].map( lambda x: os.path.join(os.getcwd(), Vehicules1024, str(x).zfill(8) _co.png)) contained_df[width], contained_df[height] get_img_size(contained_df[filepath]) # 坐标转换和类别映射 contained_df normalise_bounding_box_val(contained_df) contained_df apply_class_mapping(contained_df) # 数据划分 train_df, val_df split_data_by_ids(contained_df, sys_args.train_txt, sys_args.val_txt) # 生成YOLO格式 for state, temp_df in [(train, train_df), (val, val_df)]: annot_output_dir sys_args.output_annotation_training_folder if state train else sys_args.output_annotation_validation_folder os.makedirs(annot_output_dir, exist_okTrue) annot_file_list set(temp_df[annot_for_img_file]) generate_annotation_per_image(temp_df, annot_file_list, annot_output_dir) # 复制图像 copy_images_for_training_rgb(annot_file_list, sys_args.input_image_folder, sys_args.output_image_training_folder_rgb if state train else sys_args.output_image_validation_folder_rgb) copy_images_for_training_ir(annot_file_list, sys_args.input_image_folder, sys_args.output_image_training_folder_ir if state train else sys_args.output_image_validation_folder_ir)8. 常见问题与解决方案在实际转换过程中可能会遇到以下典型问题坐标超出图片范围原始标注可能有错误坐标需要在归一化前进行裁剪df[x] np.where(df[x] df[width], df[width], df[x]) df[x] np.where(df[x] 0, 0, df[x])图片与标注不匹配检查图片ID是否一致特别是补零处理# 统一用8位数字表示图片ID df[Image_ID_AL] df[Image_ID].map(lambda x: str(x).zfill(8))多光谱图像对丢失确保可见光和红外图像同步处理# _co和_ir图像要一起处理 img_file_list [filename.replace(.txt,_co.png) for filename in annot_file_list] ir_file_list [filename.replace(.txt,_ir.png) for filename in annot_file_list]类别不平衡使用分层抽样(stratify)保持各类别比例train_test_split(X, y, test_size0.2, stratifyy)图片尺寸不一致统一缩放到相同尺寸RESIZE_RES 640 image.thumbnail((RESIZE_RES, RESIZE_RES))处理完这些问题后你将得到一个标准的YOLO格式多光谱数据集可以直接用于YOLOv5、YOLOv8等模型的训练。在实际项目中我通常会额外保存一份类别映射关系的说明文件方便后续模型推理时使用。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2432373.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!