Comments (5)
Hi @tararelan, could you please share the whole error message?
At first glance, I can only wonder if you ensured that the image and label sizes entered are consistent.
from monai.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
140 return [_apply_transform(transform, item, unpack_items, lazy, overrides, log_stats) for item in data]
--> 141 return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
142 except Exception as e:
15 frames
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in _apply_transform(transform, data, unpack_parameters, lazy, overrides, logger_name)
97
---> 98 return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
99
[/usr/local/lib/python3.10/dist-packages/monai/transforms/croppad/dictionary.py](https://localhost:8080/#) in __call__(self, data, lazy)
999
-> 1000 self.randomize(d.get(self.label_key), fg_indices, bg_indices, d.get(self.image_key))
1001
[/usr/local/lib/python3.10/dist-packages/monai/transforms/croppad/dictionary.py](https://localhost:8080/#) in randomize(self, label, fg_indices, bg_indices, image)
981 ) -> None:
--> 982 self.cropper.randomize(label=label, fg_indices=fg_indices, bg_indices=bg_indices, image=image)
983
[/usr/local/lib/python3.10/dist-packages/monai/transforms/croppad/array.py](https://localhost:8080/#) in randomize(self, label, fg_indices, bg_indices, image)
1151 raise ValueError("label must be provided.")
-> 1152 fg_indices_, bg_indices_ = map_binary_to_indices(label, image, self.image_threshold)
1153 _shape = None
[/usr/local/lib/python3.10/dist-packages/monai/transforms/utils.py](https://localhost:8080/#) in map_binary_to_indices(label, image, image_threshold)
359 img_flat, *_ = convert_to_dst_type(img_flat, label, dtype=bool)
--> 360 bg_indices = nonzero(img_flat & ~label_flat)
361 else:
[/usr/local/lib/python3.10/dist-packages/monai/data/meta_tensor.py](https://localhost:8080/#) in __torch_function__(cls, func, types, args, kwargs)
281 kwargs = {}
--> 282 ret = super().__torch_function__(func, types, args, kwargs)
283 # if `out` has been used as argument, metadata is not copied, nothing to do.
[/usr/local/lib/python3.10/dist-packages/torch/_tensor.py](https://localhost:8080/#) in __torch_function__(cls, func, types, args, kwargs)
1417 with _C.DisableTorchFunctionSubclass():
-> 1418 ret = func(*args, **kwargs)
1419 if func in get_default_nowrap_functions():
RuntimeError: The size of tensor a (7663256) must match the size of tensor b (4919040) at non-singleton dimension 0
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
[<ipython-input-9-ca3a090a9802>](https://localhost:8080/#) in <cell line: 2>()
1 check_iter = iter(train_loader)
----> 2 check_data = next(check_iter)
3
4 image, label = check_data["image"].squeeze().numpy(), check_data["label"].squeeze().numpy()
5 print(f"image shape: {image.shape}, label shape: {label.shape}")
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py](https://localhost:8080/#) in __next__(self)
629 # TODO(https://github.com/pytorch/pytorch/issues/76750)
630 self._reset() # type: ignore[call-arg]
--> 631 data = self._next_data()
632 self._num_yielded += 1
633 if self._dataset_kind == _DatasetKind.Iterable and \
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py](https://localhost:8080/#) in _next_data(self)
673 def _next_data(self):
674 index = self._next_index() # may raise StopIteration
--> 675 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
676 if self._pin_memory:
677 data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py](https://localhost:8080/#) in fetch(self, possibly_batched_index)
49 data = self.dataset.__getitems__(possibly_batched_index)
50 else:
---> 51 data = [self.dataset[idx] for idx in possibly_batched_index]
52 else:
53 data = self.dataset[possibly_batched_index]
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py](https://localhost:8080/#) in <listcomp>(.0)
49 data = self.dataset.__getitems__(possibly_batched_index)
50 else:
---> 51 data = [self.dataset[idx] for idx in possibly_batched_index]
52 else:
53 data = self.dataset[possibly_batched_index]
[/content/drive/MyDrive/Colab Notebooks/COMP4211/Project/loader.py](https://localhost:8080/#) in __getitem__(self, idx)
14 data_dict = self.data[idx]
15 if self.transform:
---> 16 data_dict = self.transform(data_dict)
17 return data_dict
18
[/usr/local/lib/python3.10/dist-packages/monai/transforms/compose.py](https://localhost:8080/#) in __call__(self, input_, start, end, threading, lazy)
333 def __call__(self, input_, start=0, end=None, threading=False, lazy: bool | None = None):
334 _lazy = self._lazy if lazy is None else lazy
--> 335 result = execute_compose(
336 input_,
337 transforms=self.transforms,
[/usr/local/lib/python3.10/dist-packages/monai/transforms/compose.py](https://localhost:8080/#) in execute_compose(data, transforms, map_items, unpack_items, start, end, lazy, overrides, threading, log_stats)
109 if threading:
110 _transform = deepcopy(_transform) if isinstance(_transform, ThreadUnsafe) else _transform
--> 111 data = apply_transform(
112 _transform, data, map_items, unpack_items, lazy=lazy, overrides=overrides, log_stats=log_stats
113 )
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
169 else:
170 _log_stats(data=data)
--> 171 raise RuntimeError(f"applying transform {transform}") from e
172
173
RuntimeError: applying transform <monai.transforms.croppad.dictionary.RandCropByPosNegLabeld object at 0x7f32347b8c10>
from monai.
Hi @tararelan, could you please try Dataset
instead of your customized one? Didn't see special reason you write your own dataset.
apply_transform
has some logic when your data
is a list.
https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/transform.py#L101
from monai.
Hi, I used Dataset instead of the customised one and it works. However, when I try to train my model using:
train_ds = Dataset(train_data_dicts, train_transforms)
val_ds = Dataset(val_data_dicts, val_transforms)
train_loader = DataLoader(train_ds, batch_size=8, shuffle=True, collate_fn=pad_list_data_collate)
val_loader = DataLoader(val_ds, batch_size=8, shuffle=False, collate_fn=pad_list_data_collate)
import torch.nn as nn
from torchsummary import summary
from monai.networks.nets import UNet
from monai.networks.layers import Norm
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
model = UNet(
spatial_dims=3,
in_channels=1,
out_channels=2,
channels=(16, 32, 64, 128, 256),
strides=(2, 2, 2, 2),
num_res_units=2,
norm=Norm.BATCH,
).to(device)
model = model.to(device)
summary(model, input_size=(1, 16, 128, 128))
from monai.metrics import DiceMetric, SurfaceDistanceMetric, HausdorffDistanceMetric, MeanIoU
from monai.losses import DiceLoss
optimizer = torch.optim.Adam(model.parameters(), 1e-4)
loss_function = DiceLoss(to_onehot_y=True, softmax=True, jaccard=False)
dice_metric = DiceMetric(include_background=False, reduction="mean")
asd_metric = SurfaceDistanceMetric(include_background=False, distance_metric="euclidean")
hausdorff_metric = HausdorffDistanceMetric(include_background=False,percentile=95)
jaccard_metric = MeanIoU(include_background=False)
max_epochs = 2
val_interval = 2
best_metric = -1
best_metric_epoch = -1
epoch_loss_values = []
dice_values = []
asd_values = []
hausdorff_values = []
jaccard_values = []
post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])
post_label = Compose([AsDiscrete(to_onehot=2)])
from monai.inferers import sliding_window_inference
from monai.data import decollate_batch
from monai.transforms import SpatialCrop
wandb.init(project="Project", name="Trial1")
for epoch in range(max_epochs):
print("-" * 10)
model.train()
epoch_loss = 0
step = 0
for batch_data in train_loader:
step += 1
inputs, labels = (
batch_data["image"].to(device),
batch_data["label"].to(device),
)
optimizer.zero_grad()
outputs = model(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_loss /= step
epoch_loss_values.append(epoch_loss)
print(f"epoch {epoch + 1} average loss: {epoch_loss:.4f}")
wandb.log({'train_loss': epoch_loss}, step= epoch)
if (epoch + 1) % val_interval == 0:
model.eval()
with torch.no_grad():
for val_data in val_loader:
val_inputs, val_labels = (
val_data["image"].to(device),
val_data["label"].to(device),
)
roi_size = (96, 96, 96)
sw_batch_size = 4
val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)
val_outputs = [post_pred(i) for i in decollate_batch(val_outputs)]
val_labels = [post_label(i) for i in decollate_batch(val_labels)]
# val_outputs = torch.stack(val_outputs)
# val_labels = torch.stack(val_labels)
print(val_outputs.shape, val_labels.shape)
dice_metric(val_outputs, val_labels)
asd_metric(y_pred=val_outputs, y=val_labels)
hausdorff_metric(y_pred=val_outputs, y=val_labels)
jaccard_metric(y_pred=val_outputs, y=val_labels)
dice = dice_metric.aggregate().item()
asd = asd_metric.aggregate().item()
hausdorff = hausdorff_metric.aggregate().item()
jaccard = jaccard_metric.aggregate().item()
dice_metric.reset()
asd_metric.reset()
hausdorff_metric.reset()
jaccard_metric.reset()
dice_values.append(dice)
asd_values.append(asd)
hausdorff_values.append(hausdorff)
jaccard_values.append(jaccard)
wandb.log({'val_dice': dice}, step= epoch)
wandb.log({'val_asd': asd}, step= epoch)
wandb.log({'val_hausdorff': hausdorff}, step= epoch)
wandb.log({'val_jaccard': jaccard}, step= epoch)
if dice > best_metric:
best_metric = dice
best_metric_epoch = epoch + 1
torch.save(model.state_dict(), "./best_metric_model.pth")
print("saved new best metric model")
print(
f"current epoch: {epoch + 1} current mean dice: {dice:.4f}"
f"\nbest mean dice: {best_metric:.4f} "
f"at epoch: {best_metric_epoch}"
)
I get the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
139 if isinstance(data, (list, tuple)) and map_items:
--> 140 return [_apply_transform(transform, item, unpack_items, lazy, overrides, log_stats) for item in data]
141 return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
16 frames
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in <listcomp>(.0)
139 if isinstance(data, (list, tuple)) and map_items:
--> 140 return [_apply_transform(transform, item, unpack_items, lazy, overrides, log_stats) for item in data]
141 return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in _apply_transform(transform, data, unpack_parameters, lazy, overrides, logger_name)
97
---> 98 return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
99
[/usr/local/lib/python3.10/dist-packages/monai/transforms/croppad/array.py](https://localhost:8080/#) in __call__(self, img, mode, lazy, **pad_kwargs)
1470 lazy_ = self.lazy if lazy is None else lazy
-> 1471 ret = self.padder(self.cropper(img, lazy_), mode=mode, lazy=lazy_, **pad_kwargs)
1472 # remove the individual info and combine
[/usr/local/lib/python3.10/dist-packages/monai/transforms/croppad/array.py](https://localhost:8080/#) in __call__(self, img, lazy)
534 img=img,
--> 535 slices=self.compute_slices(img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]),
536 lazy=lazy_,
AttributeError: 'dict' object has no attribute 'shape'
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
140 return [_apply_transform(transform, item, unpack_items, lazy, overrides, log_stats) for item in data]
--> 141 return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
142 except Exception as e:
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in _apply_transform(transform, data, unpack_parameters, lazy, overrides, logger_name)
97
---> 98 return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
99
[/usr/local/lib/python3.10/dist-packages/monai/transforms/compose.py](https://localhost:8080/#) in __call__(self, input_, start, end, threading, lazy)
334 _lazy = self._lazy if lazy is None else lazy
--> 335 result = execute_compose(
336 input_,
[/usr/local/lib/python3.10/dist-packages/monai/transforms/compose.py](https://localhost:8080/#) in execute_compose(data, transforms, map_items, unpack_items, start, end, lazy, overrides, threading, log_stats)
110 _transform = deepcopy(_transform) if isinstance(_transform, ThreadUnsafe) else _transform
--> 111 data = apply_transform(
112 _transform, data, map_items, unpack_items, lazy=lazy, overrides=overrides, log_stats=log_stats
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
170 _log_stats(data=data)
--> 171 raise RuntimeError(f"applying transform {transform}") from e
172
RuntimeError: applying transform <monai.transforms.croppad.array.ResizeWithPadOrCrop object at 0x7f32347b8b80>
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
[<ipython-input-21-727b3f1caebb>](https://localhost:8080/#) in <cell line: 7>()
10 epoch_loss = 0
11 step = 0
---> 12 for batch_data in train_loader:
13 step += 1
14 inputs, labels = (
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py](https://localhost:8080/#) in __next__(self)
629 # TODO(https://github.com/pytorch/pytorch/issues/76750)
630 self._reset() # type: ignore[call-arg]
--> 631 data = self._next_data()
632 self._num_yielded += 1
633 if self._dataset_kind == _DatasetKind.Iterable and \
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py](https://localhost:8080/#) in _next_data(self)
673 def _next_data(self):
674 index = self._next_index() # may raise StopIteration
--> 675 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
676 if self._pin_memory:
677 data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py](https://localhost:8080/#) in fetch(self, possibly_batched_index)
49 data = self.dataset.__getitems__(possibly_batched_index)
50 else:
---> 51 data = [self.dataset[idx] for idx in possibly_batched_index]
52 else:
53 data = self.dataset[possibly_batched_index]
[/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py](https://localhost:8080/#) in <listcomp>(.0)
49 data = self.dataset.__getitems__(possibly_batched_index)
50 else:
---> 51 data = [self.dataset[idx] for idx in possibly_batched_index]
52 else:
53 data = self.dataset[possibly_batched_index]
[/usr/local/lib/python3.10/dist-packages/monai/data/dataset.py](https://localhost:8080/#) in __getitem__(self, index)
110 # dataset[[1, 3, 4]]
111 return Subset(dataset=self, indices=index)
--> 112 return self._transform(index)
113
114
[/usr/local/lib/python3.10/dist-packages/monai/data/dataset.py](https://localhost:8080/#) in _transform(self, index)
96 """
97 data_i = self.data[index]
---> 98 return apply_transform(self.transform, data_i) if self.transform is not None else data_i
99
100 def __getitem__(self, index: int | slice | Sequence[int]):
[/usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py](https://localhost:8080/#) in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
169 else:
170 _log_stats(data=data)
--> 171 raise RuntimeError(f"applying transform {transform}") from e
172
173
RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7f315d5ca4a0>
from monai.
Hi @tararelan, could you please share a reproducible code then I can take a deep look at it? Please check your data after transforms and run the code step by step to see where the size mismatch is. Thanks.
(Move to discussion, more like a usage issue)
from monai.
Related Issues (20)
- RandSpatialCropSamplesd HOT 2
- Missing documentation for `Invert`
- Cannot use non-`nearest` interpolation methods with `Invertd`
- ResourceWarning: Implicitly cleaning up TemporaryDirectory
- Add deep supervision to classification models
- Support more dtypes in `pad_nd`
- The resolution to be used in input images for inferencing in a trained nodule detector model HOT 1
- How to get image file names from DataLoader HOT 1
- RandWeightedCropd in batch
- Cannot run V-Net on medical decathlon data HOT 1
- Numpy 2.0 Behaviour Changes HOT 1
- Adding Tailored Model Implementations into Generative Model Application
- Version 1.3.2 Hotfix For Numpy HOT 2
- Attribute error in utils type_conversion code in latest monai version 1.3.1 HOT 2
- Load data repeatedly in class CSVIterableDataset from monai/data/iterable_dataset.py
- releasing v1.3.2 -- checklist
- Adding Tailored ControlNet Implementations into Generative Model Application
- 'load_module()' deprecated in Python 3.12
- Add 'ignore_empty' parameter to loss functions for handling empty ground truth
- Ruff check failed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from monai.