Skip to content

napari_cellseg3d.code_models.worker_training

Contains the workers used to train the models.

Classes:

Name Description
SupervisedTrainingWorker

A custom worker to run supervised training jobs in.

TrainingWorkerBase

A basic worker abstract class, to run training jobs in.

WNetTrainingWorker

A custom worker to run WNet (unsupervised) training jobs in.

SupervisedTrainingWorker

Bases: TrainingWorkerBase

A custom worker to run supervised training jobs in.

Inherits from :py:class:napari.qt.threading.GeneratorWorker via :py:class:TrainingWorkerBase.

Methods:

Name Description
__init__

Initializes a worker for inference with the arguments needed by the :py:func:~train function. Note: See :py:func:~train.

log_parameters

Logs the parameters of the training.

train

Trains the PyTorch model for the given number of epochs.

Source code in napari_cellseg3d/code_models/worker_training.py
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
class SupervisedTrainingWorker(TrainingWorkerBase):
    """A custom worker to run supervised training jobs in.

    Inherits from :py:class:`napari.qt.threading.GeneratorWorker` via :py:class:`TrainingWorkerBase`.
    """

    labels_not_semantic = False

    def __init__(
        self,
        worker_config: config.SupervisedTrainingWorkerConfig,
    ):
        """Initializes a worker for inference with the arguments needed by the :py:func:`~train` function. Note: See :py:func:`~train`.

        Config provides the following attributes:
            * device : device to train on, cuda or cpu

            * model_dict : dict containing the model's "name" and "class"

            * weights_path : path to weights files if transfer learning is to be used

            * data_dicts : dict from :py:func:`Trainer.create_train_dataset_dict`

            * validation_percent : percentage of images to use as validation

            * max_epochs : the amout of epochs to train for

            * loss_function : the loss function to use for training

            * learning_rate : the learning rate of the optimizer

            * val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not

            * batch_size : the batch size to use for training

            * results_path : the path to save results in

            * sampling : whether to extract patches from images or not

            * num_samples : the number of samples to extract from an image for training

            * sample_size : the size of the patches to extract when sampling

            * do_augmentation : whether to perform data augmentation or not

            * deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG

        """
        super().__init__()  # worker function is self.train in parent class
        self.config = worker_config
        #######################################
        self.loss_dict = {
            "Dice": DiceLoss(sigmoid=True),
            # "BCELoss": torch.nn.BCELoss(), # dev
            # "BCELogits": torch.nn.BCEWithLogitsLoss(),
            "Generalized Dice": GeneralizedDiceLoss(sigmoid=True),
            "DiceCE": DiceCELoss(sigmoid=True, lambda_ce=0.5),
            "Tversky": TverskyLoss(sigmoid=True),
            # "Focal loss": FocalLoss(),
            # "Dice-Focal loss": DiceFocalLoss(sigmoid=True, lambda_dice=0.5),
        }
        self.loss_function = None

    def _set_loss_from_config(self):
        try:
            self.loss_function = self.loss_dict[self.config.loss_function]
        except KeyError as e:
            self.raise_error(e, "Loss function not found, aborting job")
        return self.loss_function

    def log_parameters(self):
        """Logs the parameters of the training."""
        self.log("-" * 20)
        self.log("Parameters summary :\n")

        self.log(
            f"Percentage of dataset used for training : {self.config.training_percent * 100}%"
        )

        # self.log("-" * 10)
        self.log("Training files :\n")
        [
            self.log(f"- {Path(train_file['image']).name}\n")
            for train_file in self.train_files
        ]
        # self.log("-" * 10)
        self.log("Validation files :\n")
        [self.log(f"- {Path(val_file['image']).name}\n") for val_file in self.val_files]
        # self.log("-" * 10)

        if self.config.deterministic_config.enabled:
            self.log("Deterministic training is enabled")
            self.log(f"Seed is {self.config.deterministic_config.seed}")

        self.log(f"Training for {self.config.max_epochs} epochs")
        self.log(f"Loss function is : {str(self.config.loss_function)}")
        self.log(
            f"Validation is performed every {self.config.validation_interval} epochs"
        )
        self.log(f"Batch size is {self.config.batch_size}")
        self.log(f"Learning rate is {self.config.learning_rate}")

        if self.config.sampling:
            self.log(
                f"Extracting {self.config.num_samples} patches of size {self.config.sample_size}"
            )
        else:
            self.log("Using whole images as dataset")

        if self.config.do_augmentation:
            self.log("Data augmentation is enabled")

        if self.config.weights_info.use_custom:
            self.log(f"Using weights from : {self.config.weights_info.path}")
            if self._weight_error:
                self.log(
                    ">>>>>>>>>>>>>>>>>\n"
                    "WARNING:\nChosen weights were incompatible with the model,\n"
                    "the model will be trained from random weights\n"
                    "<<<<<<<<<<<<<<<<<\n"
                )

        # self.log("\n")
        # self.log("-" * 20)

    def train(
        self,
        provided_model=None,
        provided_optimizer=None,
        provided_loss=None,
        provided_scheduler=None,
    ):
        """Trains the PyTorch model for the given number of epochs.

        Uses the selected model and data, using the chosen batch size, validation interval, loss function, and number of samples.
        Will perform validation once every :py:obj:`val_interval` and save results if the mean dice is better.

        Requires:

        * device : device to train on, cuda or cpu

        * model_dict : dict containing the model's "name" and "class"

        * weights_path : path to weights files if transfer learning is to be used

        * data_dicts : dict from :py:func:`Trainer.create_train_dataset_dict`

        * validation_percent : percentage of images to use as validation

        * max_epochs : the amount of epochs to train for

        * loss_function : the loss function to use for training

        * learning rate : the learning rate of the optimizer

        * val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not

        * batch_size : the batch size to use for training

        * results_path : the path to save results in

        * sampling : whether to extract patches from images or not

        * num_samples : the number of samples to extract from an image for training

        * sample_size : the size of the patches to extract when sampling

        * do_augmentation : whether to perform data augmentation or not

        * deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG
        """
        #########################
        # error_log = open(results_path +"/error_log.log" % multiprocessing.current_process().name, 'x')
        # faulthandler.enable(file=error_log, all_threads=True)
        #########################
        model_config = self.config.model_info
        weights_config = self.config.weights_info
        deterministic_config = self.config.deterministic_config

        if self.config.device == "mps":
            from os import environ

            environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

        start_time = time.time()

        try:
            if WANDB_INSTALLED:
                config_dict = self.config.__dict__
                logger.debug(f"wandb config : {config_dict}")
                try:
                    if wandb.run is not None:
                        logger.warning(
                            "A previous wandb run is still active. It will be stopped before starting a new one."
                        )
                        wandb.finish()
                    wandb.init(
                        config=config_dict,
                        project="CellSeg3D",
                        name=f"{model_config.name}_supervised_training - {utils.get_date_time()}",
                        tags=[f"{model_config.name}", "supervised"],
                        mode=self.wandb_config.mode,
                    )
                except AttributeError:
                    logger.warning(
                        "Could not initialize wandb."
                        "This might be due to running napari in a folder where there is a directory named 'wandb'."
                        "Aborting, please run napari in a different folder or install wandb. Sorry for the inconvenience."
                    )

            if deterministic_config.enabled:
                set_determinism(
                    seed=deterministic_config.seed
                )  # use_deterministic_algorithms = True causes cuda error

            sys = platform.system()
            logger.debug(sys)
            if sys == "Darwin":  # required for macOS ?
                torch.set_num_threads(1)
                self.log("Number of threads has been set to 1 for macOS")

            self.log(f"config model : {self.config.model_info.name}")
            model_name = model_config.name
            model_class = model_config.get_model()

            ######## Check that labels are semantic, not instance
            check_labels = LoadImaged(keys=["label"])(self.config.train_data_dict[0])
            if check_labels["label"].max() > 1:
                self.warn(
                    "Labels are not semantic, but instance. Converting to semantic, this might cause errors."
                )
                self.labels_not_semantic = True
            ########

            if not self.config.sampling:
                data_check = LoadImaged(keys=["image"])(self.config.train_data_dict[0])
                check = data_check["image"].shape
            do_sampling = self.config.sampling
            size = self.config.sample_size if do_sampling else check
            PADDING = utils.get_padding_dim(size)

            model = (
                model_class(input_img_size=PADDING, use_checkpoint=True)
                if provided_model is None
                else provided_model
            )

            device = torch.device(self.config.device)
            model = model.to(device)

            if WANDB_INSTALLED:
                wandb.watch(model, log_freq=100)

            epoch_loss_values = []
            val_metric_values = []

            if len(self.config.train_data_dict) > 1:
                self.train_files, self.val_files = (
                    self.config.train_data_dict[
                        0 : int(
                            len(self.config.train_data_dict)
                            * self.config.training_percent
                        )
                    ],
                    self.config.train_data_dict[
                        int(
                            len(self.config.train_data_dict)
                            * self.config.training_percent
                        ) :
                    ],
                )
            else:
                self.train_files = self.val_files = self.config.train_data_dict
                msg = f"Only one image file was provided : {self.config.train_data_dict[0]['image']}.\n"

                logger.debug(f"SAMPLING is {self.config.sampling}")
                if not self.config.sampling:
                    msg += "Sampling is not in use, the only image provided will be used as the validation file."
                    self.warn(msg)
                else:
                    msg += "Samples for validation will be cropped for the same only volume that is being used for training"

                logger.warning(msg)

            logger.debug(f"Data dict from config is {self.config.train_data_dict}")
            logger.debug(f"Train files : {self.train_files}")
            logger.debug(f"Val. files : {self.val_files}")

            if len(self.train_files) == 0:
                raise ValueError("Training dataset is empty")
            if len(self.val_files) == 0:
                raise ValueError("Validation dataset is empty")

            if self.config.do_augmentation:
                train_transforms = Compose(  # TODO : figure out which ones and values ?
                    [
                        RandShiftIntensityd(keys=["image"], offsets=0.7),
                        Rand3DElasticd(
                            keys=["image", "label"],
                            sigma_range=(0.3, 0.7),
                            magnitude_range=(0.3, 0.7),
                        ),
                        RandFlipd(keys=["image", "label"]),
                        RandRotate90d(keys=["image", "label"]),
                        RandAffined(
                            keys=["image"],
                        ),
                        EnsureTyped(keys=["image"]),
                    ]
                )
            else:
                train_transforms = EnsureTyped(keys=["image", "label"])

            val_transforms = Compose(
                [
                    EnsureTyped(keys=["image", "label"]),
                ]
            )

            def get_patch_loader_func(num_samples):
                """Returns a function that will be used to extract patches from the images."""
                return Compose(
                    [
                        LoadImaged(keys=["image", "label"]),
                        EnsureChannelFirstd(keys=["image", "label"]),
                        RandSpatialCropSamplesd(
                            keys=["image", "label"],
                            roi_size=(
                                self.config.sample_size
                            ),  # multiply by axis_stretch_factor if anisotropy
                            # max_roi_size=(120, 120, 120),
                            random_size=False,
                            num_samples=num_samples,
                        ),
                        Orientationd(keys=["image", "label"], axcodes="PLI"),
                        SpatialPadd(
                            keys=["image", "label"],
                            spatial_size=(
                                utils.get_padding_dim(self.config.sample_size)
                            ),
                        ),
                        QuantileNormalizationd(keys=["image"]),
                        EnsureTyped(keys=["image"]),
                    ]
                )

            if do_sampling:
                # if there is only one volume, split samples
                # TODO(cyril) : maybe implement something in user config to toggle this behavior
                if len(self.config.train_data_dict) < 2:
                    num_train_samples = ceil(
                        self.config.num_samples * self.config.training_percent
                    )
                    num_val_samples = ceil(
                        self.config.num_samples * (1 - self.config.training_percent)
                    )
                    if num_train_samples < 2:
                        self.log(
                            "WARNING : not enough samples for training. Raising to 2"
                        )
                        num_train_samples = 2
                    if num_val_samples < 2:
                        self.log(
                            "WARNING : not enough samples for validation. Raising to 2"
                        )
                        num_val_samples = 2

                    sample_loader_train = get_patch_loader_func(num_train_samples)
                    sample_loader_eval = get_patch_loader_func(num_val_samples)
                else:
                    num_train_samples = num_val_samples = self.config.num_samples

                    sample_loader_train = get_patch_loader_func(num_train_samples)
                    sample_loader_eval = get_patch_loader_func(num_val_samples)

                logger.debug(f"AMOUNT of train samples : {num_train_samples}")
                logger.debug(f"AMOUNT of validation samples : {num_val_samples}")

                logger.debug("train_ds")
                train_dataset = PatchDataset(
                    data=self.train_files,
                    transform=train_transforms,
                    patch_func=sample_loader_train,
                    samples_per_image=num_train_samples,
                )
                logger.debug("val_ds")
                validation_dataset = PatchDataset(
                    data=self.val_files,
                    transform=val_transforms,
                    patch_func=sample_loader_eval,
                    samples_per_image=num_val_samples,
                )
            else:
                load_whole_images = Compose(
                    [
                        LoadImaged(
                            keys=["image", "label"],
                            # image_only=True,
                            # reader=WSIReader(backend="tifffile")
                        ),
                        EnsureChannelFirstd(keys=["image", "label"]),
                        Orientationd(keys=["image", "label"], axcodes="PLI"),
                        QuantileNormalizationd(keys=["image"]),
                        SpatialPadd(
                            keys=["image", "label"],
                            spatial_size=PADDING,
                        ),
                        EnsureTyped(keys=["image", "label"]),
                    ]
                )
                logger.debug("Cache dataset : train")
                train_dataset = CacheDataset(
                    data=self.train_files,
                    transform=Compose([load_whole_images, train_transforms]),
                )
                logger.debug("Cache dataset : val")
                validation_dataset = CacheDataset(
                    data=self.val_files, transform=load_whole_images
                )
            logger.debug("Dataloader")
            train_loader = self._make_dataloader(
                train_dataset,
                self.config.batch_size,
                shuffle=True,
                collate_fn=pad_list_data_collate,
            )
            validation_loader = self._make_dataloader(
                validation_dataset, self.config.batch_size
            )
            logger.debug("\nDone")

            logger.debug("Optimizer")
            optimizer = (
                torch.optim.Adam(model.parameters(), self.config.learning_rate)
                if provided_optimizer is None
                else provided_optimizer
            )

            factor = self.config.scheduler_factor
            if factor >= 1.0:
                self.log(f"Warning : scheduler factor is {factor} >= 1.0")
                self.log("Setting it to 0.5")
                factor = 0.5

            scheduler = (
                torch.optim.lr_scheduler.ReduceLROnPlateau(
                    optimizer=optimizer,
                    mode="min",
                    factor=factor,
                    patience=self.config.scheduler_patience,
                )
                if provided_scheduler is None
                else provided_scheduler
            )
            dice_metric = DiceMetric(
                include_background=False, reduction="mean", ignore_empty=False
            )

            best_metric = -1
            best_metric_epoch = -1

            # time = utils.get_date_time()
            logger.debug("Weights")

            if weights_config.use_custom or weights_config.use_pretrained:
                if weights_config.use_pretrained:
                    weights_file = model_class.weights_file
                    weights = str(
                        self.downloader.download_weights(model_name, weights_file)
                    )
                    weights_config.path = weights
                elif weights_config.use_custom:
                    weights = str(Path(weights_config.path))

                try:
                    model.load_state_dict(
                        torch.load(
                            weights,
                            map_location=device,
                        ),
                        strict=True,
                    )
                except RuntimeError as e:
                    logger.error(f"Error when loading weights : {e}")
                    logger.exception(e)
                    warn = (
                        "WARNING:\nIt'd seem that the weights were incompatible with the model,\n"
                        "the model will be trained from random weights"
                    )
                    self.log(warn)
                    self.warn(warn)
                    self._weight_error = True

            if "cuda" in self.config.device:
                device_id = self.config.device.split(":")[-1]
                self.log("\nUsing GPU :")
                self.log(torch.cuda.get_device_name(int(device_id)))
            else:
                self.log("Using CPU")

            self.log_parameters()

            # device = torch.device(self.config.device)
            self._set_loss_from_config()
            if provided_loss is not None:
                self.loss_function = provided_loss

            # if model_name == "test":
            #     self.quit()
            #     yield TrainingReport(False)

            for epoch in range(self.config.max_epochs):
                # self.log("\n")
                self.log("-" * 10)
                self.log(f"Epoch {epoch + 1}/{self.config.max_epochs}")
                if device.type == "cuda":
                    self.log("Memory Usage:")
                    alloc_mem = round(torch.cuda.memory_allocated(device) / 1024**3, 1)
                    reserved_mem = round(
                        torch.cuda.memory_reserved(device) / 1024**3, 1
                    )
                    self.log(f"Allocated: {alloc_mem}GB")
                    self.log(f"Cached: {reserved_mem}GB")

                model.train()
                epoch_loss = 0
                step = 0
                for batch_data in train_loader:
                    step += 1
                    inputs, labels = (
                        batch_data["image"].to(device),
                        batch_data["label"].to(device),
                    )
                    # logger.debug(f"Inputs shape : {inputs.shape}")
                    # logger.debug(f"Labels shape : {labels.shape}")
                    if self.labels_not_semantic:
                        labels = labels.clamp(0, 1)

                    optimizer.zero_grad()
                    outputs = model(inputs)
                    # logger.debug(f"Output dimensions : {outputs.shape}")
                    if outputs.shape[1] > 1:
                        outputs = outputs[
                            :, 1:, :, :
                        ]  # TODO(cyril): adapt if additional channels
                        if len(outputs.shape) < 4:
                            outputs = outputs.unsqueeze(0)
                    # logger.debug(f"Outputs shape : {outputs.shape}")
                    loss = self.loss_function(outputs, labels)

                    if WANDB_INSTALLED:
                        wandb.log({"Training/Loss": loss.item()})

                    loss.backward()
                    optimizer.step()
                    epoch_loss += loss.detach().item()
                    self.log(
                        f"* {step}/{len(train_dataset) // train_loader.batch_size}, "
                        f"Train loss: {loss.detach().item():.4f}"
                    )

                    if self._abort_requested:
                        self.log("Aborting training...")
                        model = None
                        del model
                        train_loader = None
                        del train_loader
                        validation_loader = None
                        del validation_loader
                        optimizer = None
                        del optimizer
                        scheduler = None
                        del scheduler
                        if device.type == "cuda":
                            torch.cuda.empty_cache()

                        if WANDB_INSTALLED:
                            wandb.finish()

                    yield TrainingReport(
                        show_plot=False,
                        weights=model.state_dict(),
                        supervised=True,
                    )

                if WANDB_INSTALLED:
                    wandb.log({"Training/Epoch loss": epoch_loss / step})
                    wandb.log(
                        {"LR/Model learning rate": optimizer.param_groups[0]["lr"]}
                    )

                epoch_loss /= step
                epoch_loss_values.append(epoch_loss)
                self.log(f"Epoch: {epoch + 1}, Average loss: {epoch_loss:.4f}")

                self.log("Updating scheduler...")
                scheduler.step(epoch_loss)
                self.log(f"Current learning rate: {optimizer.param_groups[0]['lr']}")

                checkpoint_output = []
                eta = (
                    (time.time() - start_time)
                    * (self.config.max_epochs / (epoch + 1) - 1)
                    / 60
                )
                self.log("ETA: " + f"{eta:.2f}" + " minutes")

                if (
                    (epoch + 1) % self.config.validation_interval == 0
                    or epoch + 1 == self.config.max_epochs
                ):
                    model.eval()
                    self.log("Performing validation...")
                    with torch.no_grad():
                        for val_data in validation_loader:
                            val_inputs, val_labels = (
                                val_data["image"].to(device),
                                val_data["label"].to(device),
                            )

                            try:
                                with torch.no_grad():
                                    val_outputs = sliding_window_inference(
                                        val_inputs,
                                        roi_size=size,
                                        sw_batch_size=self.config.batch_size,
                                        predictor=model,
                                        overlap=0.25,
                                        mode="gaussian",
                                        sigma_scale=0.01,
                                        sw_device=self.config.device,
                                        device=self.config.device,
                                        progress=False,
                                    )
                            except Exception as e:
                                self.raise_error(e, "Error during validation")

                            logger.debug(f"val_outputs shape : {val_outputs.shape}")
                            # val_outputs = model(val_inputs)

                            pred = decollate_batch(val_outputs)
                            labs = decollate_batch(val_labels)
                            # TODO : more parameters/flexibility
                            post_pred = Compose(
                                [
                                    RemapTensor(new_max=1, new_min=0),
                                    Threshold(threshold=0.5),
                                    EnsureType(),
                                ]
                            )  #
                            post_label = EnsureType()

                            output_raw = [
                                RemapTensor(new_max=1, new_min=0)(t) for t in pred
                            ]
                            # output_raw = pred

                            val_outputs = [post_pred(res_tensor) for res_tensor in pred]

                            val_labels = [post_label(res_tensor) for res_tensor in labs]

                            # logger.debug(len(val_outputs))
                            # logger.debug(len(val_labels))
                            # dice_test = np.array(
                            #     [
                            #         utils.dice_coeff(i, j)
                            #         for i, j in zip(val_outputs, val_labels)
                            #     ]
                            # )
                            # logger.debug(
                            #     f"TEST VALIDATION Dice score : {dice_test.mean()}"
                            # )

                            dice_metric(y_pred=val_outputs, y=val_labels)

                        checkpoint_output.append(
                            [
                                output_raw[0].detach().cpu(),
                                val_outputs[0].detach().cpu(),
                                val_inputs[0].detach().cpu(),
                                val_labels[0].detach().cpu(),
                            ]
                        )
                        checkpoint_output = [
                            item.numpy()
                            for channel in checkpoint_output
                            for item in channel
                        ]
                        checkpoint_output[3] = checkpoint_output[3].astype(np.uint16)

                        metric = dice_metric.aggregate().detach().item()

                        if WANDB_INSTALLED:
                            wandb.log({"Validation/Dice metric": metric})

                        dice_metric.reset()
                        val_metric_values.append(metric)

                        images_dict = {
                            "Validation output": {
                                "data": checkpoint_output[0],
                                "cmap": "turbo",
                            },
                            "Validation output (discrete)": {
                                "data": checkpoint_output[1],
                                "cmap": "bop blue",
                            },
                            "Validation image": {
                                "data": checkpoint_output[2],
                                "cmap": "inferno",
                            },
                            "Validation labels": {
                                "data": checkpoint_output[3],
                                "cmap": "green",
                            },
                        }

                        train_report = TrainingReport(
                            show_plot=True,
                            epoch=epoch,
                            loss_1_values={"Loss": epoch_loss_values},
                            loss_2_values=val_metric_values,
                            weights=model.state_dict(),
                            images_dict=images_dict,
                            supervised=True,
                        )
                        self.log("Validation completed")
                        yield train_report

                        weights_filename = (
                            f"{model_name}_best_metric"
                            # + f"_epoch_{epoch + 1}" # avoid saving per epoch
                            + ".pth"
                        )

                        if metric > best_metric:
                            best_metric = metric
                            best_metric_epoch = epoch + 1
                            self.log("Saving best metric model")
                            torch.save(
                                model.state_dict(),
                                Path(self.config.results_path_folder)
                                / Path(
                                    weights_filename,
                                ),
                            )
                            self.log("Saving complete")
                        self.log(
                            f"Current epoch: {epoch + 1}, Current mean dice: {metric:.4f}"
                            f"\nBest mean dice: {best_metric:.4f} "
                            f"at epoch: {best_metric_epoch}"
                        )
            self.log("=" * 10)
            self.log(
                f"Train completed, best_metric: {best_metric:.4f} "
                f"at epoch: {best_metric_epoch}"
            )

            if WANDB_INSTALLED:
                wandb.log({"Validation/Best metric": best_metric})
                wandb.log({"Validation/Best metric epoch": best_metric_epoch})

            # Save last checkpoint
            weights_filename = f"{model_name}_latest.pth"
            self.log("Saving last model")
            torch.save(
                model.state_dict(),
                Path(self.config.results_path_folder) / Path(weights_filename),
            )
            self.log("Saving complete, exiting")
            model.to("cpu")

            if WANDB_INSTALLED:
                wandb.finish()
            # clear (V)RAM
            model = None
            del model
            train_loader = None
            del train_loader
            validation_loader = None
            del validation_loader
            optimizer = None
            del optimizer
            scheduler = None
            del scheduler
            if device.type == "cuda":
                torch.cuda.empty_cache()

        except Exception as e:
            if WANDB_INSTALLED:
                wandb.finish()
            self.raise_error(e, "Error in training")
            self.quit()
        finally:
            self.quit()

