CellVitPanoptic
Bases: BaseModelPanoptic
Source code in src/histolytics/models/cellvit_panoptic.py
__init__ ¶
__init__(n_nuc_classes: int, n_tissue_classes: int, enc_name: str = 'samvit_base_patch16', enc_pretrain: bool = True, enc_freeze: bool = False, device: device = torch.device('cuda'), model_kwargs: Dict[str, Any] = {}) -> None
CellVitPanoptic model for panoptic segmentation of nuclei and tissues.
Note
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_nuc_classes
|
int
|
Number of nuclei type classes. |
required |
n_tissue_classes
|
int
|
Number of tissue type classes. |
required |
enc_name
|
str
|
Name of the pytorch-image-models encoder. |
'samvit_base_patch16'
|
enc_pretrain
|
bool
|
Whether to use pretrained weights in the encoder. |
True
|
enc_freeze
|
bool
|
Freeze encoder weights for training. |
False
|
device
|
device
|
Device to run the model on. |
device('cuda')
|
model_kwargs
|
dict
|
Additional keyword arguments for the model. |
{}
|
Source code in src/histolytics/models/cellvit_panoptic.py
set_inference_mode ¶
Set model to inference mode.
Source code in src/histolytics/models/cellvit_panoptic.py
from_pretrained
classmethod
¶
from_pretrained(weights: Union[str, Path], device: device = torch.device('cuda'), model_kwargs: Dict[str, Any] = {})
Load the model from pretrained weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the pretrained model. |
required |
device
|
device
|
Device to run the model on. |
device('cuda')
|
model_kwargs
|
Dict[str, Any]
|
Additional arguments for the model. |
{}
|
Examples:
Source code in src/histolytics/models/_base_model.py
predict ¶
predict(x: Union[Tensor, ndarray, Image], *, use_sliding_win: bool = False, window_size: Tuple[int, int] = None, stride: int = None, save_intermediate: bool = False) -> Dict[str, Union[SoftSemanticOutput, SoftInstanceOutput]]
Predict the input image or image batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[Tensor, ndarray, Image]
|
Input image (H, W, C) or input image batch (B, C, H, W). |
required |
use_sliding_win
|
bool
|
Whether to use sliding window for prediction. |
False
|
window_size
|
Tuple[int, int]
|
The height and width of the sliding window. If |
None
|
stride
|
int
|
The stride for the sliding window. If |
None
|
save_intermediate
|
bool, default=False
|
Whether to save intermediate results (logits). If True, the method returns a tuple (final predictions, intermediate results), where the intermediate results are the raw model outputs before argmax. |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Union[SoftSemanticOutput, SoftInstanceOutput]]
|
Dict[str, Union[SoftSemanticOutput, SoftInstanceOutput]]: Dictionary of soft outputs: |
Examples:
>>> my_model.set_inference_mode()
>>> # with sliding window if image is large
>>> x = my_model.predict(x=image, use_sliding_win=True, window_size=(256, 256), stride=128)
>>> # without sliding window if image is small enough
>>> x = my_model.predict(x=image, use_sliding_win=False)
Source code in src/histolytics/models/_base_model.py
post_process ¶
post_process(x: Dict[str, Union[SoftSemanticOutput, SoftInstanceOutput]], *, use_async_postproc: bool = True, start_method: str = 'threading', n_jobs: int = 4, save_paths_nuc: List[Union[Path, str]] = None, save_paths_cyto: List[Union[Path, str]] = None, save_paths_tissue: List[Union[Path, str]] = None, coords: List[Tuple[int, int, int, int]] = None, class_dict_nuc: Dict[int, str] = None, class_dict_cyto: Dict[int, str] = None, class_dict_tissue: Dict[int, str] = None, nuc_smooth_func: Callable = gaussian_smooth, cyto_smooth_func: Callable = gaussian_smooth, tissue_smooth_func: Callable = None) -> Dict[str, List[np.ndarray]]
Post-process the output of the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Dict[str, Union[SoftSemanticOutput, SoftInstanceOutput]]
|
The output of the .predict() method. |
required |
use_async_postproc
|
bool
|
Whether to use async post-processing. Can give some run-time benefits. |
True
|
start_method
|
str
|
The start method. One of: "threading", "fork", "spawn". See mpire docs. |
'threading'
|
n_jobs
|
int
|
The number of workers for the post-processing. |
4
|
save_paths_nuc
|
List[Union[Path, str]]
|
The paths to save the panlei masks. If None, the masks are not saved. |
None
|
save_paths_cyto
|
List[Union[Path, str]]
|
The paths to save the cytoplasm masks. If None, the masks are not saved. |
None
|
save_paths_tissue
|
List[Union[Path, str]]
|
The paths to save the tissue masks. If None, the masks are not saved. |
None
|
coords
|
List[Tuple[int, int, int, int]]
|
The XYWH coordinates of the image patch. If not None, the coordinates are saved in the filenames of outputs. |
None
|
class_dict_nuc
|
Dict[int, str]
|
The dictionary of panlei classes. E.g. {0: "bg", 1: "neoplastic"} |
None
|
class_dict_cyto
|
Dict[int, str]
|
The dictionary of cytoplasm classes. E.g. {0: "bg", 1: "macrophage_cyto"} |
None
|
class_dict_tissue
|
Dict[int, str]
|
The dictionary of tissue classes. E.g. {0: "bg", 1: "stroma", 2: "tumor"} |
None
|
nuc_smooth_func
|
Callable
|
The smoothing function to apply to the nuclei instance maps before post-processing. If None, no smoothing is applied. This is only used when nuclei segmentation masks are saved into vectorized format (e.g. parquet). Ignored save_paths_nuc is None. |
gaussian_smooth
|
cyto_smooth_func
|
Callable
|
The smoothing function to apply to the cytoplasm instance maps before post-processing. If None, no smoothing is applied. This is only used when cytoplasm segmentation masks are saved into vectorized format (e.g. parquet). Ignored save_paths_cyto is None. |
gaussian_smooth
|
tissue_smooth_func
|
Callable
|
The smoothing function to apply to the tissue type maps before post-processing. If None, no smoothing is applied. This is only used when tissue segmentation masks are saved into vectorized format (e.g. parquet). Ignored save_paths_tissue is None. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, List[ndarray]]
|
Dict[str, List[np.ndarray]]: Dictionary of post-processed outputs:
|
Examples:
>>> my_model.set_inference_mode()
>>> x = my_model.predict(x=image, use_sliding_win=False)
>>> x = my_model.post_process(
... x,
... use_async_postproc=True,
... start_method="threading",
... n_jobs=4,
... )
Source code in src/histolytics/models/_base_model.py
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 | |