Skip to content

Commit fc82d50

Browse files
KumoLiujuampatronics
authored andcommitted
Fix RuntimeError in DataAnalyzer (Project-MONAI#7310)
Fixes Project-MONAI#7309 ### Description `DataAnalyzer` only catch error when data is on GPU, add catching error when data is on CPU. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: YunLiu <[email protected]> Signed-off-by: Juan Pablo de la Cruz Gutiérrez <[email protected]>
1 parent 6a45df2 commit fc82d50

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

monai/apps/auto3dseg/data_analyzer.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from monai.data import DataLoader, Dataset, partition_dataset
2929
from monai.data.utils import no_collation
3030
from monai.transforms import Compose, EnsureTyped, LoadImaged, Orientationd
31-
from monai.utils import StrEnum, min_version, optional_import
31+
from monai.utils import ImageMetaKey, StrEnum, min_version, optional_import
3232
from monai.utils.enums import DataStatsKeys, ImageStatsKeys
3333

3434

@@ -343,19 +343,25 @@ def _get_all_case_stats(
343343
d = summarizer(batch_data)
344344
except BaseException as err:
345345
if "image_meta_dict" in batch_data.keys():
346-
filename = batch_data["image_meta_dict"]["filename_or_obj"]
346+
filename = batch_data["image_meta_dict"][ImageMetaKey.FILENAME_OR_OBJ]
347347
else:
348-
filename = batch_data[self.image_key].meta["filename_or_obj"]
348+
filename = batch_data[self.image_key].meta[ImageMetaKey.FILENAME_OR_OBJ]
349349
logger.info(f"Unable to process data {filename} on {device}. {err}")
350350
if self.device.type == "cuda":
351351
logger.info("DataAnalyzer `device` set to GPU execution hit an exception. Falling back to `cpu`.")
352-
batch_data[self.image_key] = batch_data[self.image_key].to("cpu")
353-
if self.label_key is not None:
354-
label = batch_data[self.label_key]
355-
if not _label_argmax:
356-
label = torch.argmax(label, dim=0) if label.shape[0] > 1 else label[0]
357-
batch_data[self.label_key] = label.to("cpu")
358-
d = summarizer(batch_data)
352+
try:
353+
batch_data[self.image_key] = batch_data[self.image_key].to("cpu")
354+
if self.label_key is not None:
355+
label = batch_data[self.label_key]
356+
if not _label_argmax:
357+
label = torch.argmax(label, dim=0) if label.shape[0] > 1 else label[0]
358+
batch_data[self.label_key] = label.to("cpu")
359+
d = summarizer(batch_data)
360+
except BaseException as err:
361+
logger.info(f"Unable to process data {filename} on {device}. {err}")
362+
continue
363+
else:
364+
continue
359365

360366
stats_by_cases = {
361367
DataStatsKeys.BY_CASE_IMAGE_PATH: d[DataStatsKeys.BY_CASE_IMAGE_PATH],

monai/auto3dseg/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def __call__(self, data: Mapping[Hashable, MetaTensor]) -> dict[Hashable, MetaTe
460460
torch.set_grad_enabled(False)
461461

462462
ndas: list[MetaTensor] = [d[self.image_key][i] for i in range(d[self.image_key].shape[0])] # type: ignore
463-
ndas_label: MetaTensor = d[self.label_key] # (H,W,D)
463+
ndas_label: MetaTensor = d[self.label_key].astype(torch.int8) # (H,W,D)
464464

465465
if ndas_label.shape != ndas[0].shape:
466466
raise ValueError(f"Label shape {ndas_label.shape} is different from image shape {ndas[0].shape}")

0 commit comments

Comments
 (0)