Skip to content

nebulamodel

NebulaModel

Bases: LightningModule, ABC

Abstract class for the NEBULA model.

This class is an abstract class that defines the interface for the NEBULA model.

Source code in nebula/core/models/nebulamodel.py
 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
class NebulaModel(pl.LightningModule, ABC):
    """
    Abstract class for the NEBULA model.

    This class is an abstract class that defines the interface for the NEBULA model.
    """

    def process_metrics(self, phase, y_pred, y, loss=None):
        """
        Calculate and log metrics for the given phase.
        The metrics are calculated in each batch.
        Args:
            phase (str): One of 'Train', 'Validation', or 'Test'
            y_pred (torch.Tensor): Model predictions
            y (torch.Tensor): Ground truth labels
            loss (torch.Tensor, optional): Loss value
        """

        y_pred_classes = torch.argmax(y_pred, dim=1).detach()
        y = y.detach()
        if phase == "Train":
            self.logger.log_data({f"{phase}/Loss": loss.detach()})
            self.train_metrics.update(y_pred_classes, y)
        elif phase == "Validation":
            self.val_metrics.update(y_pred_classes, y)
        elif phase == "Test (Local)":
            self.test_metrics.update(y_pred_classes, y)
            self.cm.update(y_pred_classes, y) if self.cm is not None else None
        elif phase == "Test (Global)":
            self.test_metrics_global.update(y_pred_classes, y)
            self.cm_global.update(y_pred_classes, y) if self.cm_global is not None else None
        else:
            raise NotImplementedError

        del y_pred_classes, y

    def log_metrics_end(self, phase):
        """
        Log metrics for the given phase.
        Args:
            phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'
            print_cm (bool): Print confusion matrix
            plot_cm (bool): Plot confusion matrix
        """
        if phase == "Train":
            output = self.train_metrics.compute()
        elif phase == "Validation":
            output = self.val_metrics.compute()
        elif phase == "Test (Local)":
            output = self.test_metrics.compute()
        elif phase == "Test (Global)":
            output = self.test_metrics_global.compute()
        else:
            raise NotImplementedError

        output = {
            f"{phase}/{key.replace('Multiclass', '').split('/')[-1]}": value.detach() for key, value in output.items()
        }

        self.logger.log_data(output, step=self.global_number[phase])

        metrics_str = ""
        for key, value in output.items():
            metrics_str += f"{key}: {value:.4f}\n"
        print_msg_box(
            metrics_str,
            indent=2,
            title=f"{phase} Metrics | Epoch: {self.global_number[phase]} | Round: {self.round}",
            logger_name=TRAINING_LOGGER,
        )

    def generate_confusion_matrix(self, phase, print_cm=False, plot_cm=False):
        """
        Generate and plot the confusion matrix for the given phase.
        Args:
            phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'
        """
        if phase == "Test (Local)":
            if self.cm is None:
                raise ValueError(f"Confusion matrix not available for {phase} phase.")
            cm = self.cm.compute().cpu()
        elif phase == "Test (Global)":
            if self.cm_global is None:
                raise ValueError(f"Confusion matrix not available for {phase} phase.")
            cm = self.cm_global.compute().cpu()
        else:
            raise NotImplementedError

        if print_cm:
            logging_training.info(f"{phase} / Confusion Matrix:\n{cm}")

        if plot_cm:
            cm_numpy = cm.numpy().astype(int)
            classes = [i for i in range(self.num_classes)]
            fig, ax = plt.subplots(figsize=(12, 12))
            sns.heatmap(
                cm_numpy,
                annot=False,
                fmt="",
                cmap="Blues",
                ax=ax,
                xticklabels=classes,
                yticklabels=classes,
                square=True,
            )
            ax.set_xlabel("Predicted labels", fontsize=12)
            ax.set_ylabel("True labels", fontsize=12)
            ax.set_title(f"{phase} Confusion Matrix", fontsize=16)
            plt.xticks(rotation=90, fontsize=6)
            plt.yticks(rotation=0, fontsize=6)
            plt.tight_layout()
            self.logger.log_figure(fig, step=self.round, name=f"{phase}/CM")
            plt.close()

            del cm_numpy, classes, fig, ax

        # Restablecer la matriz de confusión
        if phase == "Test (Local)":
            self.cm.reset()
        else:
            self.cm_global.reset()

        del cm

    def __init__(
        self,
        input_channels=1,
        num_classes=10,
        learning_rate=1e-3,
        metrics=None,
        confusion_matrix=None,
        seed=None,
    ):
        super().__init__()

        self.input_channels = input_channels
        self.num_classes = num_classes
        self.learning_rate = learning_rate

        if metrics is None:
            metrics = MetricCollection([
                MulticlassAccuracy(num_classes=num_classes),
                MulticlassPrecision(num_classes=num_classes),
                MulticlassRecall(num_classes=num_classes),
                MulticlassF1Score(num_classes=num_classes),
            ])
        self.train_metrics = metrics.clone(prefix="Train/")
        self.val_metrics = metrics.clone(prefix="Validation/")
        self.test_metrics = metrics.clone(prefix="Test (Local)/")
        self.test_metrics_global = metrics.clone(prefix="Test (Global)/")
        del metrics
        if confusion_matrix is None:
            self.cm = MulticlassConfusionMatrix(num_classes=num_classes)
            self.cm_global = MulticlassConfusionMatrix(num_classes=num_classes)
        if seed is not None:
            torch.manual_seed(seed)
            torch.cuda.manual_seed_all(seed)

        # Round counter (number of training-validation-test rounds)
        self.round = 0

        # Epochs counter
        self.global_number = {
            "Train": 0,
            "Validation": 0,
            "Test (Local)": 0,
            "Test (Global)": 0,
        }

        # Communication manager for sending messages from the model (e.g., prototypes, gradients)
        # Model parameters are sent by default using network.propagator
        self.communication_manager = None

    def set_communication_manager(self, communication_manager):
        self.communication_manager = communication_manager

    def get_communication_manager(self):
        if self.communication_manager is None:
            raise ValueError("Communication manager not set.")
        return self.communication_manager

    @abstractmethod
    def forward(self, x):
        """Forward pass of the model."""
        pass

    @abstractmethod
    def configure_optimizers(self):
        """Optimizer configuration."""
        pass

    def step(self, batch, batch_idx, phase):
        """Training/validation/test step."""
        x, y = batch
        y_pred = self.forward(x)
        loss = self.criterion(y_pred, y)
        self.process_metrics(phase, y_pred, y, loss)

        return loss

    def training_step(self, batch, batch_idx):
        """
        Training step for the model.
        Args:
            batch:
            batch_id:

        Returns:
        """
        return self.step(batch, batch_idx=batch_idx, phase="Train")

    def on_train_start(self):
        logging_training.info(f"{'=' * 10} [Training] Started {'=' * 10}")

    def on_train_end(self):
        logging_training.info(f"{'=' * 10} [Training] Done {'=' * 10}")

    def on_train_epoch_end(self):
        self.log_metrics_end("Train")
        self.train_metrics.reset()
        self.global_number["Train"] += 1

    def validation_step(self, batch, batch_idx):
        """
        Validation step for the model.
        Args:
            batch:
            batch_idx:

        Returns:
        """
        return self.step(batch, batch_idx=batch_idx, phase="Validation")

    def on_validation_end(self):
        pass

    def on_validation_epoch_end(self):
        # In general, the validation phase is done in one epoch
        self.log_metrics_end("Validation")
        self.val_metrics.reset()
        self.global_number["Validation"] += 1

    def test_step(self, batch, batch_idx, dataloader_idx=None):
        """
        Test step for the model.
        Args:
            batch:
            batch_idx:

        Returns:
        """
        if dataloader_idx == 0:
            return self.step(batch, batch_idx=batch_idx, phase="Test (Local)")
        else:
            return self.step(batch, batch_idx=batch_idx, phase="Test (Global)")

    def on_test_start(self):
        logging_training.info(f"{'=' * 10} [Testing] Started {'=' * 10}")

    def on_test_end(self):
        logging_training.info(f"{'=' * 10} [Testing] Done {'=' * 10}")

    def on_test_epoch_end(self):
        # In general, the test phase is done in one epoch
        self.log_metrics_end("Test (Local)")
        self.log_metrics_end("Test (Global)")
        self.generate_confusion_matrix("Test (Local)", print_cm=True, plot_cm=True)
        self.generate_confusion_matrix("Test (Global)", print_cm=True, plot_cm=True)
        self.test_metrics.reset()
        self.test_metrics_global.reset()
        self.global_number["Test (Local)"] += 1
        self.global_number["Test (Global)"] += 1

    def on_round_end(self):
        self.round += 1