__init__

__init__(worker_config: SupervisedTrainingWorkerConfig)

Initializes a worker for inference with the arguments needed by the :py:func:~train function. Note: See :py:func:~train.

Config provides the following attributes
  • device : device to train on, cuda or cpu

  • model_dict : dict containing the model's "name" and "class"

  • weights_path : path to weights files if transfer learning is to be used

  • data_dicts : dict from :py:func:Trainer.create_train_dataset_dict

  • validation_percent : percentage of images to use as validation

  • max_epochs : the amout of epochs to train for

  • loss_function : the loss function to use for training

  • learning_rate : the learning rate of the optimizer

  • val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not

  • batch_size : the batch size to use for training

  • results_path : the path to save results in

  • sampling : whether to extract patches from images or not

  • num_samples : the number of samples to extract from an image for training

  • sample_size : the size of the patches to extract when sampling

  • do_augmentation : whether to perform data augmentation or not

  • deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG

Source code in napari_cellseg3d/code_models/worker_training.py
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
def __init__(
    self,
    worker_config: config.SupervisedTrainingWorkerConfig,
):
    """Initializes a worker for inference with the arguments needed by the :py:func:`~train` function. Note: See :py:func:`~train`.

    Config provides the following attributes:
        * device : device to train on, cuda or cpu

        * model_dict : dict containing the model's "name" and "class"

        * weights_path : path to weights files if transfer learning is to be used

        * data_dicts : dict from :py:func:`Trainer.create_train_dataset_dict`

        * validation_percent : percentage of images to use as validation

        * max_epochs : the amout of epochs to train for

        * loss_function : the loss function to use for training

        * learning_rate : the learning rate of the optimizer

        * val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not

        * batch_size : the batch size to use for training

        * results_path : the path to save results in

        * sampling : whether to extract patches from images or not

        * num_samples : the number of samples to extract from an image for training

        * sample_size : the size of the patches to extract when sampling

        * do_augmentation : whether to perform data augmentation or not

        * deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG

    """
    super().__init__()  # worker function is self.train in parent class
    self.config = worker_config
    #######################################
    self.loss_dict = {
        "Dice": DiceLoss(sigmoid=True),
        # "BCELoss": torch.nn.BCELoss(), # dev
        # "BCELogits": torch.nn.BCEWithLogitsLoss(),
        "Generalized Dice": GeneralizedDiceLoss(sigmoid=True),
        "DiceCE": DiceCELoss(sigmoid=True, lambda_ce=0.5),
        "Tversky": TverskyLoss(sigmoid=True),
        # "Focal loss": FocalLoss(),
        # "Dice-Focal loss": DiceFocalLoss(sigmoid=True, lambda_dice=0.5),
    }
    self.loss_function = None

