Skip to content

careamist_v2

CAREamistV2 #

Source code in src/careamics/careamist_v2.py
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
class CAREamistV2:
    def __init__(
        self,
        config: Configuration | Path | None = None,
        *,
        checkpoint_path: Path | None = None,
        bmz_path: Path | None = None,
        **user_context: Unpack[UserContext],
    ):
        self.checkpoint_path = checkpoint_path
        self.work_dir = self._resolve_work_dir(user_context.get("work_dir"))
        self.config, self.model = self._load_model(config, checkpoint_path, bmz_path)

        enable_progress_bar = user_context.get("enable_progress_bar", True)
        self.config.training_config.lightning_trainer_config["enable_progress_bar"] = (
            enable_progress_bar
        )
        callbacks = user_context.get("callbacks", None)
        self.callbacks = self._define_callbacks(callbacks, self.config, self.work_dir)

        self.prediction_writer = PredictionWriterCallback(
            self.work_dir, enable_writing=False
        )

        experiment_loggers = self._create_loggers(
            self.config.training_config.logger,
            self.config.experiment_name,
            self.work_dir,
        )

        self.trainer = Trainer(
            callbacks=[self.prediction_writer, *self.callbacks],
            default_root_dir=self.work_dir,
            logger=experiment_loggers,
            **self.config.training_config.lightning_trainer_config or {},
        )

        self.train_datamodule: CareamicsDataModule | None = None

    def _load_model(
        self,
        config: Configuration | Path | None,
        checkpoint_path: Path | None,
        bmz_path: Path | None,
    ) -> tuple[Configuration, CAREamicsModule]:
        n_inputs = sum(
            [config is not None, checkpoint_path is not None, bmz_path is not None]
        )
        if n_inputs != 1:
            raise ValueError(
                "Exactly one of `config`, `checkpoint_path`, or `bmz_path` "
                "must be provided."
            )
        if config is not None:
            return self._from_config(config)
        elif checkpoint_path is not None:
            return self._from_checkpoint(checkpoint_path)
        else:
            assert bmz_path is not None
            return self._from_bmz(bmz_path)

    @staticmethod
    def _from_config(
        config: Configuration | Path,
    ) -> tuple[Configuration, CAREamicsModule]:
        if isinstance(config, Path):
            config = load_configuration_ng(config)
        assert not isinstance(config, Path)

        model = create_module(config.algorithm_config)
        return config, model

    @staticmethod
    def _from_checkpoint(
        checkpoint_path: Path,
    ) -> tuple[Configuration, CAREamicsModule]:
        checkpoint: dict = torch.load(checkpoint_path, map_location="cpu")

        careamics_info = checkpoint.get("careamics_info", None)
        if careamics_info is None:
            raise ValueError(
                "Could not find CAREamics related information within the provided "
                "checkpoint. This means that it was saved without using the "
                "CAREamics callback `CareamicsCheckpointInfo`. "
                "Please use a checkpoint saved with CAREamics or initialize with a "
                "config instead."
            )

        try:
            algorithm_config: dict[str, Any] = checkpoint["hyper_parameters"][
                "algorithm_config"
            ]
        except (KeyError, IndexError) as e:
            raise ValueError(
                "Could not determine CAREamics supported algorithm from the provided "
                f"checkpoint at: {checkpoint_path!s}."
            ) from e

        data_hparams_key = checkpoint.get(
            "datamodule_hparams_name", "datamodule_hyper_parameters"
        )
        try:
            data_config: dict[str, Any] = checkpoint[data_hparams_key]["data_config"]
        except (KeyError, IndexError) as e:
            raise ValueError(
                "Could not determine the data configuration from the provided "
                f"checkpoint at: {checkpoint_path!s}."
            ) from e

        # TODO: will need to resolve this with type adapter once more configs are added
        config = Configuration.model_validate(
            {
                "algorithm_config": algorithm_config,
                "data_config": data_config,
                **careamics_info,
            }
        )

        module = load_module_from_checkpoint(checkpoint_path)
        return config, module

    @staticmethod
    def _from_bmz(
        bmz_path: Path,
    ) -> tuple[Configuration, CAREamicsModule]:
        raise NotImplementedError("Loading from BMZ is not implemented yet.")

    @staticmethod
    def _resolve_work_dir(work_dir: str | Path | None) -> Path:
        if work_dir is None:
            work_dir = Path.cwd().resolve()
            logger.warning(
                f"No working directory provided. Using current working directory: "
                f"{work_dir}."
            )
        else:
            work_dir = Path(work_dir).resolve()
        return work_dir

    @staticmethod
    def _define_callbacks(
        callbacks: list[Callback] | None,
        config: Configuration,
        work_dir: Path,
    ) -> list[Callback]:
        callbacks = [] if callbacks is None else callbacks
        for c in callbacks:
            if isinstance(c, (ModelCheckpoint, EarlyStopping)):
                raise ValueError(
                    "`ModelCheckpoint` and `EarlyStopping` callbacks are already "
                    "defined in CAREamics and should only be modified through the "
                    "training configuration (see TrainingConfig)."
                )

            if isinstance(c, (CareamicsCheckpointInfo, ProgressBarCallback)):
                raise ValueError(
                    "`CareamicsCheckpointInfo` and `ProgressBar` callbacks are defined "
                    "internally and should not be passed as callbacks."
                )

        internal_callbacks = [
            ModelCheckpoint(
                dirpath=work_dir / "checkpoints",
                filename=f"{config.experiment_name}_{{epoch:02d}}_step_{{step}}",
                **config.training_config.checkpoint_callback.model_dump(),
            ),
            CareamicsCheckpointInfo(
                config.version, config.experiment_name, config.training_config
            ),
        ]

        enable_progress_bar = config.training_config.lightning_trainer_config.get(
            "enable_progress_bar", True
        )
        if enable_progress_bar:
            internal_callbacks.append(ProgressBarCallback())

        if config.training_config.early_stopping_callback is not None:
            internal_callbacks.append(
                EarlyStopping(
                    **config.training_config.early_stopping_callback.model_dump()
                )
            )

        return internal_callbacks + callbacks

    @staticmethod
    def _create_loggers(
        logger: str | None, experiment_name: str, work_dir: Path
    ) -> list[ExperimentLogger]:
        csv_logger = CSVLogger(name=experiment_name, save_dir=work_dir / "csv_logs")

        if logger is not None:
            logger = SupportedLogger(logger)

        match logger:
            case SupportedLogger.WANDB:
                return [
                    WandbLogger(name=experiment_name, save_dir=work_dir / "wandb_logs"),
                    csv_logger,
                ]
            case SupportedLogger.TENSORBOARD:
                return [
                    TensorBoardLogger(save_dir=work_dir / "tb_logs"),
                    csv_logger,
                ]
            case _:
                return [csv_logger]

    def train(
        self,
        *,
        # BASIC PARAMS
        train_data: InputType | None = None,
        train_data_target: InputType | None = None,
        val_data: InputType | None = None,
        val_data_target: InputType | None = None,
        # val_percentage: float | None = None, # TODO: hidden till re-implemented
        # val_minimum_split: int = 5,
        # ADVANCED PARAMS
        filtering_mask: InputType | None = None,
        read_source_func: ReadFunc | None = None,
        read_kwargs: dict[str, Any] | None = None,
        extension_filter: str = "",
    ) -> None:
        """Train the model on the provided data.

        The training data can be provided as arrays or paths.

        Parameters
        ----------
        train_data : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
            Training data, by default None.
        train_data_target : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
            Training target data, by default None.
        val_data : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
            Validation data, by default None.
        val_data_target : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
            Validation target data, by default None.
        filtering_mask : InputType, optional
            Filtering mask for coordinate-based patch filtering, by default None.
        read_source_func : ReadFunc, optional
            Function to read the source data, by default None.
        read_kwargs : dict[str, Any], optional
            Keyword arguments for the read function, by default None.
        extension_filter : str, optional
            Filter for file extensions, by default "".

        Raises
        ------
        ValueError
            If neither train_data is not provided.
        """
        if train_data is None:
            raise ValueError(
                "Training data must be provided. Provide `train_data`."
            )

        datamodule = CareamicsDataModule(  # type: ignore[misc]
            data_config=self.config.data_config,
            train_data=train_data,
            val_data=val_data,
            train_data_target=train_data_target,
            val_data_target=val_data_target,
            train_data_mask=filtering_mask,  # type: ignore[arg-type]
            read_source_func=read_source_func,  # type: ignore[arg-type]
            read_kwargs=read_kwargs,
            extension_filter=extension_filter,
        )
        self.train_datamodule = datamodule

        # set defaults (in case `stop_training` was called before)
        self.trainer.should_stop = False
        self.trainer.limit_val_batches = 1.0

        self.trainer.fit(self.model, datamodule=datamodule, ckpt_path=self.checkpoint_path)

    def predict(
        self,
        # BASIC PARAMS
        pred_data: Any | None = None,
        batch_size: int = 1,
        tile_size: tuple[int, ...] | None = None,
        tile_overlap: tuple[int, ...] | None = (48, 48),
        axes: str | None = None,
        data_type: Literal["array", "tiff", "custom"] | None = None,
        # ADVANCED PARAMS
        # tta_transforms: bool = False, # TODO: hidden till implemented
        num_workers: int | None = None,
        read_source_func: Callable | None = None,
        read_kwargs: dict[str, Any] | None = None,
        extension_filter: str = "",
    ) -> None:
        raise NotImplementedError("Predicting is not implemented yet.")

    def predict_to_disk(
        self,
        # BASIC PARAMS
        pred_data: Any | None = None,
        pred_data_target: Any | None = None,
        prediction_dir: Path | str = "predictions",
        batch_size: int = 1,
        tile_size: tuple[int, ...] | None = None,
        tile_overlap: tuple[int, ...] | None = (48, 48),
        axes: str | None = None,
        data_type: Literal["array", "tiff", "custom"] | None = None,
        # ADVANCED PARAMS
        num_workers: int | None = None,
        read_source_func: Callable | None = None,
        read_kwargs: dict[str, Any] | None = None,
        extension_filter: str = "",
        # WRITE OPTIONS
        write_type: Literal["tiff", "zarr", "custom"] = "tiff",
        write_extension: str | None = None,
        write_func: WriteFunc | None = None,
        write_func_kwargs: dict[str, Any] | None = None,
    ) -> None:
        raise NotImplementedError("Predicting to disk is not implemented yet.")

    def export_to_bmz(
        self,
        path_to_archive: Path | str,
        friendly_model_name: str,
        input_array: NDArray,
        authors: list[dict],
        general_description: str,
        data_description: str,
        covers: list[Path | str] | None = None,
        channel_names: list[str] | None = None,
        model_version: str = "0.1.0",
    ) -> None:
        """Export the model to the BioImage Model Zoo format.

        This method packages the current weights into a zip file that can be uploaded
        to the BioImage Model Zoo. The archive consists of the model weights, the model
        specifications and various files (inputs, outputs, README, env.yaml etc.).

        `path_to_archive` should point to a file with a ".zip" extension.

        `friendly_model_name` is the name used for the model in the BMZ specs
        and website, it should consist of letters, numbers, dashes, underscores and
        parentheses only.

        Input array must be of the same dimensions as the axes recorded in the
        configuration of the `CAREamist`.

        Parameters
        ----------
        path_to_archive : pathlib.Path or str
            Path in which to save the model, including file name, which should end with
            ".zip".
        friendly_model_name : str
            Name of the model as used in the BMZ specs, it should consist of letters,
            numbers, dashes, underscores and parentheses only.
        input_array : NDArray
            Input array used to validate the model and as example.
        authors : list of dict
            List of authors of the model.
        general_description : str
            General description of the model used in the BMZ metadata.
        data_description : str
            Description of the data the model was trained on.
        covers : list of pathlib.Path or str, default=None
            Paths to the cover images.
        channel_names : list of str, default=None
            Channel names.
        model_version : str, default="0.1.0"
            Version of the model.
        """
        output_patch = self.predict(
            pred_data=input_array,
            data_type=SupportedData.ARRAY.value,
        )
        output = np.concatenate(output_patch, axis=0)
        input_array = reshape_array(input_array, self.config.data_config.axes)

        export_to_bmz(
            model=self.model,
            config=self.config,
            path_to_archive=path_to_archive,
            model_name=friendly_model_name,
            general_description=general_description,
            data_description=data_description,
            authors=authors,
            input_array=input_array,
            output_array=output,
            covers=covers,
            channel_names=channel_names,
            model_version=model_version,
        )

    def get_losses(self) -> dict[str, list]:
        """Return data that can be used to plot train and validation loss curves.

        Returns
        -------
        dict of str: list
            Dictionary containing losses for each epoch.
        """
        return read_csv_logger(self.config.experiment_name, self.work_dir / "csv_logs")

    def stop_training(self) -> None:
        """Stop the training loop."""
        self.trainer.should_stop = True
        self.trainer.limit_val_batches = 0  # skip validation

