-
Notifications
You must be signed in to change notification settings - Fork 719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tutorial/demo for 2D slice analysis based on 3D inputs #130
Comments
Feel free to use this if it's of use to you. For the inverse transformation / test time augmentation notebook, I created a 2D dataset from the 3D brain tumour dataset. I use the slice in the 3D that had the largest segmented volume. I then saved to file, which obviously isn't necessary, but I found it to be faster. Output is then a list of dictionaries with filenames to the 2D slices: class SliceWithMaxNumLabelsd(MapTransform):
def __init__(self, keys, label_key):
self.keys = keys
self.label_key = label_key
def __call__(self, data):
d = dict(data)
im = d[self.label_key]
q = np.sum((im > 0).reshape(-1, im.shape[-1]), axis=0)
_slice = np.where(q == np.max(q))[0][0]
for key in self.keys:
d[key] = d[key][..., _slice]
return d
class SaveSliced(MapTransform):
def __init__(self, keys, path):
self.keys = keys
self.path = path
def __call__(self, data):
d = {}
for key in self.keys:
fname = os.path.basename(
data[key + "_meta_dict"]["filename_or_obj"])
path = os.path.join(self.path, key, fname)
nib.save(nib.Nifti1Image(data[key], np.eye(4)), path)
d[key] = path
return d
keys = ["image", "label"]
data_dir = os.path.join(root_dir, task + "_single_slice")
for key in keys:
os.makedirs(os.path.join(data_dir, key), exist_ok=True)
transform_2d_slice = Compose([
LoadImaged(keys),
AsChannelFirstd("image"),
AddChanneld("label"),
SliceWithMaxNumLabelsd(keys, "label"),
SaveSliced(keys, data_dir),
])
ds_2d = Dataset(data_dicts, transform_2d_slice)
dl_2d = DataLoader(ds_2d, batch_size=1, num_workers=10)
data_dicts_single_slice = list(tqdm(dl_2d)) |
|
thanks, I think recently we have a |
Certainly could, I'll look into updating my PR! I just noticed that when you refer to a Discussion (e.g., your first link), that discussion doesn't get a link pointing to the issue/PR discussing it. This is a shame, since the people who posted those questions won't necessarily know we're discussing it here. |
Hello, I am just wondering if there is a new transform implemented in Monai to get 2D samples from 3D volumes. I use RandWeightedCropd during the training to feed random slices into my 2D network. My question is if there is any function to sequentially get all test all of the slices of a 3D volume during the inference stage so I can save the output as a 3D volume? Thanks in advance! |
Is your feature request related to a problem? Please describe.
would be great to demonstrate the available options for both training and inference
there were quite a few requests, see also:
The text was updated successfully, but these errors were encountered: