数据集label的统计分布

在训练模型前,了解数据集标注分布至关重要。本文关注于检查是否存在缺失标注的图像,以及类别是否平衡。通过统计【num_anno】、【num_classes】和【num_label】,以XML标注文件为例,揭示了如何获取类别标签分布,并通过可视化手段直观展示标注数量,以优化数据预处理,确保模型训练效果。
摘要由CSDN通过智能技术生成

在训练模型前,需要知道数据集与标注的分布情况。看看有没有标注缺失的图像,做标注补全;类别数量不平衡的话,也要做相应的数据预处理,否则模型训练的效果不好。因此需要统计数据集的标注数量【num_anno】,类别数量【num_classes】,及各个类别标注的分布【num_label】。以xml标注文件为例,获取各类别的标签数据分布。

import  xml.dom.minidom
import os,sys
import matplotlib.pyplot as plt  
 
rootdir = '../mmdetection/data/abn/VOCdevkit/VOC2012/Annotations/'
doc_xml = os.listdir(rootdir) 
print('num_anno', len(doc_xml))
classes_list = []
num_label = {}
for i in range(0,len(doc_xml)):
    path = os.path.join(rootdir,doc_xml[i])
    if os.path.isfile(path):
        #打开xml文档
        dom = xml.dom.minidom.parse(path)
        #得到dom元素的label
        root = dom.documentElement
        label=dom.getElementsByTagName('name')
        for i in range(len(label)):
            c1 = label[i]
            class_name = c1.firstChild.data
            #列表中不存在则存入列表
            if 
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个基于YOLOv5的数据集分析代码示例: ```python import os import matplotlib.pyplot as plt import json def analyze_dataset(data_dir): annotations_dir = os.path.join(data_dir, "annotations") images_dir = os.path.join(data_dir, "images") # 统计每个类别的物体数量 class_counts = {} # 统计每张图像中物体的平均数量 object_counts = [] # 统计图像的宽度和高度分布 image_widths = [] image_heights = [] # 加载标注文件 annotation_files = os.listdir(annotations_dir) for anno_file in annotation_files: with open(os.path.join(annotations_dir, anno_file), 'r') as f: annotation_data = json.load(f) # 统计物体数量 objects = annotation_data['objects'] object_counts.append(len(objects)) # 统计每个类别的物体数量 for obj in objects: obj_label = obj['label'] if obj_label not in class_counts: class_counts[obj_label] = 1 else: class_counts[obj_label] += 1 # 加载图像文件并统计宽度和高度 image_file = os.path.join(images_dir, anno_file.replace(".json", ".jpg")) image = plt.imread(image_file) image_heights.append(image.shape[0]) image_widths.append(image.shape[1]) # 绘制物体数量直方图 plt.figure(figsize=(10, 5)) plt.bar(class_counts.keys(), class_counts.values()) plt.xlabel('Class') plt.ylabel('Count') plt.title('Object Count by Class') plt.xticks(rotation=45) plt.show() # 绘制物体平均数量直方图 plt.figure(figsize=(10, 5)) plt.hist(object_counts, bins=range(0, max(object_counts)+1)) plt.xlabel('Object Count') plt.ylabel('Image Count') plt.title('Object Count per Image') plt.show() # 绘制图像宽度和高度分布图 plt.figure(figsize=(10, 5)) plt.hist(image_widths, bins='auto', alpha=0.5, color='r', label='Width') plt.hist(image_heights, bins='auto', alpha=0.5, color='b', label='Height') plt.xlabel('Size') plt.ylabel('Count') plt.title('Image Size Distribution') plt.legend(loc='upper right') plt.show() # 使用示例 data_dir = '/path/to/dataset' analyze_dataset(data_dir) ``` 这段代码会分析YOLOv5数据集的标注文件和图像文件,统计每个类别的物体数量、每张图像中物体的平均数量以及图像的宽度和高度分布。最后,会绘制出物体数量直方图、物体平均数量直方图和图像宽度和高度分布图。你需要将代码中的`/path/to/dataset`替换为你实际的数据集路径。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值