|
| 1 | +"""Defines SemanticSegmentor Engine.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from .patch_predictor import PatchPredictor |
| 8 | + |
| 9 | +if TYPE_CHECKING: # pragma: no cover |
| 10 | + from pathlib import Path |
| 11 | + |
| 12 | + from tiatoolbox.models.models_abc import ModelABC |
| 13 | + |
| 14 | + |
| 15 | +class SemanticSegmentor(PatchPredictor): |
| 16 | + """Pixel-wise segmentation predictor. |
| 17 | +
|
| 18 | + The tiatoolbox model should produce the following results on the BCSS dataset |
| 19 | + using fcn_resnet50_unet-bcss. |
| 20 | +
|
| 21 | + .. list-table:: Semantic segmentation performance on the BCSS dataset |
| 22 | + :widths: 15 15 15 15 15 15 15 |
| 23 | + :header-rows: 1 |
| 24 | +
|
| 25 | + * - |
| 26 | + - Tumour |
| 27 | + - Stroma |
| 28 | + - Inflammatory |
| 29 | + - Necrosis |
| 30 | + - Other |
| 31 | + - All |
| 32 | + * - Amgad et al. |
| 33 | + - 0.851 |
| 34 | + - 0.800 |
| 35 | + - 0.712 |
| 36 | + - 0.723 |
| 37 | + - 0.666 |
| 38 | + - 0.750 |
| 39 | + * - TIAToolbox |
| 40 | + - 0.885 |
| 41 | + - 0.825 |
| 42 | + - 0.761 |
| 43 | + - 0.765 |
| 44 | + - 0.581 |
| 45 | + - 0.763 |
| 46 | +
|
| 47 | + Note, if `model` is supplied in the arguments, it will ignore the |
| 48 | + `pretrained_model` and `pretrained_weights` arguments. |
| 49 | +
|
| 50 | + Args: |
| 51 | + model (nn.Module): |
| 52 | + Use externally defined PyTorch model for prediction with |
| 53 | + weights already loaded. Default is `None`. If provided, |
| 54 | + `pretrained_model` argument is ignored. |
| 55 | + pretrained_model (str): |
| 56 | + Name of the existing models support by tiatoolbox for |
| 57 | + processing the data. For a full list of pretrained models, |
| 58 | + refer to the `docs |
| 59 | + <https://tia-toolbox.readthedocs.io/en/latest/pretrained.html>`_. |
| 60 | + By default, the corresponding pretrained weights will also |
| 61 | + be downloaded. However, you can override with your own set |
| 62 | + of weights via the `pretrained_weights` argument. Argument |
| 63 | + is case-insensitive. |
| 64 | + pretrained_weights (str): |
| 65 | + Path to the weight of the corresponding `pretrained_model`. |
| 66 | + batch_size (int): |
| 67 | + Number of images fed into the model each time. |
| 68 | + num_loader_workers (int): |
| 69 | + Number of workers to load the data. Take note that they will |
| 70 | + also perform preprocessing. |
| 71 | + num_postproc_workers (int): |
| 72 | + This value is there to maintain input compatibility with |
| 73 | + `tiatoolbox.models.classification` and is not used. |
| 74 | + verbose (bool): |
| 75 | + Whether to output logging information. |
| 76 | + dataset_class (obj): |
| 77 | + Dataset class to be used instead of default. |
| 78 | + auto_generate_mask (bool): |
| 79 | + To automatically generate tile/WSI tissue mask if is not |
| 80 | + provided. |
| 81 | +
|
| 82 | + Attributes: |
| 83 | + process_prediction_per_batch (bool): |
| 84 | + A flag to denote whether post-processing for inference |
| 85 | + output is applied after each batch or after finishing an entire |
| 86 | + tile or WSI. |
| 87 | +
|
| 88 | + Examples: |
| 89 | + >>> # Sample output of a network |
| 90 | + >>> wsis = ['A/wsi.svs', 'B/wsi.svs'] |
| 91 | + >>> predictor = SemanticSegmentor(model='fcn-tissue_mask') |
| 92 | + >>> output = predictor.predict(wsis, mode='wsi') |
| 93 | + >>> list(output.keys()) |
| 94 | + [('A/wsi.svs', 'output/0.raw') , ('B/wsi.svs', 'output/1.raw')] |
| 95 | + >>> # if a network have 2 output heads, each head output of 'A/wsi.svs' |
| 96 | + >>> # will be respectively stored in 'output/0.raw.0', 'output/0.raw.1' |
| 97 | +
|
| 98 | + """ |
| 99 | + |
| 100 | + def __init__( |
| 101 | + self: SemanticSegmentor, |
| 102 | + model: str | ModelABC, |
| 103 | + batch_size: int = 8, |
| 104 | + num_loader_workers: int = 0, |
| 105 | + num_post_proc_workers: int = 0, |
| 106 | + weights: str | Path | None = None, |
| 107 | + *, |
| 108 | + device: str = "cpu", |
| 109 | + verbose: bool = True, |
| 110 | + ) -> None: |
| 111 | + """Initialize :class:`SemanticSegmentor`.""" |
| 112 | + super().__init__( |
| 113 | + model=model, |
| 114 | + batch_size=batch_size, |
| 115 | + num_loader_workers=num_loader_workers, |
| 116 | + num_post_proc_workers=num_post_proc_workers, |
| 117 | + weights=weights, |
| 118 | + device=device, |
| 119 | + verbose=verbose, |
| 120 | + ) |
0 commit comments