configure_optimizers() abstractmethod

Optimizer configuration.

Source code in nebula/core/models/nebulamodel.py
213
214
215
216
@abstractmethod
def configure_optimizers(self):
    """Optimizer configuration."""
    pass

forward(x) abstractmethod

Forward pass of the model.

Source code in nebula/core/models/nebulamodel.py
208
209
210
211
@abstractmethod
def forward(self, x):
    """Forward pass of the model."""
    pass

generate_confusion_matrix(phase, print_cm=False, plot_cm=False)

Generate and plot the confusion matrix for the given phase. Args: phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'

Source code in nebula/core/models/nebulamodel.py
 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
def generate_confusion_matrix(self, phase, print_cm=False, plot_cm=False):
    """
    Generate and plot the confusion matrix for the given phase.
    Args:
        phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'
    """
    if phase == "Test (Local)":
        if self.cm is None:
            raise ValueError(f"Confusion matrix not available for {phase} phase.")
        cm = self.cm.compute().cpu()
    elif phase == "Test (Global)":
        if self.cm_global is None:
            raise ValueError(f"Confusion matrix not available for {phase} phase.")
        cm = self.cm_global.compute().cpu()
    else:
        raise NotImplementedError

    if print_cm:
        logging_training.info(f"{phase} / Confusion Matrix:\n{cm}")

    if plot_cm:
        cm_numpy = cm.numpy().astype(int)
        classes = [i for i in range(self.num_classes)]
        fig, ax = plt.subplots(figsize=(12, 12))
        sns.heatmap(
            cm_numpy,
            annot=False,
            fmt="",
            cmap="Blues",
            ax=ax,
            xticklabels=classes,
            yticklabels=classes,
            square=True,
        )
        ax.set_xlabel("Predicted labels", fontsize=12)
        ax.set_ylabel("True labels", fontsize=12)
        ax.set_title(f"{phase} Confusion Matrix", fontsize=16)
        plt.xticks(rotation=90, fontsize=6)
        plt.yticks(rotation=0, fontsize=6)
        plt.tight_layout()
        self.logger.log_figure(fig, step=self.round, name=f"{phase}/CM")
        plt.close()

        del cm_numpy, classes, fig, ax

    # Restablecer la matriz de confusión
    if phase == "Test (Local)":
        self.cm.reset()
    else:
        self.cm_global.reset()

    del cm

