|
|
import os |
|
|
import shutil |
|
|
import random |
|
|
from PIL import Image |
|
|
|
|
|
import pandas as pd |
|
|
import torch |
|
|
import torchvision.transforms as T |
|
|
from torchvision.transforms.functional import pad |
|
|
from tqdm.auto import tqdm |
|
|
|
|
|
|
|
|
INPUT_DIR = "./GTSRB" |
|
|
OUTPUT_DIR = "./GTSRB_224_balanced" |
|
|
IMAGE_SIZE = 224 |
|
|
TARGET_TRAIN_SAMPLES_PER_CLASS = 1000 |
|
|
RANDOM_STATE = 42 |
|
|
|
|
|
|
|
|
torch.manual_seed(RANDOM_STATE) |
|
|
random.seed(RANDOM_STATE) |
|
|
|
|
|
|
|
|
|
|
|
class PadToSquare: |
|
|
def __init__(self, fill_value=0): |
|
|
self.fill_value = fill_value |
|
|
|
|
|
def __call__(self, img): |
|
|
w, h = img.size |
|
|
if w == h: |
|
|
return img |
|
|
max_dim = max(w, h) |
|
|
pad_left = (max_dim - w) // 2 |
|
|
pad_right = max_dim - w - pad_left |
|
|
pad_top = (max_dim - h) // 2 |
|
|
pad_bottom = max_dim - h - pad_top |
|
|
padding = (pad_left, pad_top, pad_right, pad_bottom) |
|
|
|
|
|
return pad(img, padding, padding_mode="reflect") |
|
|
|
|
|
|
|
|
|
|
|
base_transform = T.Compose( |
|
|
[ |
|
|
PadToSquare(fill_value=0), |
|
|
T.Resize( |
|
|
(IMAGE_SIZE, IMAGE_SIZE), |
|
|
interpolation=T.InterpolationMode.BICUBIC, |
|
|
antialias=True, |
|
|
), |
|
|
] |
|
|
) |
|
|
|
|
|
|
|
|
augmentation_pipeline = T.Compose( |
|
|
[ |
|
|
base_transform, |
|
|
|
|
|
T.RandomAffine( |
|
|
degrees=15, translate=(0.1, 0.1), scale=(0.9, 1.1), shear=5, fill=128 |
|
|
), |
|
|
T.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2), |
|
|
] |
|
|
) |
|
|
|
|
|
|
|
|
def process_training_data(): |
|
|
"""处理训练数据集,包含降采样和数据增强逻辑。""" |
|
|
print("开始处理训练数据集...") |
|
|
|
|
|
train_input_dir = os.path.join(INPUT_DIR, "Final_Training", "Images") |
|
|
train_output_dir = os.path.join(OUTPUT_DIR, "train") |
|
|
|
|
|
class_dirs = [ |
|
|
d |
|
|
for d in os.listdir(train_input_dir) |
|
|
if os.path.isdir(os.path.join(train_input_dir, d)) |
|
|
] |
|
|
|
|
|
for class_id_str in tqdm(class_dirs, desc="处理类别"): |
|
|
class_input_dir = os.path.join(train_input_dir, class_id_str) |
|
|
class_id_int = int(class_id_str) |
|
|
formatted_class_id = f"{class_id_int:02d}" |
|
|
class_output_dir = os.path.join(train_output_dir, formatted_class_id) |
|
|
os.makedirs(class_output_dir, exist_ok=True) |
|
|
|
|
|
annotation_file = os.path.join(class_input_dir, f"GT-{class_id_str}.csv") |
|
|
df = pd.read_csv(annotation_file, sep=";") |
|
|
|
|
|
|
|
|
num_original = len(df) |
|
|
|
|
|
if num_original > TARGET_TRAIN_SAMPLES_PER_CLASS: |
|
|
|
|
|
df_to_process = df.sample( |
|
|
n=TARGET_TRAIN_SAMPLES_PER_CLASS, random_state=RANDOM_STATE |
|
|
) |
|
|
else: |
|
|
|
|
|
df_to_process = df |
|
|
|
|
|
|
|
|
images_for_augmentation_pool = [] |
|
|
for _, row in df_to_process.iterrows(): |
|
|
filename = row["Filename"] |
|
|
img_path = os.path.join(class_input_dir, filename) |
|
|
try: |
|
|
with Image.open(img_path) as img: |
|
|
|
|
|
images_for_augmentation_pool.append(img.copy()) |
|
|
|
|
|
processed_img = base_transform(img) |
|
|
output_filename = os.path.splitext(filename)[0] + ".jpeg" |
|
|
processed_img.save( |
|
|
os.path.join(class_output_dir, output_filename), "JPEG" |
|
|
) |
|
|
except FileNotFoundError: |
|
|
print(f"警告:文件未找到 {img_path}") |
|
|
except Exception as e: |
|
|
print(f"处理文件 {img_path} 时出错: {e}") |
|
|
|
|
|
|
|
|
num_processed = len(df_to_process) |
|
|
num_to_augment = TARGET_TRAIN_SAMPLES_PER_CLASS - num_processed |
|
|
|
|
|
if num_to_augment > 0: |
|
|
for i in range(num_to_augment): |
|
|
|
|
|
img_to_augment = random.choice(images_for_augmentation_pool) |
|
|
augmented_img = augmentation_pipeline(img_to_augment) |
|
|
output_filename = f"aug_{i:04d}.jpeg" |
|
|
augmented_img.save( |
|
|
os.path.join(class_output_dir, output_filename), "JPEG" |
|
|
) |
|
|
|
|
|
print("训练数据集处理完成。") |
|
|
|
|
|
|
|
|
def process_test_data(): |
|
|
"""处理测试数据集""" |
|
|
print("\n开始处理测试数据集...") |
|
|
test_input_dir = os.path.join(INPUT_DIR, "Final_Test", "Images") |
|
|
test_output_dir = os.path.join(OUTPUT_DIR, "test") |
|
|
annotation_file = os.path.join(INPUT_DIR, "GT-final_test.csv") |
|
|
df = pd.read_csv(annotation_file, sep=";") |
|
|
|
|
|
for _, row in tqdm(df.iterrows(), total=len(df), desc="处理测试图片"): |
|
|
filename = row["Filename"] |
|
|
class_id = row["ClassId"] |
|
|
img_path = os.path.join(test_input_dir, filename) |
|
|
formatted_class_id = f"{class_id:02d}" |
|
|
class_output_dir = os.path.join(test_output_dir, formatted_class_id) |
|
|
os.makedirs(class_output_dir, exist_ok=True) |
|
|
try: |
|
|
with Image.open(img_path) as img: |
|
|
processed_img = base_transform(img) |
|
|
output_filename = os.path.splitext(filename)[0] + ".jpeg" |
|
|
processed_img.save( |
|
|
os.path.join(class_output_dir, output_filename), "JPEG" |
|
|
) |
|
|
except FileNotFoundError: |
|
|
print(f"警告:文件未找到 {img_path}") |
|
|
except Exception as e: |
|
|
print(f"处理文件 {img_path} 时出错: {e}") |
|
|
|
|
|
print("测试数据集处理完成。") |
|
|
|
|
|
|
|
|
def main(): |
|
|
print(f"GTSRB 数据集转换脚本") |
|
|
print(f"输入目录: {INPUT_DIR}") |
|
|
print(f"输出目录: {OUTPUT_DIR}\n") |
|
|
|
|
|
if os.path.exists(OUTPUT_DIR): |
|
|
print(f"发现已存在的输出目录 {OUTPUT_DIR},正在删除...") |
|
|
shutil.rmtree(OUTPUT_DIR) |
|
|
|
|
|
print("正在创建新的输出目录结构...") |
|
|
os.makedirs(os.path.join(OUTPUT_DIR, "train"), exist_ok=True) |
|
|
os.makedirs(os.path.join(OUTPUT_DIR, "test"), exist_ok=True) |
|
|
|
|
|
process_training_data() |
|
|
process_test_data() |
|
|
|
|
|
print(f"\n🎉 全部处理完成!转换后的数据位于: {OUTPUT_DIR}") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|