summaryrefslogtreecommitdiff
path: root/training/trainer/train.py
blob: b770c941ab888696a3d61f91837dd7e395ffc02e (plain)
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
"""Training script for PyTorch models."""

from pathlib import Path
import time
from typing import Dict, List, Optional, Tuple, Type
import warnings

from einops import rearrange
from loguru import logger
import numpy as np
import torch
from torch import Tensor
from torch.optim.swa_utils import update_bn
from training.trainer.callbacks import Callback, CallbackList, LRScheduler, SWA
from training.trainer.util import log_val_metric
import wandb

from text_recognizer.models import Model


torch.backends.cudnn.benchmark = True
np.random.seed(4711)
torch.manual_seed(4711)
torch.cuda.manual_seed(4711)


warnings.filterwarnings("ignore")


class Trainer:
    """Trainer for training PyTorch models."""

    def __init__(
        self,
        max_epochs: int,
        callbacks: List[Type[Callback]],
        transformer_model: bool = False,
        max_norm: float = 0.0,
        freeze_backbone: Optional[int] = None,
    ) -> None:
        """Initialization of the Trainer.

        Args:
            max_epochs (int): The maximum number of epochs in the training loop.
            callbacks (CallbackList): List of callbacks to be called.
            transformer_model (bool): Transformer model flag, modifies the input to the model. Default is False.
            max_norm (float): Max norm for gradient cl:ipping. Defaults to 0.0.
            freeze_backbone (Optional[int]): How many epochs to freeze the backbone for. Used when training
                Transformers. Default is None.

        """
        # Training arguments.
        self.start_epoch = 1
        self.max_epochs = max_epochs
        self.callbacks = callbacks
        self.freeze_backbone = freeze_backbone

        # Flag for setting callbacks.
        self.callbacks_configured = False

        self.transformer_model = transformer_model

        self.max_norm = max_norm

        # Model placeholders
        self.model = None

    def _configure_callbacks(self) -> None:
        """Instantiate the CallbackList."""
        if not self.callbacks_configured:
            # If learning rate schedulers are present, they need to be added to the callbacks.
            if self.model.swa_scheduler is not None:
                self.callbacks.append(SWA())
            elif self.model.lr_scheduler is not None:
                self.callbacks.append(LRScheduler())

            self.callbacks = CallbackList(self.model, self.callbacks)

    def compute_metrics(
        self, output: Tensor, targets: Tensor, loss: Tensor, batch_size: int
    ) -> Dict:
        """Computes metrics for output and target pairs."""
        # Compute metrics.
        loss = loss.detach().float().item()
        output = output.detach()
        targets = targets.detach()
        if self.model.metrics is not None:
            metrics = {}
            for metric in self.model.metrics:
                if metric == "cer" or metric == "wer":
                    metrics[metric] = self.model.metrics[metric](
                        output,
                        targets,
                        batch_size,
                        self.model.mapper(self.model.pad_token),
                    )
                else:
                    metrics[metric] = self.model.metrics[metric](output, targets)
        else:
            metrics = {}
        metrics["loss"] = loss

        return metrics

    def training_step(self, batch: int, samples: Tuple[Tensor, Tensor],) -> Dict:
        """Performs the training step."""
        # Pass the tensor to the device for computation.
        data, targets = samples
        data, targets = (
            data.to(self.model.device),
            targets.to(self.model.device),
        )

        batch_size = data.shape[0]

        # Placeholder for uxiliary loss.
        aux_loss = None

        # Forward pass.
        # Get the network prediction.
        if self.transformer_model:
            if self.freeze_backbone is not None and batch < self.freeze_backbone:
                with torch.no_grad():
                    image_features = self.model.network.extract_image_features(data)

                if isinstance(image_features, Tuple):
                    image_features, _ = image_features

                output = self.model.network.decode_image_features(
                    image_features, targets[:, :-1]
                )
            else:
                output = self.model.network.forward(data, targets[:, :-1])
                if isinstance(output, Tuple):
                    output, aux_loss = output
            output = rearrange(output, "b t v -> (b t) v")
            targets = rearrange(targets[:, 1:], "b t -> (b t)").long()
        else:
            output = self.model.forward(data)

            if isinstance(output, Tuple):
                output, aux_loss = output
                targets = data

        # Compute the loss.
        loss = self.model.criterion(output, targets)

        if aux_loss is not None:
            loss += aux_loss

        # Backward pass.
        # Clear the previous gradients.
        for p in self.model.network.parameters():
            p.grad = None

        # Compute the gradients.
        loss.backward()

        if self.max_norm > 0:
            torch.nn.utils.clip_grad_norm_(
                self.model.network.parameters(), self.max_norm
            )

        # Perform updates using calculated gradients.
        self.model.optimizer.step()

        metrics = self.compute_metrics(output, targets, loss, batch_size)

        return metrics

    def train(self) -> None:
        """Runs the training loop for one epoch."""
        # Set model to traning mode.
        self.model.train()

        for batch, samples in enumerate(self.model.train_dataloader()):
            self.callbacks.on_train_batch_begin(batch)
            metrics = self.training_step(batch, samples)
            self.callbacks.on_train_batch_end(batch, logs=metrics)

    @torch.no_grad()
    def validation_step(self, batch: int, samples: Tuple[Tensor, Tensor],) -> Dict:
        """Performs the validation step."""

        # Pass the tensor to the device for computation.
        data, targets = samples
        data, targets = (
            data.to(self.model.device),
            targets.to(self.model.device),
        )

        batch_size = data.shape[0]

        # Placeholder for uxiliary loss.
        aux_loss = None

        # Forward pass.
        # Get the network prediction.
        # Use SWA if available and using test dataset.
        if self.transformer_model:
            output = self.model.network.forward(data, targets[:, :-1])
            if isinstance(output, Tuple):
                output, aux_loss = output
            output = rearrange(output, "b t v -> (b t) v")
            targets = rearrange(targets[:, 1:], "b t -> (b t)").long()
        else:
            output = self.model.forward(data)

            if isinstance(output, Tuple):
                output, aux_loss = output
                targets = data

        # Compute the loss.
        loss = self.model.criterion(output, targets)

        if aux_loss is not None:
            loss += aux_loss

        # Compute metrics.
        metrics = self.compute_metrics(output, targets, loss, batch_size)

        return metrics

    def validate(self) -> Dict:
        """Runs the validation loop for one epoch."""
        # Set model to eval mode.
        self.model.eval()

        # Summary for the current eval loop.
        summary = []

        for batch, samples in enumerate(self.model.val_dataloader()):
            self.callbacks.on_validation_batch_begin(batch)
            metrics = self.validation_step(batch, samples)
            self.callbacks.on_validation_batch_end(batch, logs=metrics)
            summary.append(metrics)

        # Compute mean of all metrics.
        metrics_mean = {
            "val_" + metric: np.mean([x[metric] for x in summary])
            for metric in summary[0]
        }

        return metrics_mean

    def fit(self, model: Type[Model]) -> None:
        """Runs the training and evaluation loop."""

        # Sets model, loads the data, criterion, and optimizers.
        self.model = model
        self.model.prepare_data()
        self.model.configure_model()

        # Configure callbacks.
        self._configure_callbacks()

        # Set start time.
        t_start = time.time()

        self.callbacks.on_fit_begin()

        # Run the training loop.
        for epoch in range(self.start_epoch, self.max_epochs + 1):
            self.callbacks.on_epoch_begin(epoch)

            # Perform one training pass over the training set.
            self.train()

            # Evaluate the model on the validation set.
            val_metrics = self.validate()
            log_val_metric(val_metrics, epoch)

            self.callbacks.on_epoch_end(epoch, logs=val_metrics)

            if self.model.stop_training:
                break

        # Calculate the total training time.
        t_end = time.time()
        t_training = t_end - t_start

        self.callbacks.on_fit_end()

        logger.info(f"Training took {t_training:.2f} s.")

        # "Teardown".
        self.model = None

    def test(self, model: Type[Model]) -> Dict:
        """Run inference on test data."""

        # Sets model, loads the data, criterion, and optimizers.
        self.model = model
        self.model.prepare_data()
        self.model.configure_model()

        # Configure callbacks.
        self._configure_callbacks()

        self.callbacks.on_test_begin()

        self.model.eval()

        # Check if SWA network is available.
        self.model.use_swa_model()

        # Summary for the current test loop.
        summary = []

        for batch, samples in enumerate(self.model.test_dataloader()):
            metrics = self.validation_step(batch, samples)
            summary.append(metrics)

        self.callbacks.on_test_end()

        # Compute mean of all test metrics.
        metrics_mean = {
            "test_" + metric: np.mean([x[metric] for x in summary])
            for metric in summary[0]
        }

        # "Teardown".
        self.model = None

        return metrics_mean