Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Scribbles optimisation to numpymaxflow #751

Merged
Merged
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ with [MONAI](https://github.com/Project-MONAI).

## Installation

**MONAI Label requires PyTorch version 1.10.0 or newer.**

MONAI Label supports following OS with **GPU/CUDA** enabled.

- Ubuntu
Expand Down
2 changes: 1 addition & 1 deletion monailabel/scribbles/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HistogramBasedGraphCut(InferTask):
indicating foreground and background regions. A likelihood volume is generated using histogram method.
User-scribbles are incorporated using Equation 7 on page 4 of the paper.

SimpleCRF's GraphCut layer is used to optimise Equation 5 from the paper, where unaries come from Equation 7
numpymaxflow's GraphCut layer is used to optimise Equation 5 from the paper, where unaries come from Equation 7
and pairwise is the original input volume.
"""

Expand Down
11 changes: 4 additions & 7 deletions monailabel/scribbles/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class ApplyGraphCutOptimisationd(InteractiveSegmentationTransform):

This can be used in conjuction with any Make*Unaryd transform
(e.g. MakeISegUnaryd from above for implementing ISeg unary term).
It optimises a typical energy function for interactive segmentation methods using SimpleCRF's GraphCut method,
It optimises a typical energy function for interactive segmentation methods using numpymaxflow's GraphCut method,
e.g. Equation 5 from https://arxiv.org/pdf/1710.04043.pdf.

Usage Example::
Expand Down Expand Up @@ -401,15 +401,12 @@ def __call__(self, data):
if unary_term.shape[0] > 2:
raise ValueError(f"GraphCut can only be applied to binary probabilities, received {unary_term.shape[0]}")

# # attempt to unfold probability term
# attempt to unfold probability term
# unary_term = self._unfold_prob(unary_term, axis=0)

# prepare data for SimpleCRF's GraphCut
unary_term = torch.from_numpy(unary_term).unsqueeze(0)
pairwise_term = torch.from_numpy(pairwise_term).unsqueeze(0)

# prepare data for numpymaxflow's GraphCut
# run GraphCut
post_proc_label = maxflow(pairwise_term, unary_term, lamda=self.lamda, sigma=self.sigma).squeeze(0).numpy()
post_proc_label = maxflow(pairwise_term, unary_term, lamda=self.lamda, sigma=self.sigma)

d[self.post_proc_label] = post_proc_label

Expand Down
8 changes: 2 additions & 6 deletions monailabel/scribbles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
import logging

import numpy as np
from monai.utils import optional_import

# torch import is needed to execute torchmaxflow
optional_import("torch")
import torchmaxflow
import numpymaxflow

logger = logging.getLogger(__name__)

Expand All @@ -27,7 +23,7 @@ def get_eps(data):
def maxflow(image, prob, lamda=5, sigma=0.1):
# lamda: weight of smoothing term
# sigma: std of intensity values
return torchmaxflow.maxflow(image, prob, lamda, sigma)
return numpymaxflow.maxflow(image, prob, lamda, sigma)


def make_iseg_unary(
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ openslide-python==1.1.2
opencv-python-headless==4.5.5.64
Shapely==1.8.1.post1
girder_client==3.1.8
torchmaxflow==0.0.6rc1
numpymaxflow==0.0.2

#sudo apt-get install openslide-tools -y
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ install_requires =
opencv-python-headless==4.5.5.64
Shapely==1.8.1.post1
girder_client==3.1.8
torchmaxflow==0.0.6rc1
numpymaxflow==0.0.2

[flake8]
select = B,C,E,F,N,P,T4,W,B9
Expand Down