summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authorVineet Kumar <git@vineetk.net>2026-04-18 21:34:11 -0400
committerVineet Kumar <git@vineetk.net>2026-04-18 21:34:11 -0400
commit8f571ee51854540d186aafc01d25908bba5ea09e (patch)
tree951e0d2e9d3eb297c654fd7a0ceb5f999482af89 /utils.py
initial commit
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py37
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 @@
1import random
2import numpy as np
3import torch
4
5CLASS_NAMES = [
6 'Healthy (0)',
7 'Mild NPDR (1)',
8 'Moderate NPDR (2)',
9 'Severe NPDR (3)',
10 'Proliferative DR (4)',
11]
12
13NUM_CLASSES = 5
14
15
16def 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
25def 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
34def get_device() -> torch.device:
35 if torch.cuda.is_available():
36 return torch.device('cuda')
37 return torch.device('cpu')