[目标检测] 训练之前要做什么

ops/2025/3/16 13:35:04/

背景:训练一个Yolo8模型,在训练之前,数据集的处理是影响效果的关键因素。

Step1 定义规则

什么是人/车,比如人的话可能是站着的人,如果是骑电动车/自行车就不算是人。

Step2 收集数据集

1. 自己标注。如果是自己标注,那么根据上述的规则进行清洗。

2. 采集他人的数据集,标注好的。那么最好是能可视化一下标签。下面是可视化代码。

import os
import cv2def visualize_yolo_boxes(image_folder, label_folder, output_folder):# 确保输出文件夹存在if not os.path.exists(output_folder):os.makedirs(output_folder)# 遍历图片文件夹for image_name in os.listdir(image_folder):# 获取图片路径image_path = os.path.join(image_folder, image_name)# 获取对应的标签路径label_path = os.path.join(label_folder, os.path.splitext(image_name)[0] + '.txt')# 如果标签文件不存在,跳过if not os.path.exists(label_path):print(f"标签文件不存在: {label_path}")continue# 读取图片image = cv2.imread(image_path)if image is None:print(f"无法读取图片: {image_path}")continue# 获取图片的宽度和高度img_height, img_width = image.shape[:2]# 读取标签文件with open(label_path, 'r') as f:lines = f.readlines()# 遍历每个标签for line in lines:parts = line.strip().split()if len(parts) != 5:continue# 解析YOLO格式的标签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 = int(x_center - width / 2)y1 = int(y_center - height / 2)x2 = int(x_center + width / 2)y2 = int(y_center + height / 2)# 在图片上绘制边界框cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)cv2.putText(image, f'Class {class_id}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)# 保存可视化结果output_path = os.path.join(output_folder, image_name)cv2.imwrite(output_path, image)print(f"已保存可视化结果: {output_path}")# 示例用法
image_folder = r'.\train\images'
label_folder = r'.\train\labels'
output_folder = r'.\train\visualize'visualize_yolo_boxes(image_folder, label_folder, output_folder)

Step3 修改标签

修改标签是指,比如要合并一些标签。已经标注好的数据集,比如把车子分为了truck,car,bus之类的,但是,我们都统称为car。所以要进行一些合并。下面是合并代码。

import os
import globdef process_line(line):"""Process a single line of text according to the mapping rules."""parts = line.strip().split()if not parts:  # Skip empty linesreturn Nonetry:id_num = int(parts[0])# Keep only specified IDsif id_num not in [0, 1, 2, 6]:return None# Apply mapping: 0,1 -> 0 and 3,4,5,8 -> 1if id_num in [0, 1, 6]:new_id = 1else:  # id in [3,4,5,8]new_id = 0# Replace first number and keep rest of the line the samereturn f"{new_id} {' '.join(parts[1:])}\n"except ValueError:return Nonedef process_file(input_path, output_path):"""Process a single text file and save to output directory."""try:with open(input_path, 'r') as infile:lines = infile.readlines()# Process lines and filter out None resultsprocessed_lines = [processed for line in linesif (processed := process_line(line)) is not None]# Create output directory if it doesn't existos.makedirs(os.path.dirname(output_path), exist_ok=True)# Write processed lines to output filewith open(output_path, 'w') as outfile:outfile.writelines(processed_lines)print(f"Processed {input_path} -> {output_path}")except Exception as e:print(f"Error processing {input_path}: {str(e)}")def process_directory(input_dir, output_dir):"""Process all .txt files in the input directory."""# Create output directory if it doesn't existos.makedirs(output_dir, exist_ok=True)# Find all .txt files in input directorytxt_files = glob.glob(os.path.join(input_dir, "*.txt"))for input_path in txt_files:# Create corresponding output pathrelative_path = os.path.relpath(input_path, input_dir)output_path = os.path.join(output_dir, relative_path)# Process the fileprocess_file(input_path, output_path)# Example usage
if __name__ == "__main__":input_directory = r"F:\1-dataset\raw\add_vehicle_person\combine\vehicle_class\valid\labels"  # Replace with your input directoryoutput_directory = r"F:\1-dataset\raw\add_vehicle_person\combine\vehicle_class\valid\labels_person_car"  # Replace with your output directoryprocess_directory(input_directory, output_directory)

 


http://www.ppmy.cn/ops/166214.html

相关文章

每日一题——63. 不同路径 II

题目链接&#xff1a;63. 不同路径 II - 力扣&#xff08;LeetCode&#xff09; 代码&#xff1a; class Solution { public:int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int m obstacleGrid.size();int n obstacleGrid[0].size();…

探索Trae:Cursor的完美替代,Claude-3.5-Sonnet与GPT-4o免费体验

2025年1月 —— 字节跳动&#xff08;TikTok 的母公司&#xff09;推出了 Trae&#xff0c;这款创新的 AI 驱动代码编辑器&#xff0c;旨在大幅提升开发者的工作效率。Trae 将强大的人工智能与简洁直观的界面结合&#xff0c;帮助开发者更快速、轻松地编写、调试和优化代码。 …

【新品解读】直采+异构,看 RFSoC FPGA 开发板 AXW49 如何应对射频信号处理高要求

在追求更快、更稳的无线通信路上&#xff0c;传统射频架构深陷带宽-功耗-成本的“不可能三角”&#xff1a;带宽每翻倍&#xff0c;系统复杂度与功耗增幅远超线性增长。传统方案通过“分立式功放多级变频链路JESD204B 接口”的组合试图平衡性能与成本&#xff0c;却难以满足实时…

Redis7——进阶篇(六)

前言&#xff1a;此篇文章系本人学习过程中记录下来的笔记&#xff0c;里面难免会有不少欠缺的地方&#xff0c;诚心期待大家多多给予指教。 基础篇&#xff1a; Redis&#xff08;一&#xff09;Redis&#xff08;二&#xff09;Redis&#xff08;三&#xff09;Redis&#x…

AcWing--871.约数之和

目录 题目&#xff1a; 分析&#xff1a; 代码&#xff1a; 题目&#xff1a; 给定 n 个正整数 ai&#xff0c;请你输出这些数的乘积的约数之和&#xff0c;答案对 10^97 取模。 输入格式 第一行包含整数 n。 接下来 n 行&#xff0c;每行包含一个整数 ai。 输出格式 输出一…

【QT:信号和槽】

QT信号涉及的三要素&#xff1a;信号源、信号类型、信号的处理方式。 QT的信号槽机制&#xff1a; 给按钮的点击操作关联一个处理函数&#xff0c;用户点击按钮时触发&#xff0c;对应的处理函数就会执行 QT中使用connect函数将信号和槽关联起来&#xff0c;信号触发&#xf…

深度解析前端面试八股文:核心知识点与高效应对策略

深度解析前端面试八股文&#xff1a;核心知识点与高效应对策略 1. 引言 前端面试是每位开发者迈向职业进阶的重要环节&#xff0c;涉及 HTML、CSS、JavaScript、性能优化、浏览器原理、网络、安全、框架&#xff08;Vue/React&#xff09; 等核心知识点。本文不仅会覆盖 前端…

责任链模式的C++实现示例

核心思想 责任链模式是一种行为设计模式&#xff0c;允许多个对象都有机会处理请求&#xff0c;从而避免请求的发送者与接收者之间的耦合。请求沿着处理链传递&#xff0c;直到某个对象处理它为止。 解决的问题 ​解耦请求发送者与处理者&#xff1a;请求的发送者无需知道具…