Skip to content

Commit

Permalink
Remove deprecated AsChannelFirst and AddChannel (#1527)
Browse files Browse the repository at this point in the history
### Description
After Project-MONAI/MONAI#7019, `AsChannelFirst`
and `AddChannel` have been deprecated, remove the usage in the tutorial.

### Checks
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [ ] Avoid including large-size files in the PR.
- [ ] Clean up long text outputs from code cells in the notebook.
- [ ] For security purposes, please check the contents and remove any
sensitive info such as user names and private key.
- [ ] Ensure (1) hyperlinks and markdown anchors are working (2) use
relative paths for tutorial repo files (3) put figure and graphs in the
`./figure` folder
- [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>`

---------

Signed-off-by: KumoLiu <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
KumoLiu and pre-commit-ci[bot] authored Sep 25, 2023
1 parent 0b2638b commit 19b42c9
Show file tree
Hide file tree
Showing 34 changed files with 651 additions and 610 deletions.
4 changes: 2 additions & 2 deletions acceleration/distributed_training/unet_evaluation_ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from monai.data import DataLoader, Dataset, create_test_image_3d, DistributedSampler, decollate_batch
from monai.inferers import sliding_window_inference
from monai.metrics import DiceMetric
from monai.transforms import Activations, AsChannelFirstd, AsDiscrete, Compose, LoadImaged, ScaleIntensityd
from monai.transforms import Activations, EnsureChannelFirstd, AsDiscrete, Compose, LoadImaged, ScaleIntensityd


def evaluate(args):
Expand Down Expand Up @@ -90,7 +90,7 @@ def evaluate(args):
val_transforms = Compose(
[
LoadImaged(keys=["img", "seg"]),
AsChannelFirstd(keys=["img", "seg"], channel_dim=-1),
EnsureChannelFirstd(keys=["img", "seg"], channel_dim=-1),
ScaleIntensityd(keys="img"),
]
)
Expand Down
12 changes: 10 additions & 2 deletions acceleration/distributed_training/unet_evaluation_horovod.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@
from monai.data import DataLoader, Dataset, create_test_image_3d, decollate_batch
from monai.inferers import sliding_window_inference
from monai.metrics import DiceMetric
from monai.transforms import Activations, AsChannelFirstd, AsDiscrete, Compose, LoadImaged, ScaleIntensityd, EnsureType
from monai.transforms import (
Activations,
EnsureChannelFirstd,
AsDiscrete,
Compose,
LoadImaged,
ScaleIntensityd,
EnsureType,
)


def evaluate(args):
Expand Down Expand Up @@ -86,7 +94,7 @@ def evaluate(args):
val_transforms = Compose(
[
LoadImaged(keys=["img", "seg"]),
AsChannelFirstd(keys=["img", "seg"], channel_dim=-1),
EnsureChannelFirstd(keys=["img", "seg"], channel_dim=-1),
ScaleIntensityd(keys="img"),
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from monai.inferers import SlidingWindowInferer
from monai.transforms import (
Activationsd,
AsChannelFirstd,
EnsureChannelFirstd,
AsDiscreted,
Compose,
KeepLargestConnectedComponentd,
Expand Down Expand Up @@ -110,7 +110,7 @@ def evaluate(args):
val_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
AsChannelFirstd(keys=["image", "label"], channel_dim=-1),
EnsureChannelFirstd(keys=["image", "label"], channel_dim=-1),
ScaleIntensityd(keys="image"),
]
)
Expand Down
4 changes: 2 additions & 2 deletions acceleration/distributed_training/unet_training_ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import monai
from monai.data import DataLoader, Dataset, create_test_image_3d, DistributedSampler
from monai.transforms import (
AsChannelFirstd,
EnsureChannelFirstd,
Compose,
LoadImaged,
RandCropByPosNegLabeld,
Expand Down Expand Up @@ -99,7 +99,7 @@ def train(args):
train_transforms = Compose(
[
LoadImaged(keys=["img", "seg"]),
AsChannelFirstd(keys=["img", "seg"], channel_dim=-1),
EnsureChannelFirstd(keys=["img", "seg"], channel_dim=-1),
ScaleIntensityd(keys="img"),
RandCropByPosNegLabeld(
keys=["img", "seg"], label_key="seg", spatial_size=[96, 96, 96], pos=1, neg=1, num_samples=4
Expand Down
4 changes: 2 additions & 2 deletions acceleration/distributed_training/unet_training_horovod.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import monai
from monai.data import DataLoader, Dataset, create_test_image_3d
from monai.transforms import (
AsChannelFirstd,
EnsureChannelFirstd,
Compose,
LoadImaged,
RandCropByPosNegLabeld,
Expand Down Expand Up @@ -99,7 +99,7 @@ def train(args):
train_transforms = Compose(
[
LoadImaged(keys=["img", "seg"]),
AsChannelFirstd(keys=["img", "seg"], channel_dim=-1),
EnsureChannelFirstd(keys=["img", "seg"], channel_dim=-1),
ScaleIntensityd(keys="img"),
RandCropByPosNegLabeld(
keys=["img", "seg"], label_key="seg", spatial_size=[96, 96, 96], pos=1, neg=1, num_samples=4
Expand Down
4 changes: 2 additions & 2 deletions acceleration/distributed_training/unet_training_smartcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
import monai
from monai.data import DataLoader, SmartCacheDataset, create_test_image_3d, partition_dataset
from monai.transforms import (
AsChannelFirstd,
EnsureChannelFirstd,
Compose,
LoadImaged,
RandCropByPosNegLabeld,
Expand Down Expand Up @@ -105,7 +105,7 @@ def train(args):
train_transforms = Compose(
[
LoadImaged(keys=["img", "seg"]),
AsChannelFirstd(keys=["img", "seg"], channel_dim=-1),
EnsureChannelFirstd(keys=["img", "seg"], channel_dim=-1),
ScaleIntensityd(keys="img"),
RandCropByPosNegLabeld(
keys=["img", "seg"], label_key="seg", spatial_size=[96, 96, 96], pos=1, neg=1, num_samples=4
Expand Down
4 changes: 2 additions & 2 deletions acceleration/distributed_training/unet_training_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from monai.inferers import SimpleInferer
from monai.transforms import (
Activationsd,
AsChannelFirstd,
EnsureChannelFirstd,
AsDiscreted,
Compose,
KeepLargestConnectedComponentd,
Expand Down Expand Up @@ -111,7 +111,7 @@ def train(args):
train_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
AsChannelFirstd(keys=["image", "label"], channel_dim=-1),
EnsureChannelFirstd(keys=["image", "label"], channel_dim=-1),
ScaleIntensityd(keys="image"),
RandCropByPosNegLabeld(
keys=["image", "label"], label_key="label", spatial_size=[96, 96, 96], pos=1, neg=1, num_samples=4
Expand Down
4 changes: 2 additions & 2 deletions acceleration/threadbuffer_performance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"from monai.data import DataLoader, Dataset, ThreadBuffer, create_test_image_2d\n",
"from monai.losses import Dice\n",
"from monai.networks.nets import UNet\n",
"from monai.transforms import AddChanneld, Compose, MapTransform\n",
"from monai.transforms import EnsureChannelFirstd, Compose, MapTransform\n",
"\n",
"monai.utils.set_determinism(seed=0)\n",
"\n",
Expand Down Expand Up @@ -99,7 +99,7 @@
"trans = Compose(\n",
" [\n",
" RandomGenerator(keys=(\"im\", \"seg\")),\n",
" AddChanneld(keys=(\"im\", \"seg\")),\n",
" EnsureChannelFirstd(keys=(\"im\", \"seg\"), channel_dim=\"no_channel\"),\n",
" ]\n",
")\n",
"\n",
Expand Down
3 changes: 0 additions & 3 deletions automl/DiNTS/search_dints.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@
Transform,
AsDiscrete,
AsDiscreted,
AddChannel,
AddChanneld,
AsChannelFirstd,
CastToTyped,
Compose,
ConcatItemsd,
Expand Down
3 changes: 0 additions & 3 deletions automl/DiNTS/train_dints.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@
Transform,
AsDiscrete,
AsDiscreted,
AddChannel,
AddChanneld,
AsChannelFirstd,
CastToTyped,
Compose,
ConcatItemsd,
Expand Down
12 changes: 6 additions & 6 deletions deepgrow/ignite/inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
" SpatialCropGuidanced,\n",
")\n",
"from monai.transforms import (\n",
" AsChannelFirstd,\n",
" EnsureChannelFirstd,\n",
" Spacingd,\n",
" LoadImaged,\n",
" AddChanneld,\n",
" Transposed,\n",
" NormalizeIntensityd,\n",
" EnsureTyped,\n",
" ToNumpyd,\n",
Expand Down Expand Up @@ -449,8 +449,8 @@
"slice_idx = original_slice_idx = data[\"foreground\"][0][2]\n",
"\n",
"pre_transforms = [\n",
" LoadImaged(keys=\"image\"),\n",
" AsChannelFirstd(keys=\"image\"),\n",
" LoadImaged(keys=\"image\", image_only=False),\n",
" Transposed(keys=\"image\", indices=[2, 0, 1]),\n",
" Spacingd(keys=\"image\", pixdim=pixdim, mode=\"bilinear\"),\n",
" AddGuidanceFromPointsd(\n",
" ref_image=\"image\",\n",
Expand All @@ -460,7 +460,7 @@
" spatial_dims=dimensions,\n",
" ),\n",
" Fetch2DSliced(keys=\"image\", guidance=\"guidance\"),\n",
" AddChanneld(keys=\"image\"),\n",
" EnsureChannelFirstd(keys=\"image\", channel_dim=\"no_channel\"),\n",
" SpatialCropGuidanced(keys=\"image\", guidance=\"guidance\", spatial_size=roi_size),\n",
" Resized(keys=\"image\", spatial_size=roi_size, mode=\"area\"),\n",
" ResizeGuidanced(guidance=\"guidance\", ref_image=\"image\"),\n",
Expand Down Expand Up @@ -500,7 +500,7 @@
" slice_idx = 0\n",
" if tname == \"LoadImaged\":\n",
" original_image = data[\"image\"]\n",
" if tname == \"AddChanneld\":\n",
" if tname == \"EnsureChannelFirstd\":\n",
" original_image_slice = data[\"image\"]"
]
},
Expand Down
16 changes: 8 additions & 8 deletions deepgrow/ignite/inference_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
" SpatialCropGuidanced,\n",
")\n",
"from monai.transforms import (\n",
" AsChannelFirstd,\n",
" EnsureChannelFirstd,\n",
" Spacingd,\n",
" LoadImaged,\n",
" AddChanneld,\n",
" Transposed,\n",
" NormalizeIntensityd,\n",
" EnsureTyped,\n",
" ToNumpyd,\n",
Expand Down Expand Up @@ -177,8 +177,8 @@
"slice_idx = original_slice_idx = data[\"foreground\"][0][2]\n",
"\n",
"pre_transforms = [\n",
" LoadImaged(keys=\"image\"),\n",
" AsChannelFirstd(keys=\"image\"),\n",
" LoadImaged(keys=\"image\", image_only=False),\n",
" Transposed(keys=\"image\", indices=[2, 0, 1]),\n",
" Spacingd(keys=\"image\", pixdim=pixdim, mode=\"bilinear\"),\n",
" AddGuidanceFromPointsd(\n",
" ref_image=\"image\",\n",
Expand All @@ -187,7 +187,7 @@
" background=\"background\",\n",
" spatial_dims=dimensions,\n",
" ),\n",
" AddChanneld(keys=\"image\"),\n",
" EnsureChannelFirstd(keys=\"image\", channel_dim=\"no_channel\"),\n",
" SpatialCropGuidanced(keys=\"image\", guidance=\"guidance\", spatial_size=roi_size),\n",
" Resized(keys=\"image\", spatial_size=model_size, mode=\"area\"),\n",
" ResizeGuidanced(guidance=\"guidance\", ref_image=\"image\"),\n",
Expand Down Expand Up @@ -216,15 +216,15 @@
" image[:, :, slice_idx]\n",
" if tname in (\"LoadImaged\")\n",
" else image[slice_idx]\n",
" if tname in (\"AsChannelFirstd\", \"Spacingd\", \"AddGuidanceFromPointsd\")\n",
" if tname in (\"Transposed\", \"Spacingd\", \"AddGuidanceFromPointsd\")\n",
" else image[0][slice_idx]\n",
" )\n",
" label = None\n",
"\n",
" show_image(image, label, guidance, slice_idx)\n",
" if tname == \"LoadImaged\":\n",
" original_image = data[\"image\"]\n",
" if tname == \"AddChanneld\":\n",
" if tname == \"EnsureChannelFirstd\":\n",
" original_image_slice = data[\"image\"]\n",
" if tname == \"SpatialCropGuidanced\":\n",
" spatial_image = data[\"image\"]\n",
Expand Down Expand Up @@ -367,7 +367,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.8.10"
},
"vscode": {
"interpreter": {
Expand Down
4 changes: 2 additions & 2 deletions deepgrow/ignite/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from monai.networks.nets import BasicUNet, UNet
from monai.transforms import (
Activationsd,
AddChanneld,
EnsureChannelFirstd,
AsDiscreted,
Compose,
EnsureTyped,
Expand Down Expand Up @@ -91,7 +91,7 @@ def get_network(network, channels, dimensions):
def get_pre_transforms(roi_size, model_size, dimensions):
t = [
LoadImaged(keys=("image", "label")),
AddChanneld(keys=("image", "label")),
EnsureChannelFirstd(keys=("image", "label"), channel_dim="no_channel"),
SpatialCropForegroundd(keys=("image", "label"), source_key="label", spatial_size=roi_size),
Resized(keys=("image", "label"), spatial_size=model_size, mode=("area", "nearest")),
NormalizeIntensityd(keys="image", subtrahend=208.0, divisor=388.0),
Expand Down
6 changes: 3 additions & 3 deletions deployment/Triton/models/mednist_class/1/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from monai.transforms import Compose
from monai.transforms import (
Activations,
AddChannel,
EnsureChannelFirst,
AsDiscrete,
CropForeground,
CastToType,
Expand Down Expand Up @@ -121,8 +121,8 @@ def initialize(self, args):
[
LoadImage(reader="PILReader", image_only=True, dtype=np.float32),
ScaleIntensity(),
AddChannel(),
AddChannel(),
EnsureChannelFirst(channel_dim="no_channel"),
EnsureChannelFirst(channel_dim="no_channel"),
ToTensor(),
Lambda(func=lambda x: x.to(device=self.inference_device)),
]
Expand Down
6 changes: 3 additions & 3 deletions deployment/Triton/models/monai_covid/1/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from monai.transforms import Compose
from monai.transforms import (
Activations,
AddChannel,
EnsureChannelFirst,
AsDiscrete,
CropForeground,
LoadImage,
Expand Down Expand Up @@ -110,11 +110,11 @@ def initialize(self, args):
self.pre_transforms = Compose(
[
LoadImage(reader="NibabelReader", image_only=True, dtype=np.float32),
AddChannel(),
EnsureChannelFirst(channel_dim="no_channel"),
ScaleIntensityRange(a_min=-1000, a_max=500, b_min=0.0, b_max=1.0, clip=True),
CropForeground(margin=5),
Resize([192, 192, 64], mode="area"),
AddChannel(),
EnsureChannelFirst(channel_dim="no_channel"),
ToTensor(),
Lambda(func=lambda x: x.to(device=self.inference_device)),
]
Expand Down
4 changes: 2 additions & 2 deletions deployment/bentoml/mednist_classifier_bentoml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"import torch\n",
"\n",
"from monai.transforms import (\n",
" AddChannel,\n",
" EnsureChannelFirst,\n",
" Compose,\n",
" Transform,\n",
" ScaleIntensity,\n",
Expand Down Expand Up @@ -414,7 +414,7 @@
"class MedNISTClassifier(bentoml.BentoService):\n",
" @cached_property\n",
" def transform(self):\n",
" return Compose([LoadStreamPIL(\"L\"), AddChannel(), ScaleIntensity(), EnsureType()])\n",
" return Compose([LoadStreamPIL(\"L\"), EnsureChannelFirst(channel_dim=\"no_channel\"), ScaleIntensity(), EnsureType()])\n",
"\n",
" @bentoml.api(input=FileInput(), output=JsonOutput(), batch=True)\n",
" def predict(self, file_streams: List[BinaryIO]) -> List[str]:\n",
Expand Down
8 changes: 4 additions & 4 deletions deployment/ray/mednist_classifier_ray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"from monai.apps import download_url\n",
"from monai.config import print_config\n",
"from monai.transforms import (\n",
" AddChannel,\n",
" EnsureChannelFirst,\n",
" Compose,\n",
" ScaleIntensity,\n",
" EnsureType,\n",
Expand Down Expand Up @@ -148,7 +148,7 @@
"class MedNISTClassifier:\n",
" def __init__(self):\n",
" # create the transform for normalizing the image data\n",
" self.transform = Compose([AddChannel(), ScaleIntensity(), EnsureType()])\n",
" self.transform = Compose([EnsureChannelFirst(channel_dim=\"no_channel\"), ScaleIntensity(), EnsureType()])\n",
" # load the network on the CPU for simplicity and in eval mode\n",
" self.net = torch.jit.load(\"../bentoml/classifier.zip\", map_location=\"cpu\").eval()\n",
"\n",
Expand Down Expand Up @@ -358,7 +358,7 @@
"\n",
"from monai.config import print_config\n",
"from monai.transforms import (\n",
" AddChannel,\n",
" EnsureChannelFirst,\n",
" Compose,\n",
" ScaleIntensity,\n",
" EnsureType,\n",
Expand All @@ -371,7 +371,7 @@
"@serve.deployment\n",
"class MedNISTClassifier:\n",
" def __init__(self):\n",
" self.transform = Compose([AddChannel(), ScaleIntensity(), EnsureType()])\n",
" self.transform = Compose([EnsureChannelFirst(channel_dim=\"no_channel\"), ScaleIntensity(), EnsureType()])\n",
" self.net = torch.jit.load(\"../bentoml/classifier.zip\", map_location=\"cpu\").eval()\n",
"\n",
" async def __call__(self, request):\n",
Expand Down
Loading

0 comments on commit 19b42c9

Please sign in to comment.