-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
197 lines (159 loc) · 5.43 KB
/
utils.py
File metadata and controls
197 lines (159 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Utility functions for the Knowledge Distillation experiment
"""
import os
import random
import numpy as np
import torch
import torch.nn as nn
import config
def set_seed(seed: int = 42):
"""Set random seed for reproducibility."""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def create_dirs():
"""Create necessary directories."""
os.makedirs(config.DATA_DIR, exist_ok=True)
os.makedirs(config.CHECKPOINT_DIR, exist_ok=True)
os.makedirs(config.RESULTS_DIR, exist_ok=True)
os.makedirs(os.path.join(config.CHECKPOINT_DIR, 'baseline_b0'), exist_ok=True)
os.makedirs(os.path.join(config.CHECKPOINT_DIR, 'distilled_b0'), exist_ok=True)
class AverageMeter:
"""Computes and stores the average and current value."""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output: torch.Tensor, target: torch.Tensor, topk=(1,)):
"""
Computes the accuracy over the k top predictions.
Args:
output: Model predictions (logits)
target: Ground truth labels
topk: Tuple of k values for top-k accuracy
Returns:
List of accuracies for each k
"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def save_checkpoint(
model: nn.Module,
optimizer: torch.optim.Optimizer,
epoch: int,
val_acc: float,
save_dir: str,
is_best: bool = False
):
"""
Save model checkpoint.
Args:
model: Model to save
optimizer: Optimizer state
epoch: Current epoch
val_acc: Validation accuracy
save_dir: Directory to save checkpoint
is_best: Whether this is the best model so far
"""
os.makedirs(save_dir, exist_ok=True)
checkpoint = {
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'val_acc': val_acc,
}
# Save latest checkpoint
latest_path = os.path.join(save_dir, 'latest_model.pth')
torch.save(checkpoint, latest_path)
# Save best checkpoint
if is_best:
best_path = os.path.join(save_dir, 'best_model.pth')
torch.save(checkpoint, best_path)
def load_checkpoint(model: nn.Module, checkpoint_path: str, device: str = 'cpu'):
"""
Load model checkpoint.
Args:
model: Model to load weights into
checkpoint_path: Path to checkpoint file
device: Device to load model to
Returns:
model, epoch, val_acc
"""
checkpoint = torch.load(checkpoint_path, map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
epoch = checkpoint.get('epoch', 0)
val_acc = checkpoint.get('val_acc', 0.0)
return model, epoch, val_acc
class EarlyStopping:
"""Early stopping to stop training when validation loss doesn't improve."""
def __init__(self, patience: int = 10, min_delta: float = 0.0):
"""
Args:
patience: Number of epochs to wait before stopping
min_delta: Minimum change to qualify as an improvement
"""
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss: float) -> bool:
"""
Check if training should stop.
Args:
val_loss: Current validation loss
Returns:
True if training should stop, False otherwise
"""
if self.best_loss is None:
self.best_loss = val_loss
elif val_loss > self.best_loss - self.min_delta:
self.counter += 1
if self.counter >= self.patience:
return True
else:
self.best_loss = val_loss
self.counter = 0
return False
def get_gpu_memory_info():
"""Get GPU memory usage information."""
if torch.cuda.is_available():
gpu_memory_allocated = torch.cuda.memory_allocated() / 1024**3
gpu_memory_reserved = torch.cuda.memory_reserved() / 1024**3
gpu_memory_total = torch.cuda.get_device_properties(0).total_memory / 1024**3
return {
'allocated_gb': gpu_memory_allocated,
'reserved_gb': gpu_memory_reserved,
'total_gb': gpu_memory_total,
'free_gb': gpu_memory_total - gpu_memory_reserved
}
return None
def print_gpu_memory():
"""Print current GPU memory usage."""
info = get_gpu_memory_info()
if info:
print(f"GPU Memory: {info['allocated_gb']:.2f}GB allocated, "
f"{info['reserved_gb']:.2f}GB reserved, "
f"{info['total_gb']:.2f}GB total")
else:
print("CUDA not available")