Spaces:
Runtime error
Runtime error
Download src/utils/checkpoint.py from mshaiel2004/dr-severity-grading: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/mshaiel2004/dr-severity-grading/resolve/main/src/utils/checkpoint.py
- Command line
-
hf download hf://spaces/mshaiel2004/dr-severity-grading/src/utils/checkpoint.py
-
curl -L -o checkpoint.py https://huggingface.co/spaces/mshaiel2004/dr-severity-grading/resolve/main/src/utils/checkpoint.py
1.33 kB
| import os | |
| from typing import Any, Dict, Optional | |
| import torch | |
| def save_checkpoint( | |
| state: Dict[str, Any], | |
| filepath: str, | |
| is_best: bool = False, | |
| best_filepath: Optional[str] = None, | |
| ) -> None: | |
| """Save model checkpoint and optionally copy/save to best filepath. | |
| Args: | |
| state: Dictionary containing state_dict, optimizer, epoch, metrics, etc. | |
| filepath: Destination path for current checkpoint. | |
| is_best: Whether this checkpoint achieved the best validation score. | |
| best_filepath: Destination path for best checkpoint. | |
| """ | |
| os.makedirs(os.path.dirname(filepath), exist_ok=True) | |
| torch.save(state, filepath) | |
| if is_best and best_filepath: | |
| os.makedirs(os.path.dirname(best_filepath), exist_ok=True) | |
| torch.save(state, best_filepath) | |
| def load_checkpoint( | |
| filepath: str, | |
| device: torch.device = torch.device("cpu"), | |
| ) -> Dict[str, Any]: | |
| """Load model checkpoint safely on target device. | |
| Args: | |
| filepath: Path to the .pth checkpoint file. | |
| device: Torch device to map loaded tensors. | |
| Returns: | |
| Loaded state dictionary. | |
| """ | |
| if not os.path.exists(filepath): | |
| raise FileNotFoundError(f"Checkpoint file not found: {filepath}") | |
| return torch.load(filepath, map_location=device, weights_only=False) | |