careamist
A class to train, predict and export models in CAREamics.
CAREamist
#
Main CAREamics class, allowing training and prediction using various algorithms.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source | pathlib.Path or str or CAREamics Configuration | Path to a configuration file or a trained model. | required |
work_dir | str | Path to working directory in which to save checkpoints and logs, by default None. | None |
callbacks | list of Callback | List of callbacks to use during training and prediction, by default None. | None |
Attributes:
Name | Type | Description |
---|---|---|
model | CAREamicsModule | CAREamics model. |
cfg | Configuration | CAREamics configuration. |
trainer | Trainer | PyTorch Lightning trainer. |
experiment_logger | TensorBoardLogger or WandbLogger | Experiment logger, "wandb" or "tensorboard". |
work_dir | Path | Working directory. |
train_datamodule | TrainDataModule | Training datamodule. |
pred_datamodule | PredictDataModule | Prediction datamodule. |
Source code in src/careamics/careamist.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 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 |
|
__init__(source, work_dir=None, callbacks=None)
#
Initialize CAREamist with a configuration object or a path.
A configuration object can be created using directly by calling Configuration
, using the configuration factory or loading a configuration from a yaml file.
Path can contain either a yaml file with parameters, or a saved checkpoint.
If no working directory is provided, the current working directory is used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source | pathlib.Path or str or CAREamics Configuration | Path to a configuration file or a trained model. | required |
work_dir | str or Path | Path to working directory in which to save checkpoints and logs, by default None. | None |
callbacks | list of Callback | List of callbacks to use during training and prediction, by default None. | None |
Raises:
Type | Description |
---|---|
NotImplementedError | If the model is loaded from BioImage Model Zoo. |
ValueError | If no hyper parameters are found in the checkpoint. |
ValueError | If no data module hyper parameters are found in the checkpoint. |
Source code in src/careamics/careamist.py
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 |
|
export_to_bmz(path_to_archive, friendly_model_name, input_array, authors, general_description='', channel_names=None, data_description=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:
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 metadata of the BMZ archive. | '' |
channel_names | list of str | Channel names, by default None. | None |
data_description | str | Description of the data, by default None. | None |
Source code in src/careamics/careamist.py
predict(source, *, batch_size=None, tile_size=None, tile_overlap=(48, 48), axes=None, data_type=None, tta_transforms=False, dataloader_params=None, read_source_func=None, extension_filter='', **kwargs)
#
predict(source: Union[Path, str], *, batch_size: int = 1, tile_size: Optional[tuple[int, ...]] = None, tile_overlap: tuple[int, ...] = (48, 48), axes: Optional[str] = None, data_type: Optional[Literal['tiff', 'custom']] = None, tta_transforms: bool = False, dataloader_params: Optional[dict] = None, read_source_func: Optional[Callable] = None, extension_filter: str = '') -> Union[list[NDArray], NDArray]
predict(source: NDArray, *, batch_size: int = 1, tile_size: Optional[tuple[int, ...]] = None, tile_overlap: tuple[int, ...] = (48, 48), axes: Optional[str] = None, data_type: Optional[Literal['array']] = None, tta_transforms: bool = False, dataloader_params: Optional[dict] = None) -> Union[list[NDArray], NDArray]
Make predictions on the provided data.
Input can be a CAREamicsPredData instance, a path to a data file, or a numpy array.
If data_type
, axes
and tile_size
are not provided, the training configuration parameters will be used, with the patch_size
instead of tile_size
.
Test-time augmentation (TTA) can be switched off using the tta_transforms
parameter. The TTA augmentation applies all possible flip and 90 degrees rotations to the prediction input and averages the predictions. TTA augmentation should not be used if you did not train with these augmentations.
Note that if you are using a UNet model and tiling, the tile size must be divisible in every dimension by 2**d, where d is the depth of the model. This avoids artefacts arising from the broken shift invariance induced by the pooling layers of the UNet. If your image has less dimensions, as it may happen in the Z dimension, consider padding your image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source | (CAREamicsPredData, Path, str or ndarray) | Data to predict on. | required |
batch_size | int | Batch size for prediction. | 1 |
tile_size | tuple of int | Size of the tiles to use for prediction. | None |
tile_overlap | tuple of int | Overlap between tiles, can be None. | (48, 48) |
axes | str | Axes of the input data, by default None. | None |
data_type | (array, tiff, custom) | Type of the input data. | "array" |
tta_transforms | bool | Whether to apply test-time augmentation. | True |
dataloader_params | dict | Parameters to pass to the dataloader. | None |
read_source_func | Callable | Function to read the source data. | None |
extension_filter | str | Filter for the file extension. | "" |
**kwargs | Any | Unused. | {} |
Returns:
Type | Description |
---|---|
list of NDArray or NDArray | Predictions made by the model. |
Raises:
Type | Description |
---|---|
ValueError | If mean and std are not provided in the configuration. |
ValueError | If tile size is not divisible by 2**depth for UNet models. |
ValueError | If tile overlap is not specified. |
Source code in src/careamics/careamist.py
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 |
|
stop_training()
#
train(*, datamodule=None, train_source=None, val_source=None, train_target=None, val_target=None, use_in_memory=True, val_percentage=0.1, val_minimum_split=1)
#
Train the model on the provided data.
If a datamodule is provided, then training will be performed using it. Alternatively, the training data can be provided as arrays or paths.
If use_in_memory
is set to True, the source provided as Path or str will be loaded in memory if it fits. Otherwise, training will be performed by loading patches from the files one by one. Training on arrays is always performed in memory.
If no validation source is provided, then the validation is extracted from the training data using val_percentage
and val_minimum_split
. In the case of data provided as Path or str, the percentage and minimum number are applied to the number of files. For arrays, it is the number of patches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datamodule | TrainDataModule | Datamodule to train on, by default None. | None |
train_source | Path or str or NDArray | Train source, if no datamodule is provided, by default None. | None |
val_source | Path or str or NDArray | Validation source, if no datamodule is provided, by default None. | None |
train_target | Path or str or NDArray | Train target source, if no datamodule is provided, by default None. | None |
val_target | Path or str or NDArray | Validation target source, if no datamodule is provided, by default None. | None |
use_in_memory | bool | Use in memory dataset if possible, by default True. | True |
val_percentage | float | Percentage of validation extracted from training data, by default 0.1. | 0.1 |
val_minimum_split | int | Minimum number of validation (patch or file) extracted from training data, by default 1. | 1 |
Raises:
Type | Description |
---|---|
ValueError | If both |
ValueError | If sources are not of the same type (e.g. train is an array and val is a Path). |
ValueError | If the training target is provided to N2V. |
ValueError | If neither a datamodule nor a source is provided. |
Source code in src/careamics/careamist.py
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 |
|