File size: 9,492 Bytes
747451d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | # /*---------------------------------------------------------------------------------------------
# * Copyright (c) 2025 STMicroelectronics.
# * All rights reserved.
# * This software is licensed under terms that can be found in the LICENSE file in
# * the root directory of this software component.
# * If no LICENSE file comes with this software, it is provided AS-IS.
# *--------------------------------------------------------------------------------------------*/
import os
import sys
from pathlib import Path
from glob import glob
from tqdm import tqdm
from collections import Counter
from statistics import mean
import matplotlib.pyplot as plt
def parse_label_file(txt_file_path : str=None) -> list:
"""
Provides detections in a list from input text file
Args:
txt_file_path (str) : Path of the detection file to analyze
Returns:
List : list of detected labels
"""
labels = []
if os.path.isfile(txt_file_path):
with open(txt_file_path, "r") as f:
data = f.readlines()
for line in data:
if line.rstrip() != "":
fields = line.split()
labels.append([float(x) for x in fields])
return labels
def compute_labels_stats(dataset_path : str=None,
dataset_name : str=None,
histogram_dir: str=None) -> None:
"""
Provides statistics on the dataset labels
Args:
dataset_path (str) : Path of the dataset to analyze
dataset_name (str) : Name of the dataset used
histogram_dir (str): location of the histograms storage
Returns:
None
"""
print("\nCalculating groundtruth labels statistics:")
print("-----------------------------------------")
print("Dataset root:", dataset_path)
jpg_file_paths = glob.glob(os.path.join(dataset_path, "*.jpg"))
if len(jpg_file_paths) == 0:
raise ValueError(f"Could not find any .jpg file in dataset root directory")
num_jpg_files = len(jpg_file_paths)
num_txt_files = 0
num_empty_txt = 0
label_sizes = []
for jpg_path in tqdm(jpg_file_paths):
txt_path = os.path.join(Path(jpg_path).parent, Path(jpg_path).stem + ".txt")
if os.path.isfile(txt_path):
num_txt_files += 1
labels = parse_label_file(txt_path)
if not labels:
num_empty_txt += 1
label_sizes.append(0)
else:
label_sizes.append(len(labels))
# label_sizes.append(len(labels))
print("Image files: ", num_jpg_files)
print("Labels files:", num_txt_files)
print("Empty labels files:", num_empty_txt)
print("Labels per image: min = {}, max = {}, mean = {:.2f}".
format(min(label_sizes), max(label_sizes), mean(label_sizes)))
plt.figure(figsize=(8, 8))
plt.hist(label_sizes, bins=max(label_sizes))
plot_title = "Number of labels per image"
if dataset_name:
plot_title += " in dataset " + dataset_name
plt.title(plot_title)
if histogram_dir:
if not os.path.isdir(histogram_dir):
os.makedirs(histogram_dir, exist_ok=True)
plt.savefig(os.path.join(histogram_dir, "labels_stats_" + dataset_name + ".png"))
plt.show()
plt.close()
def compute_class_stats(dataset_path : str=None,
dataset_name : str=None,
histogram_dir: str=None) -> None:
"""
Provides statistics on the dataset classes
Args:
dataset_path (str) : Path of the dataset to analyze
dataset_name (str) : Name of the dataset used
histogram_dir (str): location of the histograms storage
Returns:
None
"""
print("\nCalculating groundtruth class statistics:")
print("----------------------------------------")
print("Dataset root:", dataset_path)
jpg_file_paths = glob.glob(os.path.join(dataset_path, "*.jpg"))
if len(jpg_file_paths) == 0:
raise ValueError(f"Could not find any .jpg file in dataset root directory")
classes = []
for jpg_path in tqdm(jpg_file_paths):
txt_path = os.path.join(Path(jpg_path).parent, Path(jpg_path).stem + ".txt")
if not os.path.isfile(txt_path):
continue
labels = parse_label_file(txt_path)
# Skip .txt files with no objects
if len(labels) == 0:
continue
for i in range(len(labels)):
id = int(labels[i][0])
classes.append(id)
classes_dict = Counter(classes)
class_ids = list(classes_dict.keys())
class_ids.sort()
num_classes = max(class_ids) + 1
print("Number of classes:", num_classes)
print("Occurences:")
class_occurences = []
for id in range(num_classes):
n = classes_dict[id] if id in classes_dict else 0
class_occurences.append(n)
print(f"Class {id}: {n}")
plt.figure(figsize=(8, 8))
plot_title = "Class occurences"
if dataset_name:
plot_title += " in dataset " + dataset_name
plt.title(plot_title)
plt.xticks(class_ids)
plt.bar(class_ids, class_occurences, width=0.4)
if histogram_dir:
if not os.path.isdir(histogram_dir):
os.makedirs(histogram_dir, exist_ok=True)
print(histogram_dir, dataset_name)
plt.savefig(os.path.join(histogram_dir, "classes_stats_" + dataset_name + ".png"))
plt.show()
plt.close()
def num_labels_above_cutoff(dataset_path : str=None,
padded_labels_size : int=15) -> float:
"""
Calculates the percentage of filtered images corresponding to the maximum number of detections
kept per image
Args:
dataset_path (str): Path of the dataset to analyze
padded_labels_size (int) : The max number of detection allowed per image
Returns:
float : The corresponding percentage of filtered detections
"""
print("\nCalculating number of truncated groundtruth labels:")
print("--------------------------------------------------")
print("Dataset root:", dataset_path)
if (padded_labels_size <= 0):
print("Please make sure that you provided maximum number of detections bigger than 0")
print("Exiting the script...")
sys.exit()
jpg_file_paths = glob.glob(os.path.join(dataset_path, "*.jpg"))
if len(jpg_file_paths) == 0:
raise ValueError(f"Could not find any .jpg file under dataset root {dataset_path}")
num_examples = 0
above_cutoff = 0
txt_file_paths = glob.glob(os.path.join(dataset_path, "*.txt"))
for path in txt_file_paths:
num_examples += 1
labels = parse_label_file(path)
if len(labels) > padded_labels_size:
above_cutoff += 1
cutoff_percentage = 100 * above_cutoff/num_examples
print("Padded labels size:", padded_labels_size)
print("Examples with a number of labels greater than padding size: {}/{} ({:.2f}%)".
format(above_cutoff, num_examples, cutoff_percentage))
return (cutoff_percentage)
def num_labels_above_percentage(dataset_path : str=None,
target_percentage : float=0.0) -> int:
"""
Calculates the maximum number of detections in the input images corresponding to the max percentage
of the dataset to be filtered by removing images with a lot a detections
Args:
dataset_path (str) : Path of the dataset to analyze
target_percentage (float) : The max percentage of the dataset to be filtered by
removing images with a lot a detections
Returns:
int : The corresponding maximum number of detections per image filtered.
"""
print("\nCalculating number of truncated groundtruth labels:")
print("--------------------------------------------------")
print("Dataset root:", dataset_path)
if (target_percentage < 0.0) and (target_percentage >= 100.0):
print("Please make sure that you provided maximum percentage of images to filter between [0.0 100[")
print("Exiting the script...")
sys.exit()
jpg_file_paths = glob.glob(os.path.join(dataset_path, "*.jpg"))
if len(jpg_file_paths) == 0:
raise ValueError(f"Could not find any .jpg file under dataset root {dataset_path}")
num_examples = 0
label_sizes = []
txt_file_paths = glob.glob(os.path.join(dataset_path, "*.txt"))
for path in txt_file_paths:
num_examples += 1
labels = parse_label_file(path)
if not labels:
label_sizes.append(0)
else:
label_sizes.append(len(labels))
padded_labels_size = max(label_sizes)
above_cutoff_final = 0
while padded_labels_size > 0:
above_cutoff = 0
for path in txt_file_paths:
labels = parse_label_file(path)
if len(labels) > padded_labels_size:
above_cutoff += 1
current_percentage = 100 * above_cutoff/num_examples
if (current_percentage <= target_percentage):
above_cutoff_final = above_cutoff
padded_labels_size -= 1
else:
above_cutoff = above_cutoff_final
break
print("Padded labels size:", padded_labels_size+1)
print("Examples with a number of labels greater than padding size: {}/{} ({:.2f}%)".
format(above_cutoff, num_examples, 100 * above_cutoff/num_examples))
return (padded_labels_size+1)
|