export_to_bmz(path_to_archive, friendly_model_name, input_array, authors, general_description, data_description, covers=None, channel_names=None, model_version='0.1.0') #

Export the model to the BioImage Model Zoo format.

This method packages the current weights into a zip file that can be uploaded to the BioImage Model Zoo. The archive consists of the model weights, the model specifications and various files (inputs, outputs, README, env.yaml etc.).

path_to_archive should point to a file with a ".zip" extension.

friendly_model_name is the name used for the model in the BMZ specs and website, it should consist of letters, numbers, dashes, underscores and parentheses only.

Input array must be of the same dimensions as the axes recorded in the configuration of the CAREamist.

Parameters:

Name Type Description Default
path_to_archive Path or str

Path in which to save the model, including file name, which should end with ".zip".

required
friendly_model_name str

Name of the model as used in the BMZ specs, it should consist of letters, numbers, dashes, underscores and parentheses only.

required
input_array NDArray

Input array used to validate the model and as example.

required
authors list of dict

List of authors of the model.

required
general_description str

General description of the model used in the BMZ metadata.

required
data_description str

Description of the data the model was trained on.

required
covers list of pathlib.Path or str

Paths to the cover images.

None
channel_names list of str

Channel names.

None
model_version str

Version of the model.

"0.1.0"
Source code in src/careamics/careamist_v2.py
def export_to_bmz(
    self,
    path_to_archive: Path | str,
    friendly_model_name: str,
    input_array: NDArray,
    authors: list[dict],
    general_description: str,
    data_description: str,
    covers: list[Path | str] | None = None,
    channel_names: list[str] | None = None,
    model_version: str = "0.1.0",
) -> None:
    """Export the model to the BioImage Model Zoo format.

    This method packages the current weights into a zip file that can be uploaded
    to the BioImage Model Zoo. The archive consists of the model weights, the model
    specifications and various files (inputs, outputs, README, env.yaml etc.).

    `path_to_archive` should point to a file with a ".zip" extension.

    `friendly_model_name` is the name used for the model in the BMZ specs
    and website, it should consist of letters, numbers, dashes, underscores and
    parentheses only.

    Input array must be of the same dimensions as the axes recorded in the
    configuration of the `CAREamist`.

    Parameters
    ----------
    path_to_archive : pathlib.Path or str
        Path in which to save the model, including file name, which should end with
        ".zip".
    friendly_model_name : str
        Name of the model as used in the BMZ specs, it should consist of letters,
        numbers, dashes, underscores and parentheses only.
    input_array : NDArray
        Input array used to validate the model and as example.
    authors : list of dict
        List of authors of the model.
    general_description : str
        General description of the model used in the BMZ metadata.
    data_description : str
        Description of the data the model was trained on.
    covers : list of pathlib.Path or str, default=None
        Paths to the cover images.
    channel_names : list of str, default=None
        Channel names.
    model_version : str, default="0.1.0"
        Version of the model.
    """
    output_patch = self.predict(
        pred_data=input_array,
        data_type=SupportedData.ARRAY.value,
    )
    output = np.concatenate(output_patch, axis=0)
    input_array = reshape_array(input_array, self.config.data_config.axes)

    export_to_bmz(
        model=self.model,
        config=self.config,
        path_to_archive=path_to_archive,
        model_name=friendly_model_name,
        general_description=general_description,
        data_description=data_description,
        authors=authors,
        input_array=input_array,
        output_array=output,
        covers=covers,
        channel_names=channel_names,
        model_version=model_version,
    )

