Download backend/src/data/utils.py from StillWorking/Brain-Tumor-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/datasets/StillWorking/Brain-Tumor-AI/resolve/main/backend/src/data/utils.py
- Command line
-
hf download hf://datasets/StillWorking/Brain-Tumor-AI/backend/src/data/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/StillWorking/Brain-Tumor-AI/resolve/main/backend/src/data/utils.py
1.56 kB
| """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) | |