Skip to content

Commit

Permalink
Merge branch 'main' into addBatchInfer
Browse files Browse the repository at this point in the history
  • Loading branch information
SachidanandAlle authored Oct 10, 2023
2 parents 02ad823 + 493a69c commit 28875b7
Show file tree
Hide file tree
Showing 28 changed files with 70 additions and 75 deletions.
4 changes: 2 additions & 2 deletions docs/source/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ in this example they follow the default behavior in the base class.
def train_pre_transforms(self, context: Context):
return Compose([
LoadImaged(keys=("image", "label")),
AsChannelFirstd(keys=("image", "label")),
EnsureChannelFirstd(keys=("image", "label")),
SpatialCropForegroundd(keys=("image", "label"), source_key="label", spatial_size=(128, 128, 128)),
NormalizeIntensityd(keys="image"),
])
Expand All @@ -148,7 +148,7 @@ in this example they follow the default behavior in the base class.
def val_pre_transforms(self, context: Context):
return Compose([
LoadImaged(keys=("image", "label")),
AsChannelFirstd(keys=("image", "label")),
EnsureChannelFirstd(keys=("image", "label")),
ScaleIntensityRanged(keys="image", a_min=-57, a_max=164, b_min=0.0, b_max=1.0, clip=True),
CropForegroundd(keys=("image", "label"), source_key="image"),
ToTensord(keys=("image", "label")),
Expand Down
4 changes: 2 additions & 2 deletions monailabel/datastore/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def dicom_to_nifti(series_dir, is_seg=False):
def binary_to_image(reference_image, label, dtype=np.uint16, file_ext=".nii.gz"):
start = time.time()

image_np, meta_dict = LoadImage()(reference_image)
image_np, meta_dict = LoadImage(image_only=False)(reference_image)
label_np = np.fromfile(label, dtype=dtype)

logger.info(f"Image: {image_np.shape}")
Expand All @@ -83,7 +83,7 @@ def binary_to_image(reference_image, label, dtype=np.uint16, file_ext=".nii.gz")
def nifti_to_dicom_seg(series_dir, label, label_info, file_ext="*", use_itk=True):
start = time.time()

label_np, meta_dict = LoadImage()(label)
label_np, meta_dict = LoadImage(image_only=False)(label)
unique_labels = np.unique(label_np.flatten()).astype(np.int_)
unique_labels = unique_labels[unique_labels != 0]

Expand Down
17 changes: 10 additions & 7 deletions monailabel/tasks/infer/basic_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import torch
from monai.data import decollate_batch
from monai.inferers import Inferer, SimpleInferer, SlidingWindowInferer
from monai.utils import deprecated

from monailabel.interfaces.exception import MONAILabelError, MONAILabelException
from monailabel.interfaces.tasks.infer_v2 import InferTask, InferType
Expand Down Expand Up @@ -150,12 +151,14 @@ def get_path(self, validate=True):
return path
return None

@deprecated(since="0.8.0", msg_suffix="This feature is not supported anymore")
def add_cache_transform(self, t, data, keys=("image", "image_meta_dict"), hash_key=("image_path", "model")):
if data and data.get("cache_transforms", False):
in_memory = data.get("cache_transforms_in_memory", True)
ttl = data.get("cache_transforms_ttl", 300)

t.append(CacheTransformDatad(keys=keys, hash_key=hash_key, in_memory=in_memory, ttl=ttl))
pass
# if data and data.get("cache_transforms", False):
# in_memory = data.get("cache_transforms_in_memory", True)
# ttl = data.get("cache_transforms_ttl", 300)
#
# t.append(CacheTransformDatad(keys=keys, hash_key=hash_key, in_memory=in_memory, ttl=ttl))

@abstractmethod
def pre_transforms(self, data=None) -> Sequence[Callable]:
Expand All @@ -168,7 +171,7 @@ def pre_transforms(self, data=None) -> Sequence[Callable]:
return [
monai.transforms.LoadImaged(keys='image'),
monai.transforms.AddChanneld(keys='image'),
monai.transforms.EnsureChannelFirstd(keys='image', channel_dim='no_channel'),
monai.transforms.Spacingd(keys='image', pixdim=[1.0, 1.0, 1.0]),
monai.transforms.ScaleIntensityRanged(keys='image',
a_min=-57, a_max=164, b_min=0.0, b_max=1.0, clip=True),
Expand Down Expand Up @@ -208,7 +211,7 @@ def post_transforms(self, data=None) -> Sequence[Callable]:
For Example::
return [
monai.transforms.AddChanneld(keys='pred'),
monai.transforms.EnsureChannelFirstd(keys='pred', channel_dim='no_channel'),
monai.transforms.Activationsd(keys='pred', softmax=True),
monai.transforms.AsDiscreted(keys='pred', argmax=True),
monai.transforms.SqueezeDimd(keys='pred', dim=0),
Expand Down
4 changes: 4 additions & 0 deletions monailabel/tasks/infer/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ def pre_transforms(self, data=None) -> Sequence[Callable]:
pre = list(c.transforms) if isinstance(c, Compose) else c
pre = self._filter_transforms(pre, self.pre_filter)

for t in pre:
if isinstance(t, LoadImaged):
t._loader.image_only = False

if pre and self.extend_load_image:
res = []
for t in pre:
Expand Down
2 changes: 1 addition & 1 deletion monailabel/utils/others/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def run(self, datastore):
pix_img_min = []
pix_img_mean = []
pix_img_std = []
loader = LoadImage()
loader = LoadImage(image_only=False)
for n in tqdm(datastore_check):
img, mtdt = loader(datastore.get_image_uri(n))

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

monai[nibabel, skimage, pillow, tensorboard, gdown, ignite, torchvision, itk, tqdm, lmdb, psutil, openslide, fire, mlflow]>=1.2.0
monai[nibabel, skimage, pillow, tensorboard, gdown, ignite, torchvision, itk, tqdm, lmdb, psutil, openslide, fire, mlflow]>=1.3.0rc3
uvicorn==0.21.1
pydantic>=1.10.7
python-dotenv==1.0.0
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/endoscopy/lib/configs/inbody.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def init(self, name: str, model_dir: str, conf: Dict[str, str], planner: Any, **
super().init(name, model_dir, conf, planner, **kwargs)

bundle_name = "endoscopic_inbody_classification"
version = conf.get("inbody", "0.3.7")
version = conf.get("inbody", "0.4.4")
zoo_source = conf.get("zoo_source", settings.MONAI_ZOO_SOURCE)

self.bundle_path = os.path.join(self.model_dir, bundle_name)
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/endoscopy/lib/configs/tooltracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def init(self, name: str, model_dir: str, conf: Dict[str, str], planner: Any, **
super().init(name, model_dir, conf, planner, **kwargs)

bundle_name = "endoscopic_tool_segmentation"
version = conf.get("tooltracking", "0.4.4")
version = conf.get("tooltracking", "0.5.5")
zoo_source = conf.get("zoo_source", settings.MONAI_ZOO_SOURCE)

self.bundle_path = os.path.join(self.model_dir, bundle_name)
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/endoscopy/lib/infers/deepedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(

def pre_transforms(self, data=None) -> Sequence[Callable]:
return [
LoadImageExd(keys="image", dtype=np.uint8),
LoadImageExd(keys="image", dtype=np.uint8, image_only=False),
EnsureTyped(keys="image", device=data.get("device") if data else None),
EnsureChannelFirstd(keys="image"),
Resized(keys="image", spatial_size=self.roi_size, mode="area"),
Expand Down
5 changes: 2 additions & 3 deletions sample-apps/endoscopy/lib/trainers/deepedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from monai.metrics import MeanIoU
from monai.transforms import (
Activationsd,
AddChanneld,
AsDiscreted,
EnsureChannelFirstd,
EnsureTyped,
Expand Down Expand Up @@ -88,9 +87,9 @@ def get_click_transforms(self, context: Context):

def train_pre_transforms(self, context: Context):
return [
LoadImaged(keys=("image", "label"), dtype=np.uint8),
LoadImaged(keys=("image", "label"), dtype=np.uint8, image_only=False),
EnsureChannelFirstd(keys="image"),
AddChanneld(keys="label"),
EnsureChannelFirstd(keys="label", channel_dim="no_channel"),
Resized(keys=("image", "label"), spatial_size=self.roi_size, mode=("area", "nearest")),
ToTensord(keys="image"),
TorchVisiond(
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/pathology/lib/configs/classification_nuclei.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def init(self, name: str, model_dir: str, conf: Dict[str, str], planner: Any, **

bundle_name = "pathology_nuclei_classification"
zoo_source = conf.get("zoo_source", settings.MONAI_ZOO_SOURCE)
version = conf.get("classification_nuclei", "0.1.0")
version = conf.get("classification_nuclei", "0.1.4")

self.bundle_path = os.path.join(self.model_dir, bundle_name)
if not os.path.exists(self.bundle_path):
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/pathology/lib/configs/hovernet_nuclei.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def init(self, name: str, model_dir: str, conf: Dict[str, str], planner: Any, **

bundle_name = "pathology_nuclei_segmentation_classification"
zoo_source = conf.get("zoo_source", settings.MONAI_ZOO_SOURCE)
version = conf.get("hovernet_nuclei", "0.1.8")
version = conf.get("hovernet_nuclei", "0.2.1")

self.bundle_path = os.path.join(self.model_dir, bundle_name)
if not os.path.exists(self.bundle_path):
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/pathology/lib/configs/nuclick.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def init(self, name: str, model_dir: str, conf: Dict[str, str], planner: Any, **

bundle_name = "pathology_nuclick_annotation"
zoo_source = conf.get("zoo_source", settings.MONAI_ZOO_SOURCE)
version = conf.get("nuclick", "0.1.1")
version = conf.get("nuclick", "0.1.4")

self.bundle_path = os.path.join(self.model_dir, bundle_name)
if not os.path.exists(self.bundle_path):
Expand Down
4 changes: 2 additions & 2 deletions sample-apps/pathology/lib/infers/segmentation_nuclei.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import numpy as np
from lib.transforms import LoadImagePatchd, PostFilterLabeld
from monai.transforms import Activationsd, AsChannelFirstd, AsDiscreted, ScaleIntensityRangeD, SqueezeDimd
from monai.transforms import Activationsd, AsDiscreted, ScaleIntensityRangeD, SqueezeDimd, Transposed

from monailabel.interfaces.tasks.infer_v2 import InferType
from monailabel.tasks.infer.basic_infer import BasicInferTask
Expand Down Expand Up @@ -60,7 +60,7 @@ def info(self) -> Dict[str, Any]:
def pre_transforms(self, data=None) -> Sequence[Callable]:
return [
LoadImagePatchd(keys="image", mode="RGB", dtype=np.uint8, padding=False),
AsChannelFirstd(keys="image"),
Transposed(keys="image", indices=[2, 0, 1]),
ScaleIntensityRangeD(keys="image", a_min=0.0, a_max=255.0, b_min=-1.0, b_max=1.0),
]

Expand Down
4 changes: 1 addition & 3 deletions sample-apps/radiology/lib/infers/deepedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ def __init__(

def pre_transforms(self, data=None):
t = [
LoadImaged(keys="image", reader="ITKReader"),
LoadImaged(keys="image", reader="ITKReader", image_only=False),
EnsureChannelFirstd(keys="image"),
Orientationd(keys="image", axcodes="RAS"),
ScaleIntensityRanged(keys="image", a_min=-175, a_max=250, b_min=0.0, b_max=1.0, clip=True),
]

self.add_cache_transform(t, data)

if self.type == InferType.DEEPEDIT:
t.extend(
[
Expand Down
14 changes: 6 additions & 8 deletions sample-apps/radiology/lib/infers/deepgrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
from monai.inferers import Inferer, SimpleInferer
from monai.transforms import (
Activationsd,
AddChanneld,
AsChannelFirstd,
AsChannelLastd,
AsDiscreted,
EnsureChannelFirstd,
EnsureTyped,
LoadImaged,
NormalizeIntensityd,
Resized,
Spacingd,
ToNumpyd,
Transposed,
)

from monailabel.interfaces.tasks.infer_v2 import InferType
Expand Down Expand Up @@ -72,19 +72,17 @@ def __init__(

def pre_transforms(self, data=None) -> Sequence[Callable]:
t = [
LoadImaged(keys="image"),
AsChannelFirstd(keys="image"),
LoadImaged(keys="image", image_only=False),
Transposed(keys="image", indices=[2, 0, 1]),
Spacingd(keys="image", pixdim=[1.0] * self.dimension, mode="bilinear"),
AddGuidanceFromPointsd(ref_image="image", guidance="guidance", spatial_dims=self.dimension),
]

self.add_cache_transform(t, data)
t.append(AddGuidanceFromPointsd(ref_image="image", guidance="guidance", spatial_dims=self.dimension))

if self.dimension == 2:
t.append(Fetch2DSliced(keys="image", guidance="guidance"))
t.extend(
[
AddChanneld(keys="image"),
EnsureChannelFirstd(keys="image", channel_dim="no_channel"),
SpatialCropGuidanced(keys="image", guidance="guidance", spatial_size=self.spatial_size),
Resized(keys="image", spatial_size=self.model_size, mode="area"),
ResizeGuidanced(guidance="guidance", ref_image="image"),
Expand Down
33 changes: 13 additions & 20 deletions sample-apps/radiology/lib/infers/deepgrow_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
)
from monai.inferers import Inferer, SimpleInferer
from monai.transforms import (
AddChanneld,
AsChannelFirst,
AsChannelFirstd,
AsChannelLastd,
EnsureChannelFirstd,
LoadImage,
LoadImaged,
NormalizeIntensityd,
Resized,
Spacingd,
Transpose,
Transposed,
)

from monailabel.interfaces.tasks.infer_v2 import InferTask, InferType
Expand Down Expand Up @@ -82,24 +82,17 @@ def __init__(

def pre_transforms(self, data=None) -> Sequence[Callable]:
t = [
LoadImaged(keys="image"),
AsChannelFirstd(keys="image"),
LoadImaged(keys="image", image_only=False),
Transposed(keys="image", indices=[2, 0, 1]),
Spacingd(keys="image", pixdim=[1.0, 1.0, 1.0], mode="bilinear"),
AddGuidanceFromPointsd(ref_image="image", guidance="guidance", spatial_dims=3),
EnsureChannelFirstd(keys="image", channel_dim="no_channel"),
SpatialCropGuidanced(keys="image", guidance="guidance", spatial_size=self.spatial_size),
Resized(keys="image", spatial_size=self.model_size, mode="area"),
ResizeGuidanced(guidance="guidance", ref_image="image"),
NormalizeIntensityd(keys="image", subtrahend=208, divisor=388),
AddGuidanceSignald(image="image", guidance="guidance"),
]

self.add_cache_transform(t, data)

t.extend(
[
AddGuidanceFromPointsd(ref_image="image", guidance="guidance", spatial_dims=3),
AddChanneld(keys="image"),
SpatialCropGuidanced(keys="image", guidance="guidance", spatial_size=self.spatial_size),
Resized(keys="image", spatial_size=self.model_size, mode="area"),
ResizeGuidanced(guidance="guidance", ref_image="image"),
NormalizeIntensityd(keys="image", subtrahend=208, divisor=388),
AddGuidanceSignald(image="image", guidance="guidance"),
]
)
return t

def inferer(self, data=None) -> Inferer:
Expand All @@ -117,7 +110,7 @@ def __call__(self, request):
result_file, result_json = self.model_3d(request)

label = LoadImage(image_only=True)(result_file)
label = AsChannelFirst()(label)
label = Transpose(indices=[2, 0, 1])(label)
logger.debug(f"Label shape: {label.shape}")

foreground, slices = self.get_slices_points(label, request.get("foreground", []))
Expand Down
4 changes: 0 additions & 4 deletions sample-apps/radiology/lib/infers/segmentation_vertebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ def pre_transforms(self, data=None) -> Sequence[Callable]:
]
)

# Support caching for deepgrow interactions from the client
if add_cache:
self.add_cache_transform(t, data)

t.extend(
[
CropAndCreateSignald(keys="image", signal_key="signal"),
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/radiology/lib/trainers/deepedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_click_transforms(self, context: Context):

def train_pre_transforms(self, context: Context):
return [
LoadImaged(keys=("image", "label"), reader="ITKReader"),
LoadImaged(keys=("image", "label"), reader="ITKReader", image_only=False),
EnsureChannelFirstd(keys=("image", "label")),
NormalizeLabelsInDatasetd(keys="label", label_names=self._labels),
Orientationd(keys=["image", "label"], axcodes="RAS"),
Expand Down
6 changes: 3 additions & 3 deletions sample-apps/radiology/lib/trainers/deepgrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
from monai.losses import DiceLoss
from monai.transforms import (
Activationsd,
AddChanneld,
AsDiscreted,
EnsureChannelFirstd,
EnsureTyped,
LoadImaged,
NormalizeIntensityd,
Expand Down Expand Up @@ -115,8 +115,8 @@ def get_click_transforms(self, context: Context):
def train_pre_transforms(self, context: Context):
# Dataset preparation
t: List[Any] = [
LoadImaged(keys=("image", "label")),
AddChanneld(keys=("image", "label")),
LoadImaged(keys=("image", "label"), image_only=False),
EnsureChannelFirstd(keys=("image", "label")),
SpatialCropForegroundd(keys=("image", "label"), source_key="label", spatial_size=self.roi_size),
Resized(keys=("image", "label"), spatial_size=self.model_size, mode=("area", "nearest")),
NormalizeIntensityd(keys="image", subtrahend=208.0, divisor=388.0), # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ setup_requires =
torch
ninja
install_requires =
monai[nibabel, skimage, pillow, tensorboard, gdown, ignite, torchvision, itk, tqdm, lmdb, psutil, openslide, fire, mlflow]>=1.2.0
monai[nibabel, skimage, pillow, tensorboard, gdown, ignite, torchvision, itk, tqdm, lmdb, psutil, openslide, fire, mlflow]>=1.3.0rc3
uvicorn==0.21.1
pydantic>=1.10.7
python-dotenv==1.0.0
Expand Down
1 change: 1 addition & 0 deletions tests/integration/endoscopy/test_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_tooltracking(self):
response = requests.post(f"{SERVER_URI}/infer/{model}?image={image}")
assert response.status_code == 200

@unittest.skip("Bundle needs to be fixed for AsChannelFirstd")
def test_inbody(self):
if not torch.cuda.is_available():
return
Expand Down
Loading

0 comments on commit 28875b7

Please sign in to comment.