log_metrics_end(phase)

Log metrics for the given phase. Args: phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)' print_cm (bool): Print confusion matrix plot_cm (bool): Plot confusion matrix

Source code in nebula/core/models/nebulamodel.py
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
def log_metrics_end(self, phase):
    """
    Log metrics for the given phase.
    Args:
        phase (str): One of 'Train', 'Validation', 'Test (Local)', or 'Test (Global)'
        print_cm (bool): Print confusion matrix
        plot_cm (bool): Plot confusion matrix
    """
    if phase == "Train":
        output = self.train_metrics.compute()
    elif phase == "Validation":
        output = self.val_metrics.compute()
    elif phase == "Test (Local)":
        output = self.test_metrics.compute()
    elif phase == "Test (Global)":
        output = self.test_metrics_global.compute()
    else:
        raise NotImplementedError

    output = {
        f"{phase}/{key.replace('Multiclass', '').split('/')[-1]}": value.detach() for key, value in output.items()
    }

    self.logger.log_data(output, step=self.global_number[phase])

    metrics_str = ""
    for key, value in output.items():
        metrics_str += f"{key}: {value:.4f}\n"
    print_msg_box(
        metrics_str,
        indent=2,
        title=f"{phase} Metrics | Epoch: {self.global_number[phase]} | Round: {self.round}",
        logger_name=TRAINING_LOGGER,
    )

