-
Notifications
You must be signed in to change notification settings - Fork 24
/
protonet_utils.py
319 lines (267 loc) · 11 KB
/
protonet_utils.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import logging
import os
import sys
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
import torch
from pyprojroot import here as project_root
sys.path.insert(0, str(project_root()))
from fs_mol.data import FSMolDataset, FSMolTaskSample
from fs_mol.data.fsmol_dataset import DataFold
from fs_mol.data.protonet import (
ProtoNetBatch,
get_protonet_task_sample_iterable,
get_protonet_batcher,
task_sample_to_pn_task_sample,
)
from fs_mol.models.protonet import PrototypicalNetwork, PrototypicalNetworkConfig
from fs_mol.models.abstract_torch_fsmol_model import MetricType
from fs_mol.utils.metrics import (
BinaryEvalMetrics,
compute_binary_task_metrics,
avg_metrics_over_tasks,
avg_task_metrics_list,
)
from fs_mol.utils.metric_logger import MetricLogger
from fs_mol.utils.test_utils import eval_model, FSMolTaskSampleEvalResults
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class PrototypicalNetworkTrainerConfig(PrototypicalNetworkConfig):
batch_size: int = 256
tasks_per_batch: int = 16
support_set_size: int = 16
query_set_size: int = 256
num_train_steps: int = 10000
validate_every_num_steps: int = 50
validation_support_set_sizes: Tuple[int] = (16, 128)
validation_query_set_size: int = 256
validation_num_samples: int = 5
learning_rate: float = 0.001
clip_value: Optional[float] = None
def run_on_batches(
model: PrototypicalNetwork,
batches: List[ProtoNetBatch],
batch_labels: List[np.ndarray],
train: bool = False,
tasks_per_batch: int = 1,
) -> Tuple[float, BinaryEvalMetrics]:
if train:
model.train()
else:
model.eval()
total_loss, total_num_samples = 0.0, 0
task_preds: List[np.ndarray] = []
task_labels: List[np.ndarray] = []
num_gradient_accumulation_steps = len(batches) * tasks_per_batch
for batch_features, batch_labels in zip(batches, batch_labels):
# Compute task loss
batch_logits = model(batch_features)
batch_loss = model.compute_loss(batch_logits, batch_labels)
# divide this batch loss by the total number of accumulation steps
batch_loss = batch_loss / num_gradient_accumulation_steps
if train:
batch_loss.backward()
total_loss += (
batch_loss.detach() * batch_features.num_query_samples * num_gradient_accumulation_steps
)
total_num_samples += batch_features.num_query_samples
batch_preds = torch.nn.functional.softmax(batch_logits, dim=1).detach().cpu().numpy()
task_preds.append(batch_preds[:, 1])
task_labels.append(batch_labels)
metrics = compute_binary_task_metrics(
predictions=np.concatenate(task_preds, axis=0), labels=np.concatenate(task_labels, axis=0)
)
# we will report loss per sample as before.
return total_loss.cpu().item() / total_num_samples, metrics
def evaluate_protonet_model(
model: PrototypicalNetwork,
dataset: FSMolDataset,
support_sizes: List[int] = [16, 128],
num_samples: int = 5,
seed: int = 0,
batch_size: int = 320,
query_size: Optional[int] = None,
data_fold: DataFold = DataFold.TEST,
save_dir: Optional[str] = None,
) -> Dict[str, List[FSMolTaskSampleEvalResults]]:
batcher = get_protonet_batcher(max_num_graphs=batch_size)
def test_model_fn(
task_sample: FSMolTaskSample, temp_out_folder: str, seed: int
) -> BinaryEvalMetrics:
pn_task_sample = task_sample_to_pn_task_sample(task_sample, batcher)
_, result_metrics = run_on_batches(
model,
batches=pn_task_sample.batches,
batch_labels=pn_task_sample.batch_labels,
train=False,
)
logger.info(
f"{pn_task_sample.task_name}:"
f" {pn_task_sample.num_support_samples:3d} support samples,"
f" {pn_task_sample.num_query_samples:3d} query samples."
f" Avg. prec. {result_metrics.avg_precision:.5f}.",
)
return result_metrics
return eval_model(
test_model_fn=test_model_fn,
dataset=dataset,
train_set_sample_sizes=support_sizes,
out_dir=save_dir,
num_samples=num_samples,
test_size_or_ratio=query_size,
fold=data_fold,
seed=seed,
)
def validate_by_finetuning_on_tasks(
model: PrototypicalNetwork,
dataset: FSMolDataset,
seed: int = 0,
aml_run=None,
metric_to_use: MetricType = "avg_precision",
) -> float:
"""
Validation function for prototypical networks. Similar to test function;
each validation task is used to evaluate the model more than once, the
final results are a mean value for all tasks over the requested metric.
"""
task_results = evaluate_protonet_model(
model,
dataset,
support_sizes=model.config.validation_support_set_sizes,
num_samples=model.config.validation_num_samples,
seed=seed,
batch_size=model.config.batch_size,
query_size=model.config.validation_query_set_size,
data_fold=DataFold.VALIDATION,
aml_run=aml_run,
)
# take the dictionary of task_results and return correct mean over all tasks
mean_metrics = avg_metrics_over_tasks(task_results)
if aml_run is not None:
for metric_name, (metric_mean, _) in mean_metrics.items():
aml_run.log(f"valid_task_test_{metric_name}", float(metric_mean))
return mean_metrics[metric_to_use][0]
class PrototypicalNetworkTrainer(PrototypicalNetwork):
def __init__(self, config: PrototypicalNetworkTrainerConfig):
super().__init__(config)
self.config = config
self.optimizer = torch.optim.Adam(self.parameters(), config.learning_rate)
def get_model_state(self) -> Dict[str, Any]:
return {
"model_config": self.config,
"model_state_dict": self.state_dict(),
}
def save_model(
self,
path: str,
optimizer: Optional[torch.optim.Optimizer] = None,
epoch: Optional[int] = None,
):
data = self.get_model_state()
if optimizer is not None:
data["optimizer_state_dict"] = optimizer.state_dict()
if epoch is not None:
data["epoch"] = epoch
torch.save(data, path)
def load_model_weights(
self,
path: str,
load_task_specific_weights: bool,
quiet: bool = False,
device: Optional[torch.device] = None,
):
pretrained_state_dict = torch.load(path, map_location=device)
for name, param in pretrained_state_dict["model_state_dict"].items():
if isinstance(param, torch.nn.Parameter):
param = param.data
self.state_dict()[name].copy_(param)
optimizer_weights = pretrained_state_dict.get("optimizer_state_dict")
if optimizer_weights is not None:
for name, param in optimizer_weights.items():
self.optimizer.state_dict()[name].copy_(param)
@classmethod
def build_from_model_file(
cls,
model_file: str,
config_overrides: Dict[str, Any] = {},
quiet: bool = False,
device: Optional[torch.device] = None,
) -> "PrototypicalNetworkTrainer":
"""Build the model architecture based on a saved checkpoint."""
checkpoint = torch.load(model_file, map_location=device)
config = checkpoint["model_config"]
if not quiet:
logger.info(f" Loading model configuration from {model_file}.")
model = PrototypicalNetworkTrainer(config)
model.load_model_weights(
path=model_file,
quiet=quiet,
load_task_specific_weights=True,
device=device,
)
return model
def train_loop(self, out_dir: str, dataset: FSMolDataset, aml_run=None):
self.save_model(os.path.join(out_dir, "best_validation.pt"))
train_task_sample_iterator = iter(
get_protonet_task_sample_iterable(
dataset=dataset,
data_fold=DataFold.TRAIN,
num_samples=1,
max_num_graphs=self.config.batch_size,
support_size=self.config.support_set_size,
query_size=self.config.query_set_size,
repeat=True,
)
)
best_validation_avg_prec = 0.0
metric_logger = MetricLogger(
log_fn=lambda msg: logger.info(msg),
aml_run=aml_run,
window_size=max(10, self.config.validate_every_num_steps / 5),
)
for step in range(1, self.config.num_train_steps + 1):
torch.set_grad_enabled(True)
self.optimizer.zero_grad()
task_batch_losses: List[float] = []
task_batch_metrics: List[BinaryEvalMetrics] = []
for _ in range(self.config.tasks_per_batch):
train_task_sample = next(train_task_sample_iterator)
task_loss, task_metrics = run_on_batches(
self,
batches=train_task_sample.batches,
batch_labels=train_task_sample.batch_labels,
train=True,
tasks_per_batch=self.config.tasks_per_batch,
)
task_batch_losses.append(task_loss)
task_batch_metrics.append(task_metrics)
# Now do a training step - run_on_batches will have accumulated gradients
if self.config.clip_value is not None:
torch.nn.utils.clip_grad_norm_(self.parameters(), self.config.clip_value)
self.optimizer.step()
task_batch_mean_loss = np.mean(task_batch_losses)
task_batch_avg_metrics = avg_task_metrics_list(task_batch_metrics)
metric_logger.log_metrics(
loss=task_batch_mean_loss,
avg_prec=task_batch_avg_metrics["avg_precision"][0],
kappa=task_batch_avg_metrics["kappa"][0],
acc=task_batch_avg_metrics["acc"][0],
)
if step % self.config.validate_every_num_steps == 0:
valid_metric = validate_by_finetuning_on_tasks(self, dataset, aml_run=aml_run)
if aml_run:
# printing some measure of loss on all validation tasks.
aml_run.log(f"valid_mean_avg_prec", valid_metric)
logger.info(
f"Validated at train step [{step}/{self.config.num_train_steps}],"
f" Valid Avg. Prec.: {valid_metric:.3f}",
)
# save model if validation avg prec is the best so far
if valid_metric > best_validation_avg_prec:
best_validation_avg_prec = valid_metric
model_path = os.path.join(out_dir, "best_validation.pt")
self.save_model(model_path)
logger.info(f"Updated {model_path} to new best model at train step {step}")
# save the fully trained model
self.save_model(os.path.join(out_dir, "fully_trained.pt"))