HoVerNet¶
tiatoolbox.models.architecture.hovernet.HoVerNet
- class HoVerNet(num_input_channels=3, num_types=None, mode='original')[source]¶
HoVer-Net Architecture.
- Parameters
num_input_channels (int) – Number of channels in input.
num_types (int) – Number of nuclei types within the predictions. Once define, a branch dedicated for typing is created. By default, no typing (num_types=None) is used.
mode (str) – To use architecture defined in as in original paper (original) or the one used in PanNuke paper (fast).
References
Graham, Simon, et al. “Hover-net: Simultaneous segmentation and classification of nuclei in multi-tissue histology images.” Medical Image Analysis 58 (2019): 101563.
Gamper, Jevgenij, et al. “PanNuke dataset extension, insights and baselines.” arXiv preprint arXiv:2003.10778 (2020).
Methods
Logic for using layers defined in init.
Run inference on an input batch.
Post processing script for image tiles.
Attributes
- forward(imgs)[source]¶
Logic for using layers defined in init.
This method defines how layers are used in forward operation.
- Parameters
imgs (torch.Tensor) – Input images, the tensor is in the shape of NCHW.
- Returns
- A dictionary containing the inference output.
The expected format os {decoder_name: prediction}.
- Return type
output (dict)
- static infer_batch(model, batch_data, on_gpu)[source]¶
Run inference on an input batch.
This contains logic for forward operation as well as batch i/o aggregation.
- Parameters
model (nn.Module) – PyTorch defined model.
batch_data (ndarray) – a batch of data generated by torch.utils.data.DataLoader.
on_gpu (bool) – Whether to run inference on a GPU.
- Returns
List of output from each head, each head is expected to contain N predictions for N input patches. There are two cases, one with 2 heads (Nuclei Pixels np and Hover hv) or with 2 heads (np, hv, and Nuclei Types tp).
- static postproc(raw_maps)[source]¶
Post processing script for image tiles.
- Parameters
raw_maps (list(ndarray)) – list of prediction output of each head and assumed to be in the order of [np, hv, tp] (match with the output of infer_batch).
- Returns
- pixel-wise nuclear instance segmentation
prediction.
- inst_dict (dict): a dictionary containing a mapping of each instance
within inst_map instance information. It has following form
- inst_info = {
box: number[], centroids: number[], contour: number[][], type: number, prob: number,
} inst_dict = {[inst_uid: number] : inst_info}
and inst_uid is an integer corresponds to the instance having the same pixel value within inst_map.
- Return type
inst_map (ndarray)
Examples
>>> from tiatoolbox.models.architecture.hovernet import HoVerNet >>> import torch >>> import numpy as np >>> batch = torch.from_numpy(image_patch)[None] >>> # image_patch is a 256x256x3 numpy array >>> weights_path = "A/weights.pth" >>> pretrained = torch.load(weights_path) >>> model = HoVerNet(num_types=6, mode="fast") >>> model.load_state_dict(pretrained) >>> output = model.infer_batch(model, batch, on_gpu=False) >>> output = [v[0] for v in output] >>> output = model.postproc(output)