process_metrics(phase, y_pred, y, loss=None)

Calculate and log metrics for the given phase. The metrics are calculated in each batch. Args: phase (str): One of 'Train', 'Validation', or 'Test' y_pred (torch.Tensor): Model predictions y (torch.Tensor): Ground truth labels loss (torch.Tensor, optional): Loss value

Source code in nebula/core/models/nebulamodel.py
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
def process_metrics(self, phase, y_pred, y, loss=None):
    """
    Calculate and log metrics for the given phase.
    The metrics are calculated in each batch.
    Args:
        phase (str): One of 'Train', 'Validation', or 'Test'
        y_pred (torch.Tensor): Model predictions
        y (torch.Tensor): Ground truth labels
        loss (torch.Tensor, optional): Loss value
    """

    y_pred_classes = torch.argmax(y_pred, dim=1).detach()
    y = y.detach()
    if phase == "Train":
        self.logger.log_data({f"{phase}/Loss": loss.detach()})
        self.train_metrics.update(y_pred_classes, y)
    elif phase == "Validation":
        self.val_metrics.update(y_pred_classes, y)
    elif phase == "Test (Local)":
        self.test_metrics.update(y_pred_classes, y)
        self.cm.update(y_pred_classes, y) if self.cm is not None else None
    elif phase == "Test (Global)":
        self.test_metrics_global.update(y_pred_classes, y)
        self.cm_global.update(y_pred_classes, y) if self.cm_global is not None else None
    else:
        raise NotImplementedError

    del y_pred_classes, y

step(batch, batch_idx, phase)

Training/validation/test step.

Source code in nebula/core/models/nebulamodel.py
218
219
220
221
222
223
224
225
def step(self, batch, batch_idx, phase):
    """Training/validation/test step."""
    x, y = batch
    y_pred = self.forward(x)
    loss = self.criterion(y_pred, y)
    self.process_metrics(phase, y_pred, y, loss)

    return loss

test_step(batch, batch_idx, dataloader_idx=None)

Test step for the model. Args: batch: batch_idx:

Returns:

Source code in nebula/core/models/nebulamodel.py
269
270
271
272
273
274
275
276
277
278
279
280
281
def test_step(self, batch, batch_idx, dataloader_idx=None):
    """
    Test step for the model.
    Args:
        batch:
        batch_idx:

    Returns:
    """
    if dataloader_idx == 0:
        return self.step(batch, batch_idx=batch_idx, phase="Test (Local)")
    else:
        return self.step(batch, batch_idx=batch_idx, phase="Test (Global)")

training_step(batch, batch_idx)

Training step for the model. Args: batch: batch_id:

Returns:

Source code in nebula/core/models/nebulamodel.py
227
228
229
230
231
232
233
234
235
236
def training_step(self, batch, batch_idx):
    """
    Training step for the model.
    Args:
        batch:
        batch_id:

    Returns:
    """
    return self.step(batch, batch_idx=batch_idx, phase="Train")

validation_step(batch, batch_idx)

Validation step for the model. Args: batch: batch_idx:

Returns:

Source code in nebula/core/models/nebulamodel.py
249
250
251
252
253
254
255
256
257
258
def validation_step(self, batch, batch_idx):
    """
    Validation step for the model.
    Args:
        batch:
        batch_idx:

    Returns:
    """
    return self.step(batch, batch_idx=batch_idx, phase="Validation")