log_parameters

log_parameters()

Logs the parameters of the training.

Source code in napari_cellseg3d/code_models/worker_training.py
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
def log_parameters(self):
    """Logs the parameters of the training."""
    self.log("-" * 20)
    self.log("Parameters summary :\n")

    self.log(
        f"Percentage of dataset used for training : {self.config.training_percent * 100}%"
    )

    # self.log("-" * 10)
    self.log("Training files :\n")
    [
        self.log(f"- {Path(train_file['image']).name}\n")
        for train_file in self.train_files
    ]
    # self.log("-" * 10)
    self.log("Validation files :\n")
    [self.log(f"- {Path(val_file['image']).name}\n") for val_file in self.val_files]
    # self.log("-" * 10)

    if self.config.deterministic_config.enabled:
        self.log("Deterministic training is enabled")
        self.log(f"Seed is {self.config.deterministic_config.seed}")

    self.log(f"Training for {self.config.max_epochs} epochs")
    self.log(f"Loss function is : {str(self.config.loss_function)}")
    self.log(
        f"Validation is performed every {self.config.validation_interval} epochs"
    )
    self.log(f"Batch size is {self.config.batch_size}")
    self.log(f"Learning rate is {self.config.learning_rate}")

    if self.config.sampling:
        self.log(
            f"Extracting {self.config.num_samples} patches of size {self.config.sample_size}"
        )
    else:
        self.log("Using whole images as dataset")

    if self.config.do_augmentation:
        self.log("Data augmentation is enabled")

    if self.config.weights_info.use_custom:
        self.log(f"Using weights from : {self.config.weights_info.path}")
        if self._weight_error:
            self.log(
                ">>>>>>>>>>>>>>>>>\n"
                "WARNING:\nChosen weights were incompatible with the model,\n"
                "the model will be trained from random weights\n"
                "<<<<<<<<<<<<<<<<<\n"
            )

train

train(provided_model=None, provided_optimizer=None, provided_loss=None, provided_scheduler=None)

Trains the PyTorch model for the given number of epochs.

Uses the selected model and data, using the chosen batch size, validation interval, loss function, and number of samples. Will perform validation once every :py:obj:val_interval and save results if the mean dice is better.

Requires:

  • device : device to train on, cuda or cpu

  • model_dict : dict containing the model's "name" and "class"

  • weights_path : path to weights files if transfer learning is to be used

  • data_dicts : dict from :py:func:Trainer.create_train_dataset_dict

  • validation_percent : percentage of images to use as validation

  • max_epochs : the amount of epochs to train for

  • loss_function : the loss function to use for training

  • learning rate : the learning rate of the optimizer

  • val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not

  • batch_size : the batch size to use for training

  • results_path : the path to save results in

  • sampling : whether to extract patches from images or not

  • num_samples : the number of samples to extract from an image for training

  • sample_size : the size of the patches to extract when sampling

  • do_augmentation : whether to perform data augmentation or not

  • deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG

