-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_module.py
355 lines (300 loc) · 12.4 KB
/
single_module.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from typing import Any, List
import hydra
import torch
from omegaconf import DictConfig
from torch import nn
from src.modules.components.lit_module import BaseLitModule
from src.modules.losses import load_loss
from src.modules.metrics import load_metrics
class SingleLitModule(BaseLitModule):
"""Example of LightningModule for MNIST classification.
A LightningModule organizes your PyTorch code into 6 sections:
- Computations (init)
- Model loop (model_step)
- Train loop (training_step)
- Validation loop (validation_step)
- Test loop (test_step)
- Prediction loop (predict_step)
- Optimizers and LR Schedulers (configure_optimizers)
Docs:
https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html
"""
def __init__(
self,
network: DictConfig,
optimizer: DictConfig,
scheduler: DictConfig,
logging: DictConfig,
*args: Any,
**kwargs: Any,
) -> None:
"""LightningModule with standalone train, val and test dataloaders.
Args:
network (DictConfig): Network config.
optimizer (DictConfig): Optimizer config.
scheduler (DictConfig): Scheduler config.
logging (DictConfig): Logging config.
args (Any): Additional arguments for pytorch_lightning.LightningModule.
kwargs (Any): Additional keyword arguments for pytorch_lightning.LightningModule.
"""
super().__init__(
network, optimizer, scheduler, logging, *args, **kwargs
)
self.loss = load_loss(network.loss)
self.output_activation = hydra.utils.instantiate(
network.output_activation, _partial_=True
)
main_metric, valid_metric_best, add_metrics = load_metrics(
network.metrics
)
self.train_metric = main_metric.clone()
self.train_add_metrics = add_metrics.clone(postfix="/train")
self.valid_metric = main_metric.clone()
self.valid_metric_best = valid_metric_best.clone()
self.valid_add_metrics = add_metrics.clone(postfix="/valid")
self.test_metric = main_metric.clone()
self.test_add_metrics = add_metrics.clone(postfix="/test")
self.save_hyperparameters(logger=False)
def model_step(self, batch: Any, *args: Any, **kwargs: Any) -> Any:
logits = self.forward(batch["image"])
loss = self.loss(logits, batch["label"])
preds = self.output_activation(logits)
return loss, preds, batch["label"]
def on_train_start(self) -> None:
# by default lightning executes validation step sanity checks before
# training starts, so we need to make sure valid_metric_best doesn't store
# accuracy from these checks
self.valid_metric_best.reset()
def training_step(self, batch: Any, batch_idx: int) -> Any:
loss, preds, targets = self.model_step(batch, batch_idx)
self.log(
f"{self.loss.__class__.__name__}/train",
loss,
**self.logging_params,
)
self.train_metric(preds, targets)
self.log(
f"{self.train_metric.__class__.__name__}/train",
self.train_metric,
**self.logging_params,
)
self.train_add_metrics(preds, targets)
self.log_dict(self.train_add_metrics, **self.logging_params)
# Lightning keeps track of `training_step` outputs and metrics on GPU for
# optimization purposes. This works well for medium size datasets, but
# becomes an issue with larger ones. It might show up as a CPU memory leak
# during training step. Keep it in mind.
return {"loss": loss}
def training_epoch_end(self, outputs: List[Any]) -> None:
# `outputs` is a list of dicts returned from `training_step()`
# Warning: when overriding `training_epoch_end()`, lightning
# accumulates outputs from all batches of the epoch
# consider detaching tensors before returning them from `training_step()`
# or using `on_train_epoch_end()` instead which doesn't accumulate outputs
pass
def validation_step(self, batch: Any, batch_idx: int) -> Any:
loss, preds, targets = self.model_step(batch, batch_idx)
self.log(
f"{self.loss.__class__.__name__}/valid",
loss,
**self.logging_params,
)
self.valid_metric(preds, targets)
self.log(
f"{self.valid_metric.__class__.__name__}/valid",
self.valid_metric,
**self.logging_params,
)
self.valid_add_metrics(preds, targets)
self.log_dict(self.valid_add_metrics, **self.logging_params)
return {"loss": loss, "preds": preds, "targets": targets}
def validation_epoch_end(self, outputs: List[Any]) -> None:
valid_metric = self.valid_metric.compute() # get current valid metric
self.valid_metric_best(valid_metric) # update best so far valid metric
# log `valid_metric_best` as a value through `.compute()` method, instead
# of as a metric object otherwise metric would be reset by lightning
# after each epoch
self.log(
f"{self.valid_metric.__class__.__name__}/valid_best",
self.valid_metric_best.compute(),
**self.logging_params,
)
def test_step(self, batch: Any, batch_idx: int) -> Any:
loss, preds, targets = self.model_step(batch, batch_idx)
self.log(
f"{self.loss.__class__.__name__}/test", loss, **self.logging_params
)
self.test_metric(preds, targets)
self.log(
f"{self.test_metric.__class__.__name__}/test",
self.test_metric,
**self.logging_params,
)
self.test_add_metrics(preds, targets)
self.log_dict(self.test_add_metrics, **self.logging_params)
return {"loss": loss, "preds": preds, "targets": targets}
def test_epoch_end(self, outputs: List[Any]) -> None:
pass
def predict_step(
self, batch: Any, batch_idx: int, dataloader_idx: int = 0
) -> Any:
logits = self.forward(batch["image"])
preds = self.output_activation(logits)
outputs = {"logits": logits, "preds": preds}
if "label" in batch:
outputs.update({"targets": batch["label"]})
if "name" in batch:
outputs.update({"names": batch["name"]})
return outputs
class MNISTLitModule(SingleLitModule):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
def model_step(self, batch: Any, *args: Any, **kwargs: Any) -> Any:
x, y = batch
logits = self.forward(x["image"])
loss = self.loss(logits, y)
preds = self.output_activation(logits)
return loss, preds, y
def predict_step(
self, batch: Any, batch_idx: int, dataloader_idx: int = 0
) -> Any:
x, y = batch
logits = self.forward(x["image"])
preds = self.output_activation(logits)
return {"logits": logits, "preds": preds, "targets": y}
class SingleVicRegLitModule(BaseLitModule):
def __init__(
self,
network: DictConfig,
optimizer: DictConfig,
scheduler: DictConfig,
logging: DictConfig,
proj_hidden_dim: int,
proj_output_dim: int,
*args: Any,
**kwargs: Any,
) -> None:
"""LightningModule with standalone train, val and test dataloaders for
Self-Supervised task using VicReg approach.
Args:
network (DictConfig): Network config.
optimizer (DictConfig): Optimizer config.
scheduler (DictConfig): Scheduler config.
logging (DictConfig): Logging config.
proj_hidden_dim (int): Projector hidden dimensions.
proj_output_dim (int): Projector output dimensions.
args (Any): Additional arguments for pytorch_lightning.LightningModule.
kwargs (Any): Additional keyword arguments for pytorch_lightning.LightningModule.
"""
super().__init__(
network, optimizer, scheduler, logging, *args, **kwargs
)
self.loss = load_loss(network.loss)
# projector
self.projector = nn.Sequential(
nn.Linear(self.model.features_dim, proj_hidden_dim),
nn.BatchNorm1d(proj_hidden_dim),
nn.ReLU(),
# nn.Linear(proj_hidden_dim, proj_hidden_dim),
# nn.BatchNorm1d(proj_hidden_dim),
# nn.ReLU(),
nn.Linear(proj_hidden_dim, proj_output_dim),
)
self.save_hyperparameters(logger=False)
def forward(self, x: Any) -> Any:
x = self.model.forward(x)
return self.projector(x)
def model_step(self, batch: Any, *args: Any, **kwargs: Any) -> Any:
z1 = self.forward(batch["z1"])
z2 = self.forward(batch["z2"])
loss = self.loss(z1, z2)
return loss
def training_step(self, batch: Any, batch_idx: int) -> Any:
loss = self.model_step(batch, batch_idx)
self.log(
f"{self.loss.__class__.__name__}/train",
loss,
**self.logging_params,
)
return {"loss": loss}
def training_epoch_end(self, outputs: List[Any]) -> None:
pass
def validation_step(self, batch: Any, batch_idx: int) -> Any:
loss = self.model_step(batch, batch_idx)
self.log(
f"{self.loss.__class__.__name__}/valid",
loss,
**self.logging_params,
)
return {"loss": loss}
def validation_epoch_end(self, outputs: List[Any]) -> None:
pass
def test_step(self, batch: Any, batch_idx: int) -> Any:
loss = self.model_step(batch, batch_idx)
self.log(
f"{self.loss.__class__.__name__}/test", loss, **self.logging_params
)
return {"loss": loss}
def test_epoch_end(self, outputs: List[Any]) -> None:
pass
class SingleReIdLitModule(SingleLitModule):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
def model_step(self, batch: Any, *args: Any, **kwargs: Any) -> Any:
embeddings = self.forward(batch["image"])
return embeddings, batch["label"]
def training_step(self, batch: Any, batch_idx: int) -> Any:
embeddings, targets = self.model_step(batch, batch_idx)
loss, logits = self.loss(embeddings, batch["label"])
preds = self.output_activation(logits)
self.log(
f"{self.loss.__class__.__name__}/train",
loss,
**self.logging_params,
)
self.train_metric(preds, targets)
self.log(
f"{self.train_metric.__class__.__name__}/train",
self.train_metric,
**self.logging_params,
)
return {"loss": loss}
def validation_step(self, batch: Any, batch_idx: int) -> Any:
embeddings, targets = self.model_step(batch, batch_idx)
with torch.no_grad():
loss, logits = self.loss(embeddings, batch["label"])
preds = self.output_activation(logits)
self.log(
f"{self.loss.__class__.__name__}/valid",
loss,
**self.logging_params,
)
self.valid_metric(preds, targets)
self.log(
f"{self.valid_metric.__class__.__name__}/valid",
self.valid_metric,
**self.logging_params,
)
return {"loss": loss, "preds": preds, "targets": targets}
def test_step(self, batch: Any, batch_idx: int) -> Any:
embeddings, targets = self.model_step(batch, batch_idx)
with torch.no_grad():
loss, logits = self.loss(embeddings, batch["label"])
preds = self.output_activation(logits)
self.log(
f"{self.loss.__class__.__name__}/test", loss, **self.logging_params
)
self.test_metric(preds, targets)
self.log(
f"{self.test_metric.__class__.__name__}/test",
self.test_metric,
**self.logging_params,
)
return {"loss": loss, "preds": preds, "targets": targets}
def predict_step(
self, batch: Any, batch_idx: int, dataloader_idx: int = 0
) -> Any:
outputs = {"embeddings": self.forward(batch["image"])}
if "name" in batch:
outputs.update({"names": batch["name"]})
return outputs