import os import random # Change the static percentage parameter to scale down def remove_random_images(base_path, percentage=5): """Removes 5% percentage of images randomly from each class subdirectory.""" subsets = ['test', 'train', 'val'] for subset in subsets: subset_path = os.path.join(base_path, subset) if not os.path.exists(subset_path): continue for class_name in os.listdir(subset_path): class_path = os.path.join(subset_path, class_name) if os.path.isdir(class_path): images = [f for f in os.listdir(class_path) if f.endswith('.jpg')] num_to_remove = int(len(images) * (percentage / 100)) if num_to_remove > 0: images_to_remove = random.sample(images, num_to_remove) for img in images_to_remove: os.remove(os.path.join(class_path, img)) print(f"Removed {num_to_remove} images from {subset}/{class_name}") # Define base path base_directory = "classify" # Run the function remove_random_images(base_directory)