get_losses() #

Return data that can be used to plot train and validation loss curves.

Returns:

Type Description
dict of str: list

Dictionary containing losses for each epoch.

Source code in src/careamics/careamist_v2.py
def get_losses(self) -> dict[str, list]:
    """Return data that can be used to plot train and validation loss curves.

    Returns
    -------
    dict of str: list
        Dictionary containing losses for each epoch.
    """
    return read_csv_logger(self.config.experiment_name, self.work_dir / "csv_logs")

stop_training() #

Stop the training loop.

Source code in src/careamics/careamist_v2.py
def stop_training(self) -> None:
    """Stop the training loop."""
    self.trainer.should_stop = True
    self.trainer.limit_val_batches = 0  # skip validation

train(*, train_data=None, train_data_target=None, val_data=None, val_data_target=None, filtering_mask=None, read_source_func=None, read_kwargs=None, extension_filter='') #

Train the model on the provided data.

The training data can be provided as arrays or paths.

Parameters:

Name Type Description Default
train_data numpy.ndarray, str, pathlib.Path, or sequence of these

Training data, by default None.

None
train_data_target numpy.ndarray, str, pathlib.Path, or sequence of these

Training target data, by default None.

None
val_data numpy.ndarray, str, pathlib.Path, or sequence of these

Validation data, by default None.

None
val_data_target numpy.ndarray, str, pathlib.Path, or sequence of these

Validation target data, by default None.

None
filtering_mask InputType

Filtering mask for coordinate-based patch filtering, by default None.

None
read_source_func ReadFunc

Function to read the source data, by default None.

None
read_kwargs dict[str, Any]

Keyword arguments for the read function, by default None.

None
extension_filter str

Filter for file extensions, by default "".

''

Raises:

Type Description
ValueError

If neither train_data is not provided.

Source code in src/careamics/careamist_v2.py
def train(
    self,
    *,
    # BASIC PARAMS
    train_data: InputType | None = None,
    train_data_target: InputType | None = None,
    val_data: InputType | None = None,
    val_data_target: InputType | None = None,
    # val_percentage: float | None = None, # TODO: hidden till re-implemented
    # val_minimum_split: int = 5,
    # ADVANCED PARAMS
    filtering_mask: InputType | None = None,
    read_source_func: ReadFunc | None = None,
    read_kwargs: dict[str, Any] | None = None,
    extension_filter: str = "",
) -> None:
    """Train the model on the provided data.

    The training data can be provided as arrays or paths.

    Parameters
    ----------
    train_data : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
        Training data, by default None.
    train_data_target : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
        Training target data, by default None.
    val_data : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
        Validation data, by default None.
    val_data_target : numpy.ndarray, str, pathlib.Path, or sequence of these, optional
        Validation target data, by default None.
    filtering_mask : InputType, optional
        Filtering mask for coordinate-based patch filtering, by default None.
    read_source_func : ReadFunc, optional
        Function to read the source data, by default None.
    read_kwargs : dict[str, Any], optional
        Keyword arguments for the read function, by default None.
    extension_filter : str, optional
        Filter for file extensions, by default "".

    Raises
    ------
    ValueError
        If neither train_data is not provided.
    """
    if train_data is None:
        raise ValueError(
            "Training data must be provided. Provide `train_data`."
        )

    datamodule = CareamicsDataModule(  # type: ignore[misc]
        data_config=self.config.data_config,
        train_data=train_data,
        val_data=val_data,
        train_data_target=train_data_target,
        val_data_target=val_data_target,
        train_data_mask=filtering_mask,  # type: ignore[arg-type]
        read_source_func=read_source_func,  # type: ignore[arg-type]
        read_kwargs=read_kwargs,
        extension_filter=extension_filter,
    )
    self.train_datamodule = datamodule

    # set defaults (in case `stop_training` was called before)
    self.trainer.should_stop = False
    self.trainer.limit_val_batches = 1.0

    self.trainer.fit(self.model, datamodule=datamodule, ckpt_path=self.checkpoint_path)