Source code in napari_cellseg3d/code_models/worker_training.py
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
def train(
    self,
    provided_model=None,
    provided_optimizer=None,
    provided_loss=None,
    provided_scheduler=None,
):
    """Trains the PyTorch model for the given number of epochs.

    Uses the selected model and data, using the chosen batch size, validation interval, loss function, and number of samples.
    Will perform validation once every :py:obj:`val_interval` and save results if the mean dice is better.

    Requires:

    * device : device to train on, cuda or cpu

    * model_dict : dict containing the model's "name" and "class"

    * weights_path : path to weights files if transfer learning is to be used

    * data_dicts : dict from :py:func:`Trainer.create_train_dataset_dict`

    * validation_percent : percentage of images to use as validation

    * max_epochs : the amount of epochs to train for

    * loss_function : the loss function to use for training

    * learning rate : the learning rate of the optimizer

    * val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not

    * batch_size : the batch size to use for training

    * results_path : the path to save results in

    * sampling : whether to extract patches from images or not

    * num_samples : the number of samples to extract from an image for training

    * sample_size : the size of the patches to extract when sampling

    * do_augmentation : whether to perform data augmentation or not

    * deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG
    """
    #########################
    # error_log = open(results_path +"/error_log.log" % multiprocessing.current_process().name, 'x')
    # faulthandler.enable(file=error_log, all_threads=True)
    #########################
    model_config = self.config.model_info
    weights_config = self.config.weights_info
    deterministic_config = self.config.deterministic_config

    if self.config.device == "mps":
        from os import environ

        environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

    start_time = time.time()

    try:
        if WANDB_INSTALLED:
            config_dict = self.config.__dict__
            logger.debug(f"wandb config : {config_dict}")
            try:
                if wandb.run is not None:
                    logger.warning(
                        "A previous wandb run is still active. It will be stopped before starting a new one."
                    )
                    wandb.finish()
                wandb.init(
                    config=config_dict,
                    project="CellSeg3D",
                    name=f"{model_config.name}_supervised_training - {utils.get_date_time()}",
                    tags=[f"{model_config.name}", "supervised"],
                    mode=self.wandb_config.mode,
                )
            except AttributeError:
                logger.warning(
                    "Could not initialize wandb."
                    "This might be due to running napari in a folder where there is a directory named 'wandb'."
                    "Aborting, please run napari in a different folder or install wandb. Sorry for the inconvenience."
                )

        if deterministic_config.enabled:
            set_determinism(
                seed=deterministic_config.seed
            )  # use_deterministic_algorithms = True causes cuda error

        sys = platform.system()
        logger.debug(sys)
        if sys == "Darwin":  # required for macOS ?
            torch.set_num_threads(1)
            self.log("Number of threads has been set to 1 for macOS")

        self.log(f"config model : {self.config.model_info.name}")
        model_name = model_config.name
        model_class = model_config.get_model()

        ######## Check that labels are semantic, not instance
        check_labels = LoadImaged(keys=["label"])(self.config.train_data_dict[0])
        if check_labels["label"].max() > 1:
            self.warn(
                "Labels are not semantic, but instance. Converting to semantic, this might cause errors."
            )
            self.labels_not_semantic = True
        ########

        if not self.config.sampling:
            data_check = LoadImaged(keys=["image"])(self.config.train_data_dict[0])
            check = data_check["image"].shape
        do_sampling = self.config.sampling
        size = self.config.sample_size if do_sampling else check
        PADDING = utils.get_padding_dim(size)

        model = (
            model_class(input_img_size=PADDING, use_checkpoint=True)
            if provided_model is None
            else provided_model
        )

        device = torch.device(self.config.device)
        model = model.to(device)

        if WANDB_INSTALLED:
            wandb.watch(model, log_freq=100)

        epoch_loss_values = []
        val_metric_values = []

        if len(self.config.train_data_dict) > 1:
            self.train_files, self.val_files = (
                self.config.train_data_dict[
                    0 : int(
                        len(self.config.train_data_dict)
                        * self.config.training_percent
                    )
                ],
                self.config.train_data_dict[
                    int(
                        len(self.config.train_data_dict)
                        * self.config.training_percent
                    ) :
                ],
            )
        else:
            self.train_files = self.val_files = self.config.train_data_dict
            msg = f"Only one image file was provided : {self.config.train_data_dict[0]['image']}.\n"

            logger.debug(f"SAMPLING is {self.config.sampling}")
            if not self.config.sampling:
                msg += "Sampling is not in use, the only image provided will be used as the validation file."
                self.warn(msg)
            else:
                msg += "Samples for validation will be cropped for the same only volume that is being used for training"

            logger.warning(msg)

        logger.debug(f"Data dict from config is {self.config.train_data_dict}")
        logger.debug(f"Train files : {self.train_files}")
        logger.debug(f"Val. files : {self.val_files}")

        if len(self.train_files) == 0:
            raise ValueError("Training dataset is empty")
        if len(self.val_files) == 0:
            raise ValueError("Validation dataset is empty")

        if self.config.do_augmentation:
            train_transforms = Compose(  # TODO : figure out which ones and values ?
                [
                    RandShiftIntensityd(keys=["image"], offsets=0.7),
                    Rand3DElasticd(
                        keys=["image", "label"],
                        sigma_range=(0.3, 0.7),
                        magnitude_range=(0.3, 0.7),
                    ),
                    RandFlipd(keys=["image", "label"]),
                    RandRotate90d(keys=["image", "label"]),
                    RandAffined(
                        keys=["image"],
                    ),
                    EnsureTyped(keys=["image"]),
                ]
            )
        else:
            train_transforms = EnsureTyped(keys=["image", "label"])

        val_transforms = Compose(
            [
                EnsureTyped(keys=["image", "label"]),
            ]
        )

        def get_patch_loader_func(num_samples):
            """Returns a function that will be used to extract patches from the images."""
            return Compose(
                [
                    LoadImaged(keys=["image", "label"]),
                    EnsureChannelFirstd(keys=["image", "label"]),
                    RandSpatialCropSamplesd(
                        keys=["image", "label"],
                        roi_size=(
                            self.config.sample_size
                        ),  # multiply by axis_stretch_factor if anisotropy
                        # max_roi_size=(120, 120, 120),
                        random_size=False,
                        num_samples=num_samples,
                    ),
                    Orientationd(keys=["image", "label"], axcodes="PLI"),
                    SpatialPadd(
                        keys=["image", "label"],
                        spatial_size=(
                            utils.get_padding_dim(self.config.sample_size)
                        ),
                    ),
                    QuantileNormalizationd(keys=["image"]),
                    EnsureTyped(keys=["image"]),
                ]
            )

        if do_sampling:
            # if there is only one volume, split samples
            # TODO(cyril) : maybe implement something in user config to toggle this behavior
            if len(self.config.train_data_dict) < 2:
                num_train_samples = ceil(
                    self.config.num_samples * self.config.training_percent
                )
                num_val_samples = ceil(
                    self.config.num_samples * (1 - self.config.training_percent)
                )
                if num_train_samples < 2:
                    self.log(
                        "WARNING : not enough samples for training. Raising to 2"
                    )
                    num_train_samples = 2
                if num_val_samples < 2:
                    self.log(
                        "WARNING : not enough samples for validation. Raising to 2"
                    )
                    num_val_samples = 2

                sample_loader_train = get_patch_loader_func(num_train_samples)
                sample_loader_eval = get_patch_loader_func(num_val_samples)
            else:
                num_train_samples = num_val_samples = self.config.num_samples

                sample_loader_train = get_patch_loader_func(num_train_samples)
                sample_loader_eval = get_patch_loader_func(num_val_samples)

            logger.debug(f"AMOUNT of train samples : {num_train_samples}")
            logger.debug(f"AMOUNT of validation samples : {num_val_samples}")

            logger.debug("train_ds")
            train_dataset = PatchDataset(
                data=self.train_files,
                transform=train_transforms,
                patch_func=sample_loader_train,
                samples_per_image=num_train_samples,
            )
            logger.debug("val_ds")
            validation_dataset = PatchDataset(
                data=self.val_files,
                transform=val_transforms,
                patch_func=sample_loader_eval,
                samples_per_image=num_val_samples,
            )
        else:
            load_whole_images = Compose(
                [
                    LoadImaged(
                        keys=["image", "label"],
                        # image_only=True,
                        # reader=WSIReader(backend="tifffile")
                    ),
                    EnsureChannelFirstd(keys=["image", "label"]),
                    Orientationd(keys=["image", "label"], axcodes="PLI"),
                    QuantileNormalizationd(keys=["image"]),
                    SpatialPadd(
                        keys=["image", "label"],
                        spatial_size=PADDING,
                    ),
                    EnsureTyped(keys=["image", "label"]),
                ]
            )
            logger.debug("Cache dataset : train")
            train_dataset = CacheDataset(
                data=self.train_files,
                transform=Compose([load_whole_images, train_transforms]),
            )
            logger.debug("Cache dataset : val")
            validation_dataset = CacheDataset(
                data=self.val_files, transform=load_whole_images
            )
        logger.debug("Dataloader")
        train_loader = self._make_dataloader(
            train_dataset,
            self.config.batch_size,
            shuffle=True,
            collate_fn=pad_list_data_collate,
        )
        validation_loader = self._make_dataloader(
            validation_dataset, self.config.batch_size
        )
        logger.debug("\nDone")

        logger.debug("Optimizer")
        optimizer = (
            torch.optim.Adam(model.parameters(), self.config.learning_rate)
            if provided_optimizer is None
            else provided_optimizer
        )

        factor = self.config.scheduler_factor
        if factor >= 1.0:
            self.log(f"Warning : scheduler factor is {factor} >= 1.0")
            self.log("Setting it to 0.5")
            factor = 0.5

        scheduler = (
            torch.optim.lr_scheduler.ReduceLROnPlateau(
                optimizer=optimizer,
                mode="min",
                factor=factor,
                patience=self.config.scheduler_patience,
            )
            if provided_scheduler is None
            else provided_scheduler
        )
        dice_metric = DiceMetric(
            include_background=False, reduction="mean", ignore_empty=False
        )

        best_metric = -1
        best_metric_epoch = -1

        # time = utils.get_date_time()
        logger.debug("Weights")

        if weights_config.use_custom or weights_config.use_pretrained:
            if weights_config.use_pretrained:
                weights_file = model_class.weights_file
                weights = str(
                    self.downloader.download_weights(model_name, weights_file)
                )
                weights_config.path = weights
            elif weights_config.use_custom:
                weights = str(Path(weights_config.path))

            try:
                model.load_state_dict(
                    torch.load(
                        weights,
                        map_location=device,
                    ),
                    strict=True,
                )
            except RuntimeError as e:
                logger.error(f"Error when loading weights : {e}")
                logger.exception(e)
                warn = (
                    "WARNING:\nIt'd seem that the weights were incompatible with the model,\n"
                    "the model will be trained from random weights"
                )
                self.log(warn)
                self.warn(warn)
                self._weight_error = True

        if "cuda" in self.config.device:
            device_id = self.config.device.split(":")[-1]
            self.log("\nUsing GPU :")
            self.log(torch.cuda.get_device_name(int(device_id)))
        else:
            self.log("Using CPU")

        self.log_parameters()

        # device = torch.device(self.config.device)
        self._set_loss_from_config()
        if provided_loss is not None:
            self.loss_function = provided_loss

        # if model_name == "test":
        #     self.quit()
        #     yield TrainingReport(False)

        for epoch in range(self.config.max_epochs):
            # self.log("\n")
            self.log("-" * 10)
            self.log(f"Epoch {epoch + 1}/{self.config.max_epochs}")
            if device.type == "cuda":
                self.log("Memory Usage:")
                alloc_mem = round(torch.cuda.memory_allocated(device) / 1024**3, 1)
                reserved_mem = round(
                    torch.cuda.memory_reserved(device) / 1024**3, 1
                )
                self.log(f"Allocated: {alloc_mem}GB")
                self.log(f"Cached: {reserved_mem}GB")

            model.train()
            epoch_loss = 0
            step = 0
            for batch_data in train_loader:
                step += 1
                inputs, labels = (
                    batch_data["image"].to(device),
                    batch_data["label"].to(device),
                )
                # logger.debug(f"Inputs shape : {inputs.shape}")
                # logger.debug(f"Labels shape : {labels.shape}")
                if self.labels_not_semantic:
                    labels = labels.clamp(0, 1)

                optimizer.zero_grad()
                outputs = model(inputs)
                # logger.debug(f"Output dimensions : {outputs.shape}")
                if outputs.shape[1] > 1:
                    outputs = outputs[
                        :, 1:, :, :
                    ]  # TODO(cyril): adapt if additional channels
                    if len(outputs.shape) < 4:
                        outputs = outputs.unsqueeze(0)
                # logger.debug(f"Outputs shape : {outputs.shape}")
                loss = self.loss_function(outputs, labels)

                if WANDB_INSTALLED:
                    wandb.log({"Training/Loss": loss.item()})

                loss.backward()
                optimizer.step()
                epoch_loss += loss.detach().item()
                self.log(
                    f"* {step}/{len(train_dataset) // train_loader.batch_size}, "
                    f"Train loss: {loss.detach().item():.4f}"
                )

                if self._abort_requested:
                    self.log("Aborting training...")
                    model = None
                    del model
                    train_loader = None
                    del train_loader
                    validation_loader = None
                    del validation_loader
                    optimizer = None
                    del optimizer
                    scheduler = None
                    del scheduler
                    if device.type == "cuda":
                        torch.cuda.empty_cache()

                    if WANDB_INSTALLED:
                        wandb.finish()

                yield TrainingReport(
                    show_plot=False,
                    weights=model.state_dict(),
                    supervised=True,
                )

            if WANDB_INSTALLED:
                wandb.log({"Training/Epoch loss": epoch_loss / step})
                wandb.log(
                    {"LR/Model learning rate": optimizer.param_groups[0]["lr"]}
                )

            epoch_loss /= step
            epoch_loss_values.append(epoch_loss)
            self.log(f"Epoch: {epoch + 1}, Average loss: {epoch_loss:.4f}")

            self.log("Updating scheduler...")
            scheduler.step(epoch_loss)
            self.log(f"Current learning rate: {optimizer.param_groups[0]['lr']}")

            checkpoint_output = []
            eta = (
                (time.time() - start_time)
                * (self.config.max_epochs / (epoch + 1) - 1)
                / 60
            )
            self.log("ETA: " + f"{eta:.2f}" + " minutes")

            if (
                (epoch + 1) % self.config.validation_interval == 0
                or epoch + 1 == self.config.max_epochs
            ):
                model.eval()
                self.log("Performing validation...")
                with torch.no_grad():
                    for val_data in validation_loader:
                        val_inputs, val_labels = (
                            val_data["image"].to(device),
                            val_data["label"].to(device),
                        )

                        try:
                            with torch.no_grad():
                                val_outputs = sliding_window_inference(
                                    val_inputs,
                                    roi_size=size,
                                    sw_batch_size=self.config.batch_size,
                                    predictor=model,
                                    overlap=0.25,
                                    mode="gaussian",
                                    sigma_scale=0.01,
                                    sw_device=self.config.device,
                                    device=self.config.device,
                                    progress=False,
                                )
                        except Exception as e:
                            self.raise_error(e, "Error during validation")

                        logger.debug(f"val_outputs shape : {val_outputs.shape}")
                        # val_outputs = model(val_inputs)

                        pred = decollate_batch(val_outputs)
                        labs = decollate_batch(val_labels)
                        # TODO : more parameters/flexibility
                        post_pred = Compose(
                            [
                                RemapTensor(new_max=1, new_min=0),
                                Threshold(threshold=0.5),
                                EnsureType(),
                            ]
                        )  #
                        post_label = EnsureType()

                        output_raw = [
                            RemapTensor(new_max=1, new_min=0)(t) for t in pred
                        ]
                        # output_raw = pred

                        val_outputs = [post_pred(res_tensor) for res_tensor in pred]

                        val_labels = [post_label(res_tensor) for res_tensor in labs]

                        # logger.debug(len(val_outputs))
                        # logger.debug(len(val_labels))
                        # dice_test = np.array(
                        #     [
                        #         utils.dice_coeff(i, j)
                        #         for i, j in zip(val_outputs, val_labels)
                        #     ]
                        # )
                        # logger.debug(
                        #     f"TEST VALIDATION Dice score : {dice_test.mean()}"
                        # )

                        dice_metric(y_pred=val_outputs, y=val_labels)

                    checkpoint_output.append(
                        [
                            output_raw[0].detach().cpu(),
                            val_outputs[0].detach().cpu(),
                            val_inputs[0].detach().cpu(),
                            val_labels[0].detach().cpu(),
                        ]
                    )
                    checkpoint_output = [
                        item.numpy()
                        for channel in checkpoint_output
                        for item in channel
                    ]
                    checkpoint_output[3] = checkpoint_output[3].astype(np.uint16)

                    metric = dice_metric.aggregate().detach().item()

                    if WANDB_INSTALLED:
                        wandb.log({"Validation/Dice metric": metric})

                    dice_metric.reset()
                    val_metric_values.append(metric)

                    images_dict = {
                        "Validation output": {
                            "data": checkpoint_output[0],
                            "cmap": "turbo",
                        },
                        "Validation output (discrete)": {
                            "data": checkpoint_output[1],
                            "cmap": "bop blue",
                        },
                        "Validation image": {
                            "data": checkpoint_output[2],
                            "cmap": "inferno",
                        },
                        "Validation labels": {
                            "data": checkpoint_output[3],
                            "cmap": "green",
                        },
                    }

                    train_report = TrainingReport(
                        show_plot=True,
                        epoch=epoch,
                        loss_1_values={"Loss": epoch_loss_values},
                        loss_2_values=val_metric_values,
                        weights=model.state_dict(),
                        images_dict=images_dict,
                        supervised=True,
                    )
                    self.log("Validation completed")
                    yield train_report

                    weights_filename = (
                        f"{model_name}_best_metric"
                        # + f"_epoch_{epoch + 1}" # avoid saving per epoch
                        + ".pth"
                    )

                    if metric > best_metric:
                        best_metric = metric
                        best_metric_epoch = epoch + 1
                        self.log("Saving best metric model")
                        torch.save(
                            model.state_dict(),
                            Path(self.config.results_path_folder)
                            / Path(
                                weights_filename,
                            ),
                        )
                        self.log("Saving complete")
                    self.log(
                        f"Current epoch: {epoch + 1}, Current mean dice: {metric:.4f}"
                        f"\nBest mean dice: {best_metric:.4f} "
                        f"at epoch: {best_metric_epoch}"
                    )
        self.log("=" * 10)
        self.log(
            f"Train completed, best_metric: {best_metric:.4f} "
            f"at epoch: {best_metric_epoch}"
        )

        if WANDB_INSTALLED:
            wandb.log({"Validation/Best metric": best_metric})
            wandb.log({"Validation/Best metric epoch": best_metric_epoch})

        # Save last checkpoint
        weights_filename = f"{model_name}_latest.pth"
        self.log("Saving last model")
        torch.save(
            model.state_dict(),
            Path(self.config.results_path_folder) / Path(weights_filename),
        )
        self.log("Saving complete, exiting")
        model.to("cpu")

        if WANDB_INSTALLED:
            wandb.finish()
        # clear (V)RAM
        model = None
        del model
        train_loader = None
        del train_loader
        validation_loader = None
        del validation_loader
        optimizer = None
        del optimizer
        scheduler = None
        del scheduler
        if device.type == "cuda":
            torch.cuda.empty_cache()

    except Exception as e:
        if WANDB_INSTALLED:
            wandb.finish()
        self.raise_error(e, "Error in training")
        self.quit()
    finally:
        self.quit()

TrainingWorkerBase

Bases: GeneratorWorker

A basic worker abstract class, to run training jobs in.

Contains the minimal common elements required for training models.

Methods:

Name Description
__init__

Initializes the worker.

log

Sends a Qt signal that the provided text should be logged.

log_parameters

Logs the parameters of the training.

raise_error

Sends an error to main thread.

set_download_log

Sets the log widget for the downloader to output to.

train

Starts a training job.

warn

Sends a warning to main thread.

Source code in napari_cellseg3d/code_models/worker_training.py
 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
class TrainingWorkerBase(GeneratorWorker):
    """A basic worker abstract class, to run training jobs in.

    Contains the minimal common elements required for training models.
    """

    wandb_config = config.WandBConfig()

    def __init__(self):
        """Initializes the worker."""
        super().__init__(self.train)
        self._signals = LogSignal()
        self.log_signal = self._signals.log_signal
        self.warn_signal = self._signals.warn_signal
        self.error_signal = self._signals.error_signal
        self.downloader = WeightsDownloader()
        self.train_files = []
        self.val_files = []
        self.config = None

        self._weight_error = False
        ################################

    def set_download_log(self, widget):
        """Sets the log widget for the downloader to output to."""
        self.downloader.log_widget = widget

    def log(self, text):
        """Sends a Qt signal that the provided text should be logged.

        Goes in a Log object, defined in :py:mod:`napari_cellseg3d.interface`.
        Sends a signal to the main thread to log the text.
        Signal is defined in napari_cellseg3d.workers_utils.LogSignal.

        Args:
            text (str): text to logged
        """
        self.log_signal.emit(text)

    def warn(self, warning):
        """Sends a warning to main thread."""
        self.warn_signal.emit(warning)

    def _make_dataloader(self, dataset, batch_size, shuffle=False, **kwargs):
        """Creates a DataLoader with optional persistent workers.

        Shuffling is skipped for iterable datasets (e.g. PatchDataset).
        """
        shuffle = shuffle and not isinstance(dataset, torch.utils.data.IterableDataset)
        num_workers = self.config.num_workers
        return DataLoader(
            dataset,
            batch_size=batch_size,
            shuffle=shuffle,
            num_workers=num_workers,
            persistent_workers=num_workers > 0,
            **kwargs,
        )

    def raise_error(self, exception, msg):
        """Sends an error to main thread."""
        logger.error(msg, exc_info=True)
        logger.error(exception, exc_info=True)
        self.error_signal.emit(exception, msg)
        self.errored.emit(exception)
        self.quit()

    @abstractmethod
    def log_parameters(self):
        """Logs the parameters of the training."""
        raise NotImplementedError

    @abstractmethod
    def train(self):
        """Starts a training job."""
        raise NotImplementedError

__init__

__init__()

Initializes the worker.

Source code in napari_cellseg3d/code_models/worker_training.py
101
102
103
104
105
106
107
108
109
110
111
112
113
def __init__(self):
    """Initializes the worker."""
    super().__init__(self.train)
    self._signals = LogSignal()
    self.log_signal = self._signals.log_signal
    self.warn_signal = self._signals.warn_signal
    self.error_signal = self._signals.error_signal
    self.downloader = WeightsDownloader()
    self.train_files = []
    self.val_files = []
    self.config = None

    self._weight_error = False

log

log(text)

Sends a Qt signal that the provided text should be logged.

Goes in a Log object, defined in :py:mod:napari_cellseg3d.interface. Sends a signal to the main thread to log the text. Signal is defined in napari_cellseg3d.workers_utils.LogSignal.

Parameters:

Name Type Description Default
text str

text to logged

required
Source code in napari_cellseg3d/code_models/worker_training.py
120
121
122
123
124
125
126
127
128
129
130
def log(self, text):
    """Sends a Qt signal that the provided text should be logged.

    Goes in a Log object, defined in :py:mod:`napari_cellseg3d.interface`.
    Sends a signal to the main thread to log the text.
    Signal is defined in napari_cellseg3d.workers_utils.LogSignal.

    Args:
        text (str): text to logged
    """
    self.log_signal.emit(text)

log_parameters abstractmethod

log_parameters()

Logs the parameters of the training.

Source code in napari_cellseg3d/code_models/worker_training.py
160
161
162
163
@abstractmethod
def log_parameters(self):
    """Logs the parameters of the training."""
    raise NotImplementedError

raise_error

raise_error(exception, msg)

Sends an error to main thread.

Source code in napari_cellseg3d/code_models/worker_training.py
152
153
154
155
156
157
158
def raise_error(self, exception, msg):
    """Sends an error to main thread."""
    logger.error(msg, exc_info=True)
    logger.error(exception, exc_info=True)
    self.error_signal.emit(exception, msg)
    self.errored.emit(exception)
    self.quit()

set_download_log

set_download_log(widget)

Sets the log widget for the downloader to output to.

Source code in napari_cellseg3d/code_models/worker_training.py
116
117
118
def set_download_log(self, widget):
    """Sets the log widget for the downloader to output to."""
    self.downloader.log_widget = widget

train abstractmethod

train()

Starts a training job.

Source code in napari_cellseg3d/code_models/worker_training.py
165
166
167
168
@abstractmethod
def train(self):
    """Starts a training job."""
    raise NotImplementedError

warn

warn(warning)

Sends a warning to main thread.

Source code in napari_cellseg3d/code_models/worker_training.py
132
133
134
def warn(self, warning):
    """Sends a warning to main thread."""
    self.warn_signal.emit(warning)

WNetTrainingWorker

Bases: TrainingWorkerBase

A custom worker to run WNet (unsupervised) training jobs in.

Inherits from :py:class:napari.qt.threading.GeneratorWorker via :py:class:TrainingWorkerBase.

Methods:

Name Description
__init__

Initializes the worker.

eval

Evaluates the model on the validation set.

get_dataset

Creates a Dataset applying some transforms/augmentation on the data using the MONAI library.

get_dataset_eval

Creates a Dataset applying some transforms/augmentation on the data using the MONAI library.

get_patch_dataset

Creates a Dataset from the original data using the tifffile library.

log_parameters

Logs the parameters of the training.

train

Main training function.

Source code in napari_cellseg3d/code_models/worker_training.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
class WNetTrainingWorker(TrainingWorkerBase):
    """A custom worker to run WNet (unsupervised) training jobs in.

    Inherits from :py:class:`napari.qt.threading.GeneratorWorker` via :py:class:`TrainingWorkerBase`.
    """

    # TODO : add wandb parameters

    def __init__(
        self,
        worker_config: config.WNetTrainingWorkerConfig,
    ):
        """Initializes the worker.

        Args:
            worker_config (config.WNetTrainingWorkerConfig): The configuration object
        """
        super().__init__()
        self.config = worker_config

        self.dice_metric = DiceMetric(
            include_background=False, reduction="mean", get_not_nans=False
        )
        self.normalize_function = utils.remap_image
        self.start_time = time.time()
        self.ncuts_losses = []
        self.rec_losses = []
        self.total_losses = []
        self.best_dice = -1
        self.dice_values = []

        self.dataloader: DataLoader = None
        self.eval_dataloader: DataLoader = None
        self.data_shape = None

    def get_patch_dataset(self, train_transforms):
        """Creates a Dataset from the original data using the tifffile library.

        Args:
            train_transforms (monai.transforms.Compose): The transforms to apply to the data

        Returns:
            (tuple): A tuple containing the shape of the data and the dataset
        """
        patch_func = Compose(
            [
                LoadImaged(keys=["image"], image_only=True),
                EnsureChannelFirstd(
                    keys=["image"],
                    channel_dim="no_channel",
                ),
                RandSpatialCropSamplesd(
                    keys=["image"],
                    roi_size=(
                        self.config.sample_size
                    ),  # multiply by axis_stretch_factor if anisotropy
                    # max_roi_size=(120, 120, 120),
                    random_size=False,
                    num_samples=self.config.num_samples,
                ),
                Orientationd(keys=["image"], axcodes="PLI"),
                SpatialPadd(
                    keys=["image"],
                    spatial_size=(utils.get_padding_dim(self.config.sample_size)),
                ),
                EnsureTyped(keys=["image"]),
            ]
        )
        dataset = PatchDataset(
            data=self.config.train_data_dict,
            samples_per_image=self.config.num_samples,
            patch_func=patch_func,
            transform=train_transforms,
        )

        return self.config.sample_size, dataset

    def get_dataset_eval(self, eval_dataset_dict):
        """Creates a Dataset applying some transforms/augmentation on the data using the MONAI library."""
        eval_transforms = Compose(
            [
                LoadImaged(keys=["image", "label"]),
                EnsureChannelFirstd(
                    keys=["image", "label"],
                    channel_dim="no_channel",
                ),
                # RandSpatialCropSamplesd(
                #     keys=["image", "label"],
                #     roi_size=(
                #         self.config.sample_size
                #     ),  # multiply by axis_stretch_factor if anisotropy
                #     # max_roi_size=(120, 120, 120),
                #     random_size=False,
                #     num_samples=self.config.num_samples,
                # ),
                Orientationd(keys=["image", "label"], axcodes="PLI"),
                # SpatialPadd(
                #     keys=["image", "label"],
                #     spatial_size=(
                #         utils.get_padding_dim(self.config.sample_size)
                #     ),
                # ),
                EnsureTyped(keys=["image", "label"]),
            ]
        )

        return CacheDataset(
            data=eval_dataset_dict,
            transform=eval_transforms,
        )

    def get_dataset(self, train_transforms):
        """Creates a Dataset applying some transforms/augmentation on the data using the MONAI library.

        Args:
            train_transforms (monai.transforms.Compose): The transforms to apply to the data

        Returns:
            (tuple): A tuple containing the shape of the data and the dataset
        """
        train_files = self.config.train_data_dict

        first_volume = LoadImaged(keys=["image"])(train_files[0])
        first_volume_shape = first_volume["image"].shape

        # Transforms to be applied to each volume
        load_single_images = Compose(
            [
                LoadImaged(keys=["image"]),
                EnsureChannelFirstd(
                    keys=["image"],
                    channel_dim="no_channel",
                ),
                Orientationd(keys=["image"], axcodes="PLI"),
                SpatialPadd(
                    keys=["image"],
                    spatial_size=(utils.get_padding_dim(first_volume_shape)),
                ),
                EnsureTyped(keys=["image"]),
                # RemapTensord(keys=["image"], new_min=0.0, new_max=100.0),
            ]
        )

        # Create the dataset
        dataset = CacheDataset(
            data=train_files,
            transform=Compose([load_single_images, train_transforms]),
        )

        return first_volume_shape, dataset

    def _get_data(self):
        if self.config.do_augmentation:
            train_transforms = Compose(
                [
                    # ScaleIntensityRanged(
                    #     keys=["image"],
                    #     a_min=0,
                    #     a_max=2000,
                    #     b_min=0.0,
                    #     b_max=1.0,
                    #     clip=True,
                    # ),
                    # RandShiftIntensityd(keys=["image"], offsets=0.1, prob=0.5),
                    RandFlipd(keys=["image"], spatial_axis=[1], prob=0.5),
                    RandFlipd(keys=["image"], spatial_axis=[2], prob=0.5),
                    RandRotate90d(keys=["image"], prob=0.1, max_k=3),
                    EnsureTyped(keys=["image"]),
                ]
            )
        else:
            train_transforms = EnsureTyped(keys=["image"])

        if self.config.sampling:
            logger.debug("Loading patch dataset")
            (self.data_shape, dataset) = self.get_patch_dataset(train_transforms)
        else:
            logger.debug("Loading volume dataset")
            (self.data_shape, dataset) = self.get_dataset(train_transforms)

        logger.debug(f"Data shape : {self.data_shape}")
        self.dataloader = self._make_dataloader(
            dataset,
            self.config.batch_size,
            shuffle=True,
            collate_fn=pad_list_data_collate,
        )

        if self.config.eval_volume_dict is not None:
            eval_dataset = self.get_dataset_eval(self.config.eval_volume_dict)
            logger.debug(f"Eval batch size : {self.config.eval_batch_size}")
            self.eval_dataloader = self._make_dataloader(
                eval_dataset,
                self.config.eval_batch_size,
                collate_fn=pad_list_data_collate,
            )
        else:
            self.eval_dataloader = None
        return self.dataloader, self.eval_dataloader, self.data_shape

    def log_parameters(self):
        """Logs the parameters of the training."""
        self.log("*" * 20)
        self.log("-- Parameters --")
        self.log(f"Device: {self.config.device}")
        self.log(f"Batch size: {self.config.batch_size}")
        self.log(f"Epochs: {self.config.max_epochs}")
        self.log(f"Learning rate: {self.config.learning_rate}")
        self.log(f"Validation interval: {self.config.validation_interval}")
        if self.config.weights_info.use_custom:
            self.log(f"Custom weights: {self.config.weights_info.path}")
        elif self.config.weights_info.use_pretrained:
            self.log(f"Pretrained weights: {self.config.weights_info.path}")
        if self.config.sampling:
            self.log(
                f"Using {self.config.num_samples} samples of size {self.config.sample_size}"
            )
        if self.config.do_augmentation:
            self.log("Using data augmentation")
        ##############
        self.log("-- Model --")
        self.log(f"Using {self.config.num_classes} classes")
        self.log(f"Weight decay: {self.config.weight_decay}")
        self.log("* NCuts : ")
        self.log(f"- Intensity sigma {self.config.intensity_sigma}")
        self.log(f"- Spatial sigma {self.config.spatial_sigma}")
        self.log(f"- Radius : {self.config.radius}")
        self.log(f"* Reconstruction loss : {self.config.reconstruction_loss}")
        self.log(
            f"Weighted sum : {self.config.n_cuts_weight}*NCuts + {self.config.rec_loss_weight}*Reconstruction"
        )
        ##############
        self.log("-- Data --")
        self.log("Training data :\n")
        [
            self.log(f"{Path(v).stem}")
            for d in self.config.train_data_dict
            for k, v in d.items()
        ]
        if self.config.eval_volume_dict is not None:
            self.log("\nValidation data :\n")
            [
                self.log(f"{k}: {v}")
                for d in self.config.eval_volume_dict
                for k, v in d.items()
            ]
        self.log("*" * 20)

    def train(self, provided_model=None, provided_optimizer=None, provided_loss=None):
        """Main training function.

        Note : args are mainly used for testing purposes. Model is otherwise initialized in the function.

        Args:
            provided_model (WNet, optional): A model to use for training. Defaults to None.
            provided_optimizer (torch.optim.Optimizer, optional): An optimizer to use for training. Defaults to None.
            provided_loss (torch.nn.Module, optional): A loss function to use for training. Defaults to None.
        """
        try:
            if self.config is None:
                self.config = config.WNetTrainingWorkerConfig()
            ##############
            # disable metadata tracking in MONAI
            set_track_meta(False)
            ##############
            if WANDB_INSTALLED:
                config_dict = self.config.__dict__
                logger.debug(f"wandb config : {config_dict}")
                if wandb.run is not None:
                    logger.warning(
                        "A previous wandb run is still active. It will be stopped before starting a new one."
                    )
                    wandb.finish()
                wandb.init(
                    config=config_dict,
                    project="CellSeg3D - WNet3D",
                    name=f"WNet3D_training - {utils.get_date_time()}",
                    mode=self.wandb_config.mode,
                    tags=["WNet3D", "training"],
                )

            set_determinism(seed=self.config.deterministic_config.seed)
            torch.use_deterministic_algorithms(True, warn_only=True)

            device = self.config.device

            self.log_parameters()
            self.log("Initializing training...")
            self.log("- Getting the data")

            self._get_data()

            ###################################################
            #               Training the model                #
            ###################################################
            self.log("- Getting the model")
            # Initialize the model
            model = (
                WNet(
                    in_channels=self.config.in_channels,
                    out_channels=self.config.out_channels,
                    num_classes=self.config.num_classes,
                    dropout=self.config.dropout,
                )
                if provided_model is None
                else provided_model
            )
            model.to(device)

            if self.config.use_clipping:
                for p in model.parameters():
                    p.register_hook(
                        lambda grad: torch.clamp(
                            grad,
                            min=-self.config.clipping,
                            max=self.config.clipping,
                        )
                    )

            if WANDB_INSTALLED:
                wandb.watch(model, log_freq=100)

            if (
                self.config.weights_info.use_pretrained
                or self.config.weights_info.use_custom
            ):
                if self.config.weights_info.use_pretrained:
                    from napari_cellseg3d.code_models.models.model_WNet import (
                        WNet_,
                    )

                    weights_file = WNet_.weights_file
                    weights = str(
                        self.downloader.download_weights("WNet3D", weights_file)
                    )
                    self.config.weights_info.path = weights

                if self.config.weights_info.use_custom:
                    weights = str(Path(self.config.weights_info.path))

                try:
                    model.load_state_dict(
                        torch.load(
                            weights,
                            map_location=self.config.device,
                        ),
                        strict=True,
                    )
                except RuntimeError as e:
                    logger.error(f"Error when loading weights : {e}")
                    logger.exception(e)
                    warn = (
                        "WARNING:\nIt'd seem that the weights were incompatible with the model,\n"
                        "the model will be trained from random weights"
                    )
                    self.log(warn)
                    self.warn(warn)
                    self._weight_error = True
            else:
                self.log("Model will be trained from scratch")
            self.log("- Getting the optimizer")
            # Initialize the optimizers
            if self.config.weight_decay is not None:
                decay = self.config.weight_decay
                optimizer = torch.optim.Adam(
                    model.parameters(),
                    lr=self.config.learning_rate,
                    weight_decay=decay,
                )
            else:
                optimizer = torch.optim.Adam(
                    model.parameters(), lr=self.config.learning_rate
                )
            if provided_optimizer is not None:
                optimizer = provided_optimizer
            self.log("- Getting the loss functions")
            # Initialize the Ncuts loss function
            criterionE = SoftNCutsLoss(
                data_shape=self.data_shape,
                device=device,
                intensity_sigma=self.config.intensity_sigma,
                spatial_sigma=self.config.spatial_sigma,
                radius=self.config.radius,
            )

            if self.config.reconstruction_loss == "MSE":
                criterionW = nn.MSELoss()
            elif self.config.reconstruction_loss == "BCE":
                criterionW = nn.BCELoss()
            else:
                raise ValueError(
                    f"Unknown reconstruction loss : {self.config.reconstruction_loss} not supported"
                )

            model.train()

            self.log("Ready")
            self.log("Training the model")
            self.log("*" * 20)

            # Train the model
            for epoch in range(self.config.max_epochs):
                self.log(f"Epoch {epoch + 1} of {self.config.max_epochs}")

                epoch_ncuts_loss = 0
                epoch_rec_loss = 0
                epoch_loss = 0

                for _i, batch in enumerate(self.dataloader):
                    # raise NotImplementedError("testing")
                    image_batch = batch["image"].to(device)
                    # Normalize the image
                    for i in range(image_batch.shape[0]):
                        for j in range(image_batch.shape[1]):
                            image_batch[i, j] = self.normalize_function(
                                image_batch[i, j]
                            )

                    # Forward pass
                    enc, dec = model(image_batch)
                    # Compute the Ncuts loss
                    Ncuts = criterionE(enc, image_batch)

                    epoch_ncuts_loss += Ncuts.item()
                    if WANDB_INSTALLED:
                        wandb.log({"Train/Ncuts loss": Ncuts.item()})

                    # Compute the reconstruction loss
                    if isinstance(criterionW, nn.MSELoss):
                        reconstruction_loss = criterionW(dec, image_batch)
                    elif isinstance(criterionW, nn.BCELoss):
                        reconstruction_loss = criterionW(
                            torch.sigmoid(dec),
                            utils.remap_image(image_batch, new_max=1),
                        )

                    epoch_rec_loss += reconstruction_loss.item()
                    if WANDB_INSTALLED:
                        wandb.log(
                            {"Train/Reconstruction loss": reconstruction_loss.item()}
                        )

                    # Backward pass for the reconstruction loss
                    optimizer.zero_grad()
                    alpha = self.config.n_cuts_weight
                    beta = self.config.rec_loss_weight

                    loss = alpha * Ncuts + beta * reconstruction_loss
                    if provided_loss is not None:
                        loss = provided_loss
                    epoch_loss += loss.item()

                    if WANDB_INSTALLED:
                        wandb.log({"Train/Weighted sum of losses": loss.item()})

                    loss.backward(loss)
                    optimizer.step()

                    yield TrainingReport(
                        show_plot=False,
                        weights=model.state_dict(),
                        supervised=False,
                    )

                    if self._abort_requested:
                        self.dataloader = None
                        del self.dataloader
                        self.eval_dataloader = None
                        del self.eval_dataloader
                        model = None
                        del model
                        optimizer = None
                        del optimizer
                        criterionE = None
                        del criterionE
                        criterionW = None
                        del criterionW
                        torch.cuda.empty_cache()

                        if WANDB_INSTALLED:
                            wandb.finish()

                self.ncuts_losses.append(epoch_ncuts_loss / len(self.dataloader))
                self.rec_losses.append(epoch_rec_loss / len(self.dataloader))
                self.total_losses.append(epoch_loss / len(self.dataloader))

                if self.eval_dataloader is None:
                    try:
                        enc_out = enc[0].detach().cpu().numpy()
                        dec_out = dec[0].detach().cpu().numpy()
                        image_batch = image_batch[0].detach().cpu().numpy()

                        images_dict = {
                            "Encoder output": {
                                "data": enc_out,
                                "cmap": "turbo",
                            },
                            "Encoder output (discrete)": {
                                "data": np.where(enc_out > 0.5, enc_out, 0),
                                "cmap": "bop blue",
                            },
                            "Decoder output": {
                                "data": np.squeeze(dec_out),
                                "cmap": "gist_earth",
                            },
                            "Input image": {
                                "data": np.squeeze(image_batch),
                                "cmap": "inferno",
                            },
                        }

                        yield TrainingReport(
                            show_plot=True,
                            epoch=epoch,
                            loss_1_values={"SoftNCuts": self.ncuts_losses},
                            loss_2_values=self.rec_losses,
                            weights=model.state_dict(),
                            images_dict=images_dict,
                            supervised=False,
                        )
                    except TypeError:
                        pass

                if WANDB_INSTALLED:
                    wandb.log({"Ncuts loss for epoch": self.ncuts_losses[-1]})
                    wandb.log({"Reconstruction loss for epoch": self.rec_losses[-1]})
                    wandb.log({"Sum of losses for epoch": self.total_losses[-1]})
                    wandb.log(
                        {"LR/Model learning rate": optimizer.param_groups[0]["lr"]}
                    )

                self.log(f"Ncuts loss: {self.ncuts_losses[-1]:.5f}")
                self.log(f"Reconstruction loss: {self.rec_losses[-1]:.5f}")
                self.log(f"Weighted sum of losses: {self.total_losses[-1]:.5f}")
                if epoch > 0:
                    self.log(
                        f"Ncuts loss difference: {self.ncuts_losses[-1] - self.ncuts_losses[-2]:.5f}"
                    )
                    self.log(
                        f"Reconstruction loss difference: {self.rec_losses[-1] - self.rec_losses[-2]:.5f}"
                    )
                    self.log(
                        f"Weighted sum of losses difference: {self.total_losses[-1] - self.total_losses[-2]:.5f}"
                    )

                if (
                    self.eval_dataloader is not None
                    and (epoch + 1) % self.config.validation_interval == 0
                ):
                    model.eval()
                    self.log("Validating...")
                    yield self.eval(model, epoch)  # validation

                    if self._abort_requested:
                        self.dataloader = None
                        del self.dataloader
                        self.eval_dataloader = None
                        del self.eval_dataloader
                        model = None
                        del model
                        optimizer = None
                        del optimizer
                        criterionE = None
                        del criterionE
                        criterionW = None
                        del criterionW
                        torch.cuda.empty_cache()

                eta = (
                    (time.time() - self.start_time)
                    * (self.config.max_epochs / (epoch + 1) - 1)
                    / 60
                )
                self.log(f"ETA: {eta:.1f} minutes")
                self.log("-" * 20)

                # Save the model
                if epoch % 5 == 0:
                    torch.save(
                        model.state_dict(),
                        self.config.results_path_folder + "/wnet_checkpoint.pth",
                    )

            self.log("Training finished")
            if self.best_dice > -1:
                best_dice_epoch = epoch
                self.log(
                    f"Best dice metric : {self.best_dice} at epoch {best_dice_epoch}"
                )

                if WANDB_INSTALLED:
                    wandb.log(
                        {
                            "Validation/Best Dice": self.best_dice,
                            "Validation/Best Dice epoch": best_dice_epoch,
                        }
                    )

            # Save the model
            self.log(
                f"Saving the model to: {self.config.results_path_folder}/wnet.pth",
            )
            save_weights_path = self.config.results_path_folder + "/wnet.pth"
            torch.save(
                model.state_dict(),
                save_weights_path,
            )

            if WANDB_INSTALLED and self.wandb_config.save_model_artifact:
                model_artifact = wandb.Artifact(
                    "WNet3D",
                    type="model",
                    description="CellSeg3D WNet",
                    metadata=self.config.__dict__,
                )
                model_artifact.add_file(save_weights_path)
                wandb.log_artifact(model_artifact)

            dataloader = None
            del dataloader
            self.eval_dataloader = None
            del self.eval_dataloader
            model = None
            del model
            optimizer = None
            del optimizer
            criterionE = None
            del criterionE
            criterionW = None
            del criterionW
            torch.cuda.empty_cache()
            if WANDB_INSTALLED:
                wandb.finish()
        except Exception as e:
            if WANDB_INSTALLED:
                wandb.finish()
            msg = f"Training failed with exception: {e}"
            self.log(msg)
            self.raise_error(e, msg)
            self.quit()
            raise e

    def eval(self, model, epoch) -> TrainingReport:
        """Evaluates the model on the validation set.

        Args:
            model (WNet): The model to evaluate
            epoch (int): The current epoch

        Returns:
            TrainingReport: A training report containing the results of the evaluation. See :py:class:`napari_cellseg3d.workers_utils.TrainingReport`
        """
        with torch.no_grad():
            device = self.config.device
            for _k, val_data in enumerate(self.eval_dataloader):
                val_inputs, val_labels = (
                    val_data["image"].to(device),
                    val_data["label"].to(device),
                )

                # normalize val_inputs across channels
                for i in range(val_inputs.shape[0]):
                    for j in range(val_inputs.shape[1]):
                        val_inputs[i][j] = self.normalize_function(val_inputs[i][j])
                logger.debug(f"Val inputs shape: {val_inputs.shape}")
                val_outputs = sliding_window_inference(
                    val_inputs,
                    roi_size=[64, 64, 64],
                    sw_batch_size=1,
                    predictor=model.forward_encoder,
                    overlap=0.1,
                    mode="gaussian",
                    sigma_scale=0.01,
                    progress=True,
                )
                val_decoder_outputs = sliding_window_inference(
                    val_outputs,
                    roi_size=[64, 64, 64],
                    sw_batch_size=1,
                    predictor=model.forward_decoder,
                    overlap=0.1,
                    mode="gaussian",
                    sigma_scale=0.01,
                    progress=True,
                )
                val_outputs = AsDiscrete(threshold=0.5)(val_outputs)
                logger.debug(f"Val outputs shape: {val_outputs.shape}")
                logger.debug(f"Val labels shape: {val_labels.shape}")
                logger.debug(f"Val decoder outputs shape: {val_decoder_outputs.shape}")

                max_dice_channel = utils.seek_best_dice_coeff_channel(
                    y_pred=val_outputs, y_true=val_labels
                )
                self.dice_metric(
                    y_pred=val_outputs[
                        :,
                        max_dice_channel:,  # : (max_dice_channel + 1),
                        :,
                        :,
                    ],
                    y=val_labels,
                )

            # aggregate the final mean dice result
            metric = self.dice_metric.aggregate().item()
            self.dice_values.append(metric)
            self.log(f"Validation Dice score: {metric:.3f}")
            if self.best_dice < metric <= 1:
                self.best_dice = metric
                # save the best model
                save_best_path = self.config.results_path_folder
                # save_best_path.mkdir(parents=True, exist_ok=True)
                save_best_name = "wnet"
                save_path = (
                    str(Path(save_best_path) / save_best_name) + "_best_metric.pth"
                )
                self.log(f"Saving new best model to {save_path}")
                torch.save(model.state_dict(), save_path)

            if WANDB_INSTALLED:
                # log validation dice score for each validation round
                wandb.log({"Validation/Dice metric": metric})

            self.dice_metric.reset()
            dec_out_val = val_decoder_outputs[0].detach().cpu().numpy().copy()
            enc_out_val = val_outputs[0].detach().cpu().numpy().copy()
            lab_out_val = val_labels[0].detach().cpu().numpy().copy()
            val_in = val_inputs[0].detach().cpu().numpy().copy()

            display_dict = {
                "Reconstruction": {
                    "data": np.squeeze(dec_out_val),
                    "cmap": "gist_earth",
                },
                "Segmentation": {
                    "data": np.squeeze(enc_out_val),
                    "cmap": "turbo",
                },
                "Inputs": {
                    "data": np.squeeze(val_in),
                    "cmap": "inferno",
                },
                "Labels": {
                    "data": np.squeeze(lab_out_val),
                    "cmap": "bop blue",
                },
            }
            val_decoder_outputs = None
            del val_decoder_outputs
            val_outputs = None
            del val_outputs
            val_labels = None
            del val_labels
            val_inputs = None
            del val_inputs

            return TrainingReport(
                epoch=epoch,
                loss_1_values={
                    "SoftNCuts": self.ncuts_losses,
                    "Dice metric": self.dice_values,
                },
                loss_2_values=self.rec_losses,
                weights=model.state_dict(),
                images_dict=display_dict,
                supervised=False,
            )

__init__

__init__(worker_config: WNetTrainingWorkerConfig)

Initializes the worker.

Parameters:

Name Type Description Default
worker_config WNetTrainingWorkerConfig

The configuration object

required
Source code in napari_cellseg3d/code_models/worker_training.py
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
def __init__(
    self,
    worker_config: config.WNetTrainingWorkerConfig,
):
    """Initializes the worker.

    Args:
        worker_config (config.WNetTrainingWorkerConfig): The configuration object
    """
    super().__init__()
    self.config = worker_config

    self.dice_metric = DiceMetric(
        include_background=False, reduction="mean", get_not_nans=False
    )
    self.normalize_function = utils.remap_image
    self.start_time = time.time()
    self.ncuts_losses = []
    self.rec_losses = []
    self.total_losses = []
    self.best_dice = -1
    self.dice_values = []

    self.dataloader: DataLoader = None
    self.eval_dataloader: DataLoader = None
    self.data_shape = None

eval

eval(model, epoch) -> TrainingReport

Evaluates the model on the validation set.

Parameters:

Name Type Description Default
model WNet

The model to evaluate

required
epoch int

The current epoch

required

Returns:

Name Type Description
TrainingReport TrainingReport

A training report containing the results of the evaluation. See :py:class:napari_cellseg3d.workers_utils.TrainingReport

Source code in napari_cellseg3d/code_models/worker_training.py
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
def eval(self, model, epoch) -> TrainingReport:
    """Evaluates the model on the validation set.

    Args:
        model (WNet): The model to evaluate
        epoch (int): The current epoch

    Returns:
        TrainingReport: A training report containing the results of the evaluation. See :py:class:`napari_cellseg3d.workers_utils.TrainingReport`
    """
    with torch.no_grad():
        device = self.config.device
        for _k, val_data in enumerate(self.eval_dataloader):
            val_inputs, val_labels = (
                val_data["image"].to(device),
                val_data["label"].to(device),
            )

            # normalize val_inputs across channels
            for i in range(val_inputs.shape[0]):
                for j in range(val_inputs.shape[1]):
                    val_inputs[i][j] = self.normalize_function(val_inputs[i][j])
            logger.debug(f"Val inputs shape: {val_inputs.shape}")
            val_outputs = sliding_window_inference(
                val_inputs,
                roi_size=[64, 64, 64],
                sw_batch_size=1,
                predictor=model.forward_encoder,
                overlap=0.1,
                mode="gaussian",
                sigma_scale=0.01,
                progress=True,
            )
            val_decoder_outputs = sliding_window_inference(
                val_outputs,
                roi_size=[64, 64, 64],
                sw_batch_size=1,
                predictor=model.forward_decoder,
                overlap=0.1,
                mode="gaussian",
                sigma_scale=0.01,
                progress=True,
            )
            val_outputs = AsDiscrete(threshold=0.5)(val_outputs)
            logger.debug(f"Val outputs shape: {val_outputs.shape}")
            logger.debug(f"Val labels shape: {val_labels.shape}")
            logger.debug(f"Val decoder outputs shape: {val_decoder_outputs.shape}")

            max_dice_channel = utils.seek_best_dice_coeff_channel(
                y_pred=val_outputs, y_true=val_labels
            )
            self.dice_metric(
                y_pred=val_outputs[
                    :,
                    max_dice_channel:,  # : (max_dice_channel + 1),
                    :,
                    :,
                ],
                y=val_labels,
            )

        # aggregate the final mean dice result
        metric = self.dice_metric.aggregate().item()
        self.dice_values.append(metric)
        self.log(f"Validation Dice score: {metric:.3f}")
        if self.best_dice < metric <= 1:
            self.best_dice = metric
            # save the best model
            save_best_path = self.config.results_path_folder
            # save_best_path.mkdir(parents=True, exist_ok=True)
            save_best_name = "wnet"
            save_path = (
                str(Path(save_best_path) / save_best_name) + "_best_metric.pth"
            )
            self.log(f"Saving new best model to {save_path}")
            torch.save(model.state_dict(), save_path)

        if WANDB_INSTALLED:
            # log validation dice score for each validation round
            wandb.log({"Validation/Dice metric": metric})

        self.dice_metric.reset()
        dec_out_val = val_decoder_outputs[0].detach().cpu().numpy().copy()
        enc_out_val = val_outputs[0].detach().cpu().numpy().copy()
        lab_out_val = val_labels[0].detach().cpu().numpy().copy()
        val_in = val_inputs[0].detach().cpu().numpy().copy()

        display_dict = {
            "Reconstruction": {
                "data": np.squeeze(dec_out_val),
                "cmap": "gist_earth",
            },
            "Segmentation": {
                "data": np.squeeze(enc_out_val),
                "cmap": "turbo",
            },
            "Inputs": {
                "data": np.squeeze(val_in),
                "cmap": "inferno",
            },
            "Labels": {
                "data": np.squeeze(lab_out_val),
                "cmap": "bop blue",
            },
        }
        val_decoder_outputs = None
        del val_decoder_outputs
        val_outputs = None
        del val_outputs
        val_labels = None
        del val_labels
        val_inputs = None
        del val_inputs

        return TrainingReport(
            epoch=epoch,
            loss_1_values={
                "SoftNCuts": self.ncuts_losses,
                "Dice metric": self.dice_values,
            },
            loss_2_values=self.rec_losses,
            weights=model.state_dict(),
            images_dict=display_dict,
            supervised=False,
        )

get_dataset

get_dataset(train_transforms)

Creates a Dataset applying some transforms/augmentation on the data using the MONAI library.

Parameters:

Name Type Description Default
train_transforms Compose

The transforms to apply to the data

required

Returns:

Type Description
tuple

A tuple containing the shape of the data and the dataset

Source code in napari_cellseg3d/code_models/worker_training.py
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
def get_dataset(self, train_transforms):
    """Creates a Dataset applying some transforms/augmentation on the data using the MONAI library.

    Args:
        train_transforms (monai.transforms.Compose): The transforms to apply to the data

    Returns:
        (tuple): A tuple containing the shape of the data and the dataset
    """
    train_files = self.config.train_data_dict

    first_volume = LoadImaged(keys=["image"])(train_files[0])
    first_volume_shape = first_volume["image"].shape

    # Transforms to be applied to each volume
    load_single_images = Compose(
        [
            LoadImaged(keys=["image"]),
            EnsureChannelFirstd(
                keys=["image"],
                channel_dim="no_channel",
            ),
            Orientationd(keys=["image"], axcodes="PLI"),
            SpatialPadd(
                keys=["image"],
                spatial_size=(utils.get_padding_dim(first_volume_shape)),
            ),
            EnsureTyped(keys=["image"]),
            # RemapTensord(keys=["image"], new_min=0.0, new_max=100.0),
        ]
    )

    # Create the dataset
    dataset = CacheDataset(
        data=train_files,
        transform=Compose([load_single_images, train_transforms]),
    )

    return first_volume_shape, dataset

get_dataset_eval

get_dataset_eval(eval_dataset_dict)

Creates a Dataset applying some transforms/augmentation on the data using the MONAI library.

Source code in napari_cellseg3d/code_models/worker_training.py
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
def get_dataset_eval(self, eval_dataset_dict):
    """Creates a Dataset applying some transforms/augmentation on the data using the MONAI library."""
    eval_transforms = Compose(
        [
            LoadImaged(keys=["image", "label"]),
            EnsureChannelFirstd(
                keys=["image", "label"],
                channel_dim="no_channel",
            ),
            # RandSpatialCropSamplesd(
            #     keys=["image", "label"],
            #     roi_size=(
            #         self.config.sample_size
            #     ),  # multiply by axis_stretch_factor if anisotropy
            #     # max_roi_size=(120, 120, 120),
            #     random_size=False,
            #     num_samples=self.config.num_samples,
            # ),
            Orientationd(keys=["image", "label"], axcodes="PLI"),
            # SpatialPadd(
            #     keys=["image", "label"],
            #     spatial_size=(
            #         utils.get_padding_dim(self.config.sample_size)
            #     ),
            # ),
            EnsureTyped(keys=["image", "label"]),
        ]
    )

    return CacheDataset(
        data=eval_dataset_dict,
        transform=eval_transforms,
    )

get_patch_dataset

get_patch_dataset(train_transforms)

Creates a Dataset from the original data using the tifffile library.

Parameters:

Name Type Description Default
train_transforms Compose

The transforms to apply to the data

required

Returns:

Type Description
tuple

A tuple containing the shape of the data and the dataset

Source code in napari_cellseg3d/code_models/worker_training.py
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
def get_patch_dataset(self, train_transforms):
    """Creates a Dataset from the original data using the tifffile library.

    Args:
        train_transforms (monai.transforms.Compose): The transforms to apply to the data

    Returns:
        (tuple): A tuple containing the shape of the data and the dataset
    """
    patch_func = Compose(
        [
            LoadImaged(keys=["image"], image_only=True),
            EnsureChannelFirstd(
                keys=["image"],
                channel_dim="no_channel",
            ),
            RandSpatialCropSamplesd(
                keys=["image"],
                roi_size=(
                    self.config.sample_size
                ),  # multiply by axis_stretch_factor if anisotropy
                # max_roi_size=(120, 120, 120),
                random_size=False,
                num_samples=self.config.num_samples,
            ),
            Orientationd(keys=["image"], axcodes="PLI"),
            SpatialPadd(
                keys=["image"],
                spatial_size=(utils.get_padding_dim(self.config.sample_size)),
            ),
            EnsureTyped(keys=["image"]),
        ]
    )
    dataset = PatchDataset(
        data=self.config.train_data_dict,
        samples_per_image=self.config.num_samples,
        patch_func=patch_func,
        transform=train_transforms,
    )

    return self.config.sample_size, dataset

log_parameters

log_parameters()

Logs the parameters of the training.

Source code in napari_cellseg3d/code_models/worker_training.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def log_parameters(self):
    """Logs the parameters of the training."""
    self.log("*" * 20)
    self.log("-- Parameters --")
    self.log(f"Device: {self.config.device}")
    self.log(f"Batch size: {self.config.batch_size}")
    self.log(f"Epochs: {self.config.max_epochs}")
    self.log(f"Learning rate: {self.config.learning_rate}")
    self.log(f"Validation interval: {self.config.validation_interval}")
    if self.config.weights_info.use_custom:
        self.log(f"Custom weights: {self.config.weights_info.path}")
    elif self.config.weights_info.use_pretrained:
        self.log(f"Pretrained weights: {self.config.weights_info.path}")
    if self.config.sampling:
        self.log(
            f"Using {self.config.num_samples} samples of size {self.config.sample_size}"
        )
    if self.config.do_augmentation:
        self.log("Using data augmentation")
    ##############
    self.log("-- Model --")
    self.log(f"Using {self.config.num_classes} classes")
    self.log(f"Weight decay: {self.config.weight_decay}")
    self.log("* NCuts : ")
    self.log(f"- Intensity sigma {self.config.intensity_sigma}")
    self.log(f"- Spatial sigma {self.config.spatial_sigma}")
    self.log(f"- Radius : {self.config.radius}")
    self.log(f"* Reconstruction loss : {self.config.reconstruction_loss}")
    self.log(
        f"Weighted sum : {self.config.n_cuts_weight}*NCuts + {self.config.rec_loss_weight}*Reconstruction"
    )
    ##############
    self.log("-- Data --")
    self.log("Training data :\n")
    [
        self.log(f"{Path(v).stem}")
        for d in self.config.train_data_dict
        for k, v in d.items()
    ]
    if self.config.eval_volume_dict is not None:
        self.log("\nValidation data :\n")
        [
            self.log(f"{k}: {v}")
            for d in self.config.eval_volume_dict
            for k, v in d.items()
        ]
    self.log("*" * 20)

train

train(provided_model=None, provided_optimizer=None, provided_loss=None)

Main training function.

Note : args are mainly used for testing purposes. Model is otherwise initialized in the function.

Parameters:

Name Type Description Default
provided_model WNet

A model to use for training. Defaults to None.

None
provided_optimizer Optimizer

An optimizer to use for training. Defaults to None.

None
provided_loss Module

A loss function to use for training. Defaults to None.

None
Source code in napari_cellseg3d/code_models/worker_training.py
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
def train(self, provided_model=None, provided_optimizer=None, provided_loss=None):
    """Main training function.

    Note : args are mainly used for testing purposes. Model is otherwise initialized in the function.

    Args:
        provided_model (WNet, optional): A model to use for training. Defaults to None.
        provided_optimizer (torch.optim.Optimizer, optional): An optimizer to use for training. Defaults to None.
        provided_loss (torch.nn.Module, optional): A loss function to use for training. Defaults to None.
    """
    try:
        if self.config is None:
            self.config = config.WNetTrainingWorkerConfig()
        ##############
        # disable metadata tracking in MONAI
        set_track_meta(False)
        ##############
        if WANDB_INSTALLED:
            config_dict = self.config.__dict__
            logger.debug(f"wandb config : {config_dict}")
            if wandb.run is not None:
                logger.warning(
                    "A previous wandb run is still active. It will be stopped before starting a new one."
                )
                wandb.finish()
            wandb.init(
                config=config_dict,
                project="CellSeg3D - WNet3D",
                name=f"WNet3D_training - {utils.get_date_time()}",
                mode=self.wandb_config.mode,
                tags=["WNet3D", "training"],
            )

        set_determinism(seed=self.config.deterministic_config.seed)
        torch.use_deterministic_algorithms(True, warn_only=True)

        device = self.config.device

        self.log_parameters()
        self.log("Initializing training...")
        self.log("- Getting the data")

        self._get_data()

        ###################################################
        #               Training the model                #
        ###################################################
        self.log("- Getting the model")
        # Initialize the model
        model = (
            WNet(
                in_channels=self.config.in_channels,
                out_channels=self.config.out_channels,
                num_classes=self.config.num_classes,
                dropout=self.config.dropout,
            )
            if provided_model is None
            else provided_model
        )
        model.to(device)

        if self.config.use_clipping:
            for p in model.parameters():
                p.register_hook(
                    lambda grad: torch.clamp(
                        grad,
                        min=-self.config.clipping,
                        max=self.config.clipping,
                    )
                )

        if WANDB_INSTALLED:
            wandb.watch(model, log_freq=100)

        if (
            self.config.weights_info.use_pretrained
            or self.config.weights_info.use_custom
        ):
            if self.config.weights_info.use_pretrained:
                from napari_cellseg3d.code_models.models.model_WNet import (
                    WNet_,
                )

                weights_file = WNet_.weights_file
                weights = str(
                    self.downloader.download_weights("WNet3D", weights_file)
                )
                self.config.weights_info.path = weights

            if self.config.weights_info.use_custom:
                weights = str(Path(self.config.weights_info.path))

            try:
                model.load_state_dict(
                    torch.load(
                        weights,
                        map_location=self.config.device,
                    ),
                    strict=True,
                )
            except RuntimeError as e:
                logger.error(f"Error when loading weights : {e}")
                logger.exception(e)
                warn = (
                    "WARNING:\nIt'd seem that the weights were incompatible with the model,\n"
                    "the model will be trained from random weights"
                )
                self.log(warn)
                self.warn(warn)
                self._weight_error = True
        else:
            self.log("Model will be trained from scratch")
        self.log("- Getting the optimizer")
        # Initialize the optimizers
        if self.config.weight_decay is not None:
            decay = self.config.weight_decay
            optimizer = torch.optim.Adam(
                model.parameters(),
                lr=self.config.learning_rate,
                weight_decay=decay,
            )
        else:
            optimizer = torch.optim.Adam(
                model.parameters(), lr=self.config.learning_rate
            )
        if provided_optimizer is not None:
            optimizer = provided_optimizer
        self.log("- Getting the loss functions")
        # Initialize the Ncuts loss function
        criterionE = SoftNCutsLoss(
            data_shape=self.data_shape,
            device=device,
            intensity_sigma=self.config.intensity_sigma,
            spatial_sigma=self.config.spatial_sigma,
            radius=self.config.radius,
        )

        if self.config.reconstruction_loss == "MSE":
            criterionW = nn.MSELoss()
        elif self.config.reconstruction_loss == "BCE":
            criterionW = nn.BCELoss()
        else:
            raise ValueError(
                f"Unknown reconstruction loss : {self.config.reconstruction_loss} not supported"
            )

        model.train()

        self.log("Ready")
        self.log("Training the model")
        self.log("*" * 20)

        # Train the model
        for epoch in range(self.config.max_epochs):
            self.log(f"Epoch {epoch + 1} of {self.config.max_epochs}")

            epoch_ncuts_loss = 0
            epoch_rec_loss = 0
            epoch_loss = 0

            for _i, batch in enumerate(self.dataloader):
                # raise NotImplementedError("testing")
                image_batch = batch["image"].to(device)
                # Normalize the image
                for i in range(image_batch.shape[0]):
                    for j in range(image_batch.shape[1]):
                        image_batch[i, j] = self.normalize_function(
                            image_batch[i, j]
                        )

                # Forward pass
                enc, dec = model(image_batch)
                # Compute the Ncuts loss
                Ncuts = criterionE(enc, image_batch)

                epoch_ncuts_loss += Ncuts.item()
                if WANDB_INSTALLED:
                    wandb.log({"Train/Ncuts loss": Ncuts.item()})

                # Compute the reconstruction loss
                if isinstance(criterionW, nn.MSELoss):
                    reconstruction_loss = criterionW(dec, image_batch)
                elif isinstance(criterionW, nn.BCELoss):
                    reconstruction_loss = criterionW(
                        torch.sigmoid(dec),
                        utils.remap_image(image_batch, new_max=1),
                    )

                epoch_rec_loss += reconstruction_loss.item()
                if WANDB_INSTALLED:
                    wandb.log(
                        {"Train/Reconstruction loss": reconstruction_loss.item()}
                    )

                # Backward pass for the reconstruction loss
                optimizer.zero_grad()
                alpha = self.config.n_cuts_weight
                beta = self.config.rec_loss_weight

                loss = alpha * Ncuts + beta * reconstruction_loss
                if provided_loss is not None:
                    loss = provided_loss
                epoch_loss += loss.item()

                if WANDB_INSTALLED:
                    wandb.log({"Train/Weighted sum of losses": loss.item()})

                loss.backward(loss)
                optimizer.step()

                yield TrainingReport(
                    show_plot=False,
                    weights=model.state_dict(),
                    supervised=False,
                )

                if self._abort_requested:
                    self.dataloader = None
                    del self.dataloader
                    self.eval_dataloader = None
                    del self.eval_dataloader
                    model = None
                    del model
                    optimizer = None
                    del optimizer
                    criterionE = None
                    del criterionE
                    criterionW = None
                    del criterionW
                    torch.cuda.empty_cache()

                    if WANDB_INSTALLED:
                        wandb.finish()

            self.ncuts_losses.append(epoch_ncuts_loss / len(self.dataloader))
            self.rec_losses.append(epoch_rec_loss / len(self.dataloader))
            self.total_losses.append(epoch_loss / len(self.dataloader))

            if self.eval_dataloader is None:
                try:
                    enc_out = enc[0].detach().cpu().numpy()
                    dec_out = dec[0].detach().cpu().numpy()
                    image_batch = image_batch[0].detach().cpu().numpy()

                    images_dict = {
                        "Encoder output": {
                            "data": enc_out,
                            "cmap": "turbo",
                        },
                        "Encoder output (discrete)": {
                            "data": np.where(enc_out > 0.5, enc_out, 0),
                            "cmap": "bop blue",
                        },
                        "Decoder output": {
                            "data": np.squeeze(dec_out),
                            "cmap": "gist_earth",
                        },
                        "Input image": {
                            "data": np.squeeze(image_batch),
                            "cmap": "inferno",
                        },
                    }

                    yield TrainingReport(
                        show_plot=True,
                        epoch=epoch,
                        loss_1_values={"SoftNCuts": self.ncuts_losses},
                        loss_2_values=self.rec_losses,
                        weights=model.state_dict(),
                        images_dict=images_dict,
                        supervised=False,
                    )
                except TypeError:
                    pass

            if WANDB_INSTALLED:
                wandb.log({"Ncuts loss for epoch": self.ncuts_losses[-1]})
                wandb.log({"Reconstruction loss for epoch": self.rec_losses[-1]})
                wandb.log({"Sum of losses for epoch": self.total_losses[-1]})
                wandb.log(
                    {"LR/Model learning rate": optimizer.param_groups[0]["lr"]}
                )

            self.log(f"Ncuts loss: {self.ncuts_losses[-1]:.5f}")
            self.log(f"Reconstruction loss: {self.rec_losses[-1]:.5f}")
            self.log(f"Weighted sum of losses: {self.total_losses[-1]:.5f}")
            if epoch > 0:
                self.log(
                    f"Ncuts loss difference: {self.ncuts_losses[-1] - self.ncuts_losses[-2]:.5f}"
                )
                self.log(
                    f"Reconstruction loss difference: {self.rec_losses[-1] - self.rec_losses[-2]:.5f}"
                )
                self.log(
                    f"Weighted sum of losses difference: {self.total_losses[-1] - self.total_losses[-2]:.5f}"
                )

            if (
                self.eval_dataloader is not None
                and (epoch + 1) % self.config.validation_interval == 0
            ):
                model.eval()
                self.log("Validating...")
                yield self.eval(model, epoch)  # validation

                if self._abort_requested:
                    self.dataloader = None
                    del self.dataloader
                    self.eval_dataloader = None
                    del self.eval_dataloader
                    model = None
                    del model
                    optimizer = None
                    del optimizer
                    criterionE = None
                    del criterionE
                    criterionW = None
                    del criterionW
                    torch.cuda.empty_cache()

            eta = (
                (time.time() - self.start_time)
                * (self.config.max_epochs / (epoch + 1) - 1)
                / 60
            )
            self.log(f"ETA: {eta:.1f} minutes")
            self.log("-" * 20)

            # Save the model
            if epoch % 5 == 0:
                torch.save(
                    model.state_dict(),
                    self.config.results_path_folder + "/wnet_checkpoint.pth",
                )

        self.log("Training finished")
        if self.best_dice > -1:
            best_dice_epoch = epoch
            self.log(
                f"Best dice metric : {self.best_dice} at epoch {best_dice_epoch}"
            )

            if WANDB_INSTALLED:
                wandb.log(
                    {
                        "Validation/Best Dice": self.best_dice,
                        "Validation/Best Dice epoch": best_dice_epoch,
                    }
                )

        # Save the model
        self.log(
            f"Saving the model to: {self.config.results_path_folder}/wnet.pth",
        )
        save_weights_path = self.config.results_path_folder + "/wnet.pth"
        torch.save(
            model.state_dict(),
            save_weights_path,
        )

        if WANDB_INSTALLED and self.wandb_config.save_model_artifact:
            model_artifact = wandb.Artifact(
                "WNet3D",
                type="model",
                description="CellSeg3D WNet",
                metadata=self.config.__dict__,
            )
            model_artifact.add_file(save_weights_path)
            wandb.log_artifact(model_artifact)

        dataloader = None
        del dataloader
        self.eval_dataloader = None
        del self.eval_dataloader
        model = None
        del model
        optimizer = None
        del optimizer
        criterionE = None
        del criterionE
        criterionW = None
        del criterionW
        torch.cuda.empty_cache()
        if WANDB_INSTALLED:
            wandb.finish()
    except Exception as e:
        if WANDB_INSTALLED:
            wandb.finish()
        msg = f"Training failed with exception: {e}"
        self.log(msg)
        self.raise_error(e, msg)
        self.quit()
        raise e