CellposePanoptic
CellposePanoptic ¶
Bases: BaseModelPanoptic
Source code in src/histolytics/models/cellpose_panoptic.py
set_inference_mode ¶
set_inference_mode(
mixed_precision: bool = True,
postproc_kwargs: Dict[str, Any] = {"use_gpu": True},
) -> None
Set to model inference mode.
Source code in src/histolytics/models/cellpose_panoptic.py
from_pretrained
classmethod
¶
from_pretrained(
weights: Union[str, Path],
device: device = torch.device("cuda"),
model_kwargs: Dict[str, Any] = {},
) -> BaseModelPanoptic
Load the model from pretrained weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Union[str, Path]
|
Registered checkpoint name or local checkpoint path. |
required |
device
|
device
|
Device to load and run the model on. Defaults to CUDA. |
device('cuda')
|
model_kwargs
|
Dict[str, Any]
|
Additional arguments for the model. |
{}
|
Returns:
| Type | Description |
|---|---|
BaseModelPanoptic
|
A model with strictly loaded weights. Registered checkpoints provide |
BaseModelPanoptic
|
independent nuclei/tissue class dictionaries on the returned instance; |
BaseModelPanoptic
|
local checkpoints have None mappings. Loading or customizing another |
BaseModelPanoptic
|
model does not change these dictionaries. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If weights is neither a file nor a registered name. Checkpoint loading and model construction errors propagate. |
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, SoftSemanticOutput | SoftInstanceOutput | None
],
*,
use_async_postproc: bool = True,
start_method: str = "threading",
n_jobs: int = 4,
save_paths_nuc: list[Path | str] | None = None,
save_paths_cyto: list[Path | str] | None = None,
save_paths_tissue: list[Path | str] | None = None,
coords: list[tuple[int, int, int, int]] | None = None,
class_dict_nuc: dict[int, str] | None = None,
class_dict_cyto: dict[int, str] | None = None,
class_dict_tissue: dict[int, str] | None = None,
nuc_smooth_func: Callable | None = gaussian_smooth,
cyto_smooth_func: Callable | None = gaussian_smooth,
tissue_smooth_func: Callable | None = None,
) -> dict[str, list[Any] | None]
Reconstruct labels from predictor outputs and optionally save geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, SoftSemanticOutput | SoftInstanceOutput | None]
|
Predictor output with "nuc", "cyto", and "tissue" keys. This model facade requires nuclei and tissue maps; cytoplasm may be None. Type maps contain class IDs with shape (B, H, W), not logits. Instance auxiliary maps have shape (B, C_aux, H, W), where C_aux and values depend on the reconstruction algorithm. Optional binary maps contain foreground probabilities with shape (B, 1, H, W). The upstream processor handles tensor detachment and CPU conversion. |
required |
use_async_postproc
|
bool
|
Use asynchronous processing for batches larger than one. Single-image batches always run serially. |
True
|
start_method
|
str
|
Worker backend: "threading", "fork", or "spawn". |
'threading'
|
n_jobs
|
int
|
Number of workers for parallel processing. |
4
|
save_paths_nuc
|
list[Path | str] | None
|
One destination per image for nuclei geometry, or None to return reconstructed nuclei arrays. |
None
|
save_paths_cyto
|
list[Path | str] | None
|
Cytoplasm destinations, or None to return arrays. |
None
|
save_paths_tissue
|
list[Path | str] | None
|
Tissue destinations, or None to return arrays. |
None
|
coords
|
list[tuple[int, int, int, int]] | None
|
Per-image (x, y, width, height) in pixels. Vector output is translated by x and y; no physical-unit conversion is performed. |
None
|
class_dict_nuc
|
dict[int, str] | None
|
Nuclei class-ID to class-name mapping for vector output. |
None
|
class_dict_cyto
|
dict[int, str] | None
|
Cytoplasm class mapping for vector output. |
None
|
class_dict_tissue
|
dict[int, str] | None
|
Tissue class mapping for vector output. |
None
|
nuc_smooth_func
|
Callable | None
|
Geometry callback for saved nuclei polygons. None disables smoothing; ignored for in-memory array outputs. Async callbacks must be pickleable, for example module-level functions. |
gaussian_smooth
|
cyto_smooth_func
|
Callable | None
|
Geometry callback for saved cytoplasm polygons. |
gaussian_smooth
|
tissue_smooth_func
|
Callable | None
|
Geometry callback for saved tissue polygons. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, list[Any] | None]
|
The upstream processor result, unchanged, under "nuc", "cyto", and |
dict[str, list[Any] | None]
|
"tissue". In serial/synchronous processing, instance results are lists |
dict[str, list[Any] | None]
|
of (instance labels, class labels) int32 arrays with shape (H, W), and |
dict[str, list[Any] | None]
|
tissue results are lists of int32 class-label arrays. Absent outputs |
dict[str, list[Any] | None]
|
are None; saved entries are None instead of arrays. The published |
dict[str, list[Any] | None]
|
asynchronous processor's cytoplasm return is not normalized here. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If inference mode has not been enabled. Processor and file-writing errors propagate to the caller. |
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
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 | |