diff options
| author | Vineet Kumar <git@vineetk.net> | 2026-04-18 21:34:11 -0400 |
|---|---|---|
| committer | Vineet Kumar <git@vineetk.net> | 2026-04-18 21:34:11 -0400 |
| commit | 8f571ee51854540d186aafc01d25908bba5ea09e (patch) | |
| tree | 951e0d2e9d3eb297c654fd7a0ceb5f999482af89 /utils.py | |
initial commit
Diffstat (limited to 'utils.py')
| -rw-r--r-- | utils.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..a836ca2 --- /dev/null +++ b/utils.py | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | import random | ||
| 2 | import numpy as np | ||
| 3 | import torch | ||
| 4 | |||
| 5 | CLASS_NAMES = [ | ||
| 6 | 'Healthy (0)', | ||
| 7 | 'Mild NPDR (1)', | ||
| 8 | 'Moderate NPDR (2)', | ||
| 9 | 'Severe NPDR (3)', | ||
| 10 | 'Proliferative DR (4)', | ||
| 11 | ] | ||
| 12 | |||
| 13 | NUM_CLASSES = 5 | ||
| 14 | |||
| 15 | |||
| 16 | def set_seed(seed: int = 42) -> None: | ||
| 17 | random.seed(seed) | ||
| 18 | np.random.seed(seed) | ||
| 19 | torch.manual_seed(seed) | ||
| 20 | torch.cuda.manual_seed_all(seed) | ||
| 21 | torch.backends.cudnn.deterministic = True | ||
| 22 | torch.backends.cudnn.benchmark = False | ||
| 23 | |||
| 24 | |||
| 25 | def compute_class_weights(labels) -> torch.FloatTensor: | ||
| 26 | """Inverse-frequency weighting computed from training labels.""" | ||
| 27 | labels = np.array(labels) | ||
| 28 | class_counts = np.bincount(labels, minlength=NUM_CLASSES) | ||
| 29 | total = len(labels) | ||
| 30 | weights = total / (NUM_CLASSES * class_counts.astype(float)) | ||
| 31 | return torch.FloatTensor(weights) | ||
| 32 | |||
| 33 | |||
| 34 | def get_device() -> torch.device: | ||
| 35 | if torch.cuda.is_available(): | ||
| 36 | return torch.device('cuda') | ||
| 37 | return torch.device('cpu') | ||
