File size: 1,560 Bytes
1f2e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Utility functions for data processing."""

import os
import random
import numpy as np
import torch


def set_seed(seed: int = 42):
    """
    Set random seeds for reproducibility.
    
    Args:
        seed: Random seed value
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    
    # Additional settings for determinism (may impact performance)
    torch.backends.cudnn.deterministic = False  # Keep False for speed
    torch.backends.cudnn.benchmark = True  # Enable for fixed input size


def get_class_to_idx(class_names: list) -> dict:
    """Create class name to index mapping."""
    return {cls_name: idx for idx, cls_name in enumerate(sorted(class_names))}


def count_images_in_dir(directory: str) -> dict:
    """
    Count images per class in a directory.
    
    Args:
        directory: Path to directory with class subdirectories
        
    Returns:
        Dictionary mapping class names to counts
    """
    counts = {}
    
    if not os.path.exists(directory):
        return counts
    
    for class_name in os.listdir(directory):
        class_dir = os.path.join(directory, class_name)
        if os.path.isdir(class_dir):
            image_files = [f for f in os.listdir(class_dir) 
                          if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
            counts[class_name] = len(image_files)
    
    return counts


def ensure_dir(directory: str):
    """Create directory if it doesn't exist."""
    os.makedirs(directory, exist_ok=True)