File size: 1,199 Bytes
41f0921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)