summaryrefslogtreecommitdiff
path: root/training/trainer/callbacks/wandb_callbacks.py
blob: 552a4f4310beaf3f07f089e278ea885f15daf695 (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
"""Callback for W&B."""
from typing import Callable, Dict, List, Optional, Type

import numpy as np
from training.trainer.callbacks import Callback
import wandb

import text_recognizer.datasets.transforms as transforms
from text_recognizer.models.base import Model


class WandbCallback(Callback):
    """A custom W&B metric logger for the trainer."""

    def __init__(self, log_batch_frequency: int = None) -> None:
        """Short summary.

        Args:
            log_batch_frequency (int): If None, metrics will be logged every epoch.
                If set to an integer, callback will log every metrics every log_batch_frequency.

        """
        super().__init__()
        self.log_batch_frequency = log_batch_frequency

    def _on_batch_end(self, batch: int, logs: Dict) -> None:
        if self.log_batch_frequency and batch % self.log_batch_frequency == 0:
            wandb.log(logs, commit=True)

    def on_train_batch_end(self, batch: int, logs: Optional[Dict] = None) -> None:
        """Logs training metrics."""
        if logs is not None:
            logs["lr"] = self.model.optimizer.param_groups[0]["lr"]
            self._on_batch_end(batch, logs)

    def on_validation_batch_end(self, batch: int, logs: Optional[Dict] = None) -> None:
        """Logs validation metrics."""
        if logs is not None:
            self._on_batch_end(batch, logs)

    def on_epoch_end(self, epoch: int, logs: Dict) -> None:
        """Logs at epoch end."""
        wandb.log(logs, commit=True)


class WandbImageLogger(Callback):
    """Custom W&B callback for image logging."""

    def __init__(
        self,
        example_indices: Optional[List] = None,
        num_examples: int = 4,
        transform: Optional[bool] = None,
    ) -> None:
        """Initializes the WandbImageLogger with the model to train.

        Args:
            example_indices (Optional[List]): Indices for validation images. Defaults to None.
            num_examples (int): Number of random samples to take if example_indices are not specified. Defaults to 4.
            transform (Optional[Dict]): Use transform on image or not. Defaults to None.

        """

        super().__init__()
        self.caption = None
        self.example_indices = example_indices
        self.test_sample_indices = None
        self.num_examples = num_examples
        self.transform = (
            self._configure_transform(transform) if transform is not None else None
        )

    def _configure_transform(self, transform: Dict) -> Callable:
        args = transform["args"] or {}
        return getattr(transforms, transform["type"])(**args)

    def set_model(self, model: Type[Model]) -> None:
        """Sets the model and extracts validation images from the dataset."""
        self.model = model
        self.caption = "Validation Examples"
        if self.example_indices is None:
            self.example_indices = np.random.randint(
                0, len(self.model.val_dataset), self.num_examples
            )
        self.images = self.model.val_dataset.dataset.data[self.example_indices]
        self.targets = self.model.val_dataset.dataset.targets[self.example_indices]
        self.targets = self.targets.tolist()

    def on_test_begin(self) -> None:
        """Get samples from test dataset."""
        self.caption = "Test Examples"
        if self.test_sample_indices is None:
            self.test_sample_indices = np.random.randint(
                0, len(self.model.test_dataset), self.num_examples
            )
        self.images = self.model.test_dataset.data[self.test_sample_indices]
        self.targets = self.model.test_dataset.targets[self.test_sample_indices]
        self.targets = self.targets.tolist()

    def on_test_end(self) -> None:
        """Log test images."""
        self.on_epoch_end(0, {})

    def on_epoch_end(self, epoch: int, logs: Dict) -> None:
        """Get network predictions on validation images."""
        images = []
        for i, image in enumerate(self.images):
            image = self.transform(image) if self.transform is not None else image
            pred, conf = self.model.predict_on_image(image)
            if isinstance(self.targets[i], list):
                ground_truth = "".join(
                    [
                        self.model.mapper(int(target_index) - 26)
                        if target_index > 35
                        else self.model.mapper(int(target_index))
                        for target_index in self.targets[i]
                    ]
                ).rstrip("_")
            else:
                ground_truth = self.model.mapper(int(self.targets[i]))
            caption = f"Prediction: {pred} Confidence: {conf:.3f} Ground Truth: {ground_truth}"
            images.append(wandb.Image(image, caption=caption))

        wandb.log({f"{self.caption}": images}, commit=False)


class WandbSegmentationLogger(Callback):
    """Custom W&B callback for image logging."""

    def __init__(
        self,
        class_labels: Dict,
        example_indices: Optional[List] = None,
        num_examples: int = 4,
    ) -> None:
        """Initializes the WandbImageLogger with the model to train.

        Args:
            class_labels (Dict): A dict with int as key and class string as value.
            example_indices (Optional[List]): Indices for validation images. Defaults to None.
            num_examples (int): Number of random samples to take if example_indices are not specified. Defaults to 4.

        """

        super().__init__()
        self.caption = None
        self.class_labels = {int(k): v for k, v in class_labels.items()}
        self.example_indices = example_indices
        self.test_sample_indices = None
        self.num_examples = num_examples

    def set_model(self, model: Type[Model]) -> None:
        """Sets the model and extracts validation images from the dataset."""
        self.model = model
        self.caption = "Validation Segmentation Examples"
        if self.example_indices is None:
            self.example_indices = np.random.randint(
                0, len(self.model.val_dataset), self.num_examples
            )
        self.images = self.model.val_dataset.dataset.data[self.example_indices]
        self.targets = self.model.val_dataset.dataset.targets[self.example_indices]
        self.targets = self.targets.tolist()

    def on_test_begin(self) -> None:
        """Get samples from test dataset."""
        self.caption = "Test Segmentation Examples"
        if self.test_sample_indices is None:
            self.test_sample_indices = np.random.randint(
                0, len(self.model.test_dataset), self.num_examples
            )
        self.images = self.model.test_dataset.data[self.test_sample_indices]
        self.targets = self.model.test_dataset.targets[self.test_sample_indices]
        self.targets = self.targets.tolist()

    def on_test_end(self) -> None:
        """Log test images."""
        self.on_epoch_end(0, {})

    def on_epoch_end(self, epoch: int, logs: Dict) -> None:
        """Get network predictions on validation images."""
        images = []
        for i, image in enumerate(self.images):
            pred_mask = (
                self.model.predict_on_image(image).detach().squeeze(0).cpu().numpy()
            )
            gt_mask = np.array(self.targets[i])
            images.append(
                wandb.Image(
                    image,
                    masks={
                        "predictions": {
                            "mask_data": pred_mask,
                            "class_labels": self.class_labels,
                        },
                        "ground_truth": {
                            "mask_data": gt_mask,
                            "class_labels": self.class_labels,
                        },
                    },
                )
            )

        wandb.log({f"{self.caption}": images}, commit=False)


class WandbReconstructionLogger(Callback):
    """Custom W&B callback for image reconstructions logging."""

    def __init__(
        self, example_indices: Optional[List] = None, num_examples: int = 4,
    ) -> None:
        """Initializes the  WandbImageLogger with the model to train.

        Args:
            example_indices (Optional[List]): Indices for validation images. Defaults to None.
            num_examples (int): Number of random samples to take if example_indices are not specified. Defaults to 4.

        """

        super().__init__()
        self.caption = None
        self.example_indices = example_indices
        self.test_sample_indices = None
        self.num_examples = num_examples

    def set_model(self, model: Type[Model]) -> None:
        """Sets the model and extracts validation images from the dataset."""
        self.model = model
        self.caption = "Validation Reconstructions Examples"
        if self.example_indices is None:
            self.example_indices = np.random.randint(
                0, len(self.model.val_dataset), self.num_examples
            )
        self.images = self.model.val_dataset.dataset.data[self.example_indices]

    def on_test_begin(self) -> None:
        """Get samples from test dataset."""
        self.caption = "Test Reconstructions Examples"
        if self.test_sample_indices is None:
            self.test_sample_indices = np.random.randint(
                0, len(self.model.test_dataset), self.num_examples
            )
        self.images = self.model.test_dataset.data[self.test_sample_indices]

    def on_test_end(self) -> None:
        """Log test images."""
        self.on_epoch_end(0, {})

    def on_epoch_end(self, epoch: int, logs: Dict) -> None:
        """Get network predictions on validation images."""
        images = []
        for image in self.images:
            reconstructed_image = (
                self.model.predict_on_image(image).detach().squeeze(0).cpu().numpy()
            )
            images.append(image)
            images.append(reconstructed_image)

        wandb.log(
            {f"{self.caption}": [wandb.Image(image) for image in images]}, commit=False,
        )