Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/.05-patch-prediction_TIAViz_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
597 changes: 403 additions & 194 deletions examples/05-patch-prediction.ipynb

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pre-commit/notebook_markdown_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def main(files: list[Path]) -> None:

"""
for path in files:
notebook = json.loads(path.read_text())
with Path.open(path, encoding="utf-8", errors="ignore") as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with Path.open(path, encoding="utf-8", errors="ignore") as f:
notebook = json.loads(path.read_text())

notebook = json.load(f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
notebook = json.load(f)

formatted_notebook = format_notebook(copy.deepcopy(notebook))
changed = any(
cell != formatted_cell
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ flask-cors>=4.0.0
glymur>=0.12.7
huggingface_hub>=0.33.3
imagecodecs>=2022.9.26
ipywidgets>=8.1.7
joblib>=1.1.1
jupyterlab>=3.5.2
matplotlib>=3.6.2
Expand Down
3 changes: 2 additions & 1 deletion tiatoolbox/models/engine/deep_feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ def infer_wsi(
probabilities_zarr, coordinates_zarr = None, None

probabilities_used_percent = 0
infer_batch = self._get_model_attr("infer_batch")
for batch_data in tqdm_loop:
batch_output = self.model.infer_batch(
batch_output = infer_batch(
self.model,
batch_data["image"],
device=self.device,
Expand Down
16 changes: 13 additions & 3 deletions tiatoolbox/models/engine/engine_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

if TYPE_CHECKING: # pragma: no cover
import os
from collections.abc import Callable

from torch.utils.data import DataLoader

Expand Down Expand Up @@ -374,6 +375,14 @@ def _initialize_model_ioconfig(

return model, None

def _get_model_attr(self: EngineABC, attr_name: str) -> Callable:
"""Return a model attribute, unwrapping DataParallel if required."""
try:
return getattr(self.model, attr_name)
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except AttributeError:
except AttributeError: # pragma: no cover

module = getattr(self.model, "module", None)
return getattr(module, attr_name)

def get_dataloader(
self: EngineABC,
images: str | Path | list[str | Path] | np.ndarray,
Expand Down Expand Up @@ -427,7 +436,7 @@ def get_dataloader(
auto_get_mask=auto_get_mask,
)

dataset.preproc_func = self.model.preproc_func
dataset.preproc_func = self._get_model_attr("preproc_func")

# preprocessing must be defined with the dataset
return torch.utils.data.DataLoader(
Expand All @@ -443,7 +452,7 @@ def get_dataloader(
inputs=images, labels=labels, patch_input_shape=ioconfig.patch_input_shape
)

dataset.preproc_func = self.model.preproc_func
dataset.preproc_func = self._get_model_attr("preproc_func")

# preprocessing must be defined with the dataset
return torch.utils.data.DataLoader(
Expand Down Expand Up @@ -528,8 +537,9 @@ def infer_patches(
else self.dataloader
)

infer_batch = self._get_model_attr("infer_batch")
for batch_data in tqdm_loop:
batch_output = self.model.infer_batch(
batch_output = infer_batch(
self.model,
batch_data["image"],
device=self.device,
Expand Down
3 changes: 2 additions & 1 deletion tiatoolbox/models/engine/patch_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ def post_process_patches(
_ = kwargs.get("return_probabilities")
_ = prediction_shape
_ = prediction_dtype
raw_predictions = self.model.postproc_func(raw_predictions)
postproc_func = self._get_model_attr("postproc_func")
raw_predictions = postproc_func(raw_predictions)
return cast_to_min_dtype(raw_predictions)

def post_process_wsi(
Expand Down
5 changes: 3 additions & 2 deletions tiatoolbox/models/engine/semantic_segmentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def get_dataloader(
auto_get_mask=auto_get_mask,
)

dataset.preproc_func = self.model.preproc_func
dataset.preproc_func = self._get_model_attr("preproc_func")
self.output_locations = dataset.outputs

# preprocessing must be defined with the dataset
Expand Down Expand Up @@ -477,8 +477,9 @@ def infer_wsi(
else dataloader.dataset.outputs
)

infer_batch = self._get_model_attr("infer_batch")
for batch_idx, batch_data in enumerate(tqdm_loop):
batch_output = self.model.infer_batch(
batch_output = infer_batch(
self.model,
batch_data["image"],
device=self.device,
Expand Down
Loading