-
I am following the tutorial of SwinUNETR. The following is the code applied to the test set. with torch.no_grad():
dice_list_case = []
for i, batch in enumerate(val_loader):
val_inputs, val_labels = (batch["image"].cuda(), batch["label"].cuda())
import ipdb; ipdb.set_trace();
original_affine = batch["label_meta_dict"]["affine"][0].numpy()
_, _, h, w, d = val_labels.shape
target_shape = (h, w, d)
img_name = batch["image_meta_dict"]["filename_or_obj"][0].split("/")[-1]
print("Inference on case {}".format(img_name))
val_outputs = sliding_window_inference(
val_inputs, (args.roi_x, args.roi_y, args.roi_z), 4, model, overlap=args.infer_overlap, mode="gaussian"
)
val_outputs = torch.softmax(val_outputs, 1).cpu().numpy()
val_outputs = np.argmax(val_outputs, axis=1).astype(np.uint8)[0]
val_labels = val_labels.cpu().numpy()[0, 0, :, :, :]
val_outputs = resample_3d(val_outputs, target_shape)
dice_list_sub = []
for i in range(1, 17):
organ_Dice = dice(val_outputs == i, val_labels == i)
dice_list_sub.append(organ_Dice)
mean_dice = np.mean(dice_list_sub)
print("Mean Organ Dice: {}".format(mean_dice))
dice_list_case.append(mean_dice)
nib.save(
nib.Nifti1Image(val_outputs.astype(np.uint8), original_affine), os.path.join(output_directory, img_name)
) However, I encountered some issues when testing the codes, and the errors are as follow. Traceback (most recent call last):
File "/home/lt/Desktop/SwinUNETR_v2_script/test.py", line 126, in <module>
main()
File "/home/lt/Desktop/SwinUNETR_v2_script/test.py", line 99, in main
original_affine = batch["label_meta_dict"]["affine"][0].numpy()
KeyError: 'label_meta_dict' And I checked the keys of ipdb > batch.keys()
dict_keys(['image', 'label']) I am using version 1.3.0 of MONAI, I don't know if it's a version issue. If so, which version should I use? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @uto-lt, thanks for your interest here. Hope it helps, thanks! |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help! It works now:) |
Beta Was this translation helpful? Give feedback.
Hi @uto-lt, thanks for your interest here.
Yes, it was a version issue which is introduced by this PR.
You could downgrade to 1.2 or an alternative way you could directly use
batch["image"].meta
to get all meta information including affine and filename_or_obj.Hope it helps, thanks!