Spaces:
Paused
Paused
Download src/utils.py from ahmedsharaf/ClothSeg: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/ahmedsharaf/ClothSeg/resolve/main/src/utils.py
- Command line
-
hf download hf://spaces/ahmedsharaf/ClothSeg/src/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/ahmedsharaf/ClothSeg/resolve/main/src/utils.py
1.39 kB
| import random | |
| import numpy as np | |
| import torch | |
| def seed_everything(seed=42): | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| torch.manual_seed(seed) | |
| torch.cuda.manual_seed(seed) | |
| torch.cuda.manual_seed_all(seed) | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| def show_gpu_memory(): | |
| if torch.cuda.is_available(): | |
| for i in range(torch.cuda.device_count()): | |
| allocated = torch.cuda.memory_allocated(i) / 1024**3 | |
| reserved = torch.cuda.memory_reserved(i) / 1024**3 | |
| print( | |
| f"GPU {i}: " | |
| f"allocated: {allocated:.2f} GB " | |
| f"reserved: {reserved:.2f} GB" | |
| ) | |
| else: | |
| print( | |
| f"No GPU detected" | |
| ) | |
| def count_parameters(model): | |
| total_params = sum(p.numel() for p in model.parameters()) | |
| trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad) | |
| non_trainable_params = total_params - trainable_params | |
| return total_params, trainable_params, non_trainable_params | |
| def denormalize_image(image): | |
| mean = np.array([0.485, 0.456, 0.406]) | |
| std = np.array([0.229, 0.224, 0.225]) | |
| image = image * std + mean | |
| return np.clip(image, 0, 1) | |
| # Test | |
| if __name__ == "__main__": | |
| seed_everything() | |
| show_gpu_memory() | |