You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to load nii.gz file from s3 and apply transform on that image.
LoadImaged in compose need file path as input. But when i load image as object. So i feel transfrom is failing as LoadImaged cant accept objects.
Below code works fine when i run on sagemaker. As i don't need to download image from s3
Please suggest me the different ways to load the images and apply transform.
Note: i cant get the path of the image to pass it to LoadImaged or LoadImage
Thanks for your interest and feedback!
Yes, we don't support reading from the stream so far, we can consider to support it in future. @wyli Can we use Nibabel or ITK to read remote files with URI directly?
@Nic-Ma i have tried Nibabel but after that i try applying transform but getting below error img = nib.load('/home/ec2-user/SageMaker/efs/datasets/Task09_Spleen/imagesTr/spleen_61.nii.gz') val_transforms({"image":img.get_fdata()}) Error::
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/monai/transforms/transform.py in apply_transform(transform, data, map_items, unpack_items)
91 return [_apply_transform(transform, item, unpack_items) for item in data]
---> 92 return _apply_transform(transform, data, unpack_items)
93 except Exception as e:
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/monai/transforms/transform.py in _apply_transform(transform, parameters, unpack_parameters)
59
---> 60 return transform(parameters)
61
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/monai/transforms/utility/dictionary.py in __call__(self, data)
309 for key, meta_key, meta_key_postfix in zip(self.keys, self.meta_keys, self.meta_key_postfix):
--> 310 d[key] = self.adjuster(d[key], d[meta_key or f"{key}_{meta_key_postfix}"])
311 return d
KeyError: 'image_meta_dict'
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
/tmp/ipykernel_11054/3074050110.py in <module>
16
17 #val_transforms(np.expand_dims(np.moveaxis(img.get_fdata(),2,0),axis=0))
---> 18 val_transforms({"image":img.get_fdata()})
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/monai/transforms/compose.py in __call__(self, input_)
158 def __call__(self, input_):
159 for _transform in self.transforms:
--> 160 input_ = apply_transform(_transform, input_, self.map_items, self.unpack_items)
161 return input_
162
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/monai/transforms/transform.py in apply_transform(transform, data, map_items, unpack_items)
114 else:
115 _log_stats(data=data)
--> 116 raise RuntimeError(f"applying transform {transform}") from e
117
118
RuntimeError: applying transform <monai.transforms.utility.dictionary.EnsureChannelFirstd object at 0x7f4e50c5b410>
So what is the expected format of data input to transform when we use data loaded with nibabel
For EnsureChannelFirstd(keys=["image"]), it depends on some meta data from LoadImage to automatically adjust the channel dim. As you removed LoadImage and used your own loaded data, so it caused an error.
You can change EnsureChannelFirstdto use AddChanneld transform, then I think it should work.
Hi @Nic-Ma ,
It worked after replacing EnsureChannelFirstd to AddChanneld. Thanks for the suggestion
But the real problem is with loading s3 object and applying transformation. I will check the ways to do the same. You can also suggest me if there is any way. With out this i wont able able to deploy my MONAI trained model on AWS.
print(img.get_fdata().shape) --> (512, 512, 31) >> looks like 3rd dimension is the channels
print(val_data['image'].shape) --> torch.Size([1, 21, 342, 208]) >> here 2nd dimension represents the channel as iam using AsChannelFirstd ==> In this case i am facing above error like sequence must have lenght 2.
@wyli
I have also tried to make sure 4th dimension as channel by removing AsChannelFirstd. As per ur above suggestion. But still i am facing same error like sequence must have length 2.
Activity
Nic-Ma commentedon Jan 23, 2022
Hi @j-sieger ,
Thanks for your interest and feedback!
Yes, we don't support reading from the stream so far, we can consider to support it in future.
@wyli Can we use Nibabel or ITK to read remote files with URI directly?
Thanks.
j-sieger commentedon Jan 23, 2022
@Nic-Ma i have tried Nibabel but after that i try applying transform but getting below error
img = nib.load('/home/ec2-user/SageMaker/efs/datasets/Task09_Spleen/imagesTr/spleen_61.nii.gz')val_transforms({"image":img.get_fdata()})Error::
So what is the expected format of data input to transform when we use data loaded with nibabel
Nic-Ma commentedon Jan 23, 2022
Hi @j-sieger ,
For
EnsureChannelFirstd(keys=["image"]), it depends on some meta data fromLoadImageto automatically adjust thechannel dim. As you removedLoadImageand used your own loaded data, so it caused an error.You can change
EnsureChannelFirstdto useAddChanneldtransform, then I think it should work.Thanks.
j-sieger commentedon Jan 24, 2022
Hi @Nic-Ma ,
It worked after replacing
EnsureChannelFirstdtoAddChanneld. Thanks for the suggestionBut the real problem is with loading s3 object and applying transformation. I will check the ways to do the same. You can also suggest me if there is any way. With out this i wont able able to deploy my MONAI trained model on AWS.
wyli commentedon Jan 24, 2022
duplicate of Project-MONAI/MONAI#1656?
j-sieger commentedon Jan 24, 2022
@Nic-Ma , @wyli
**I have another issue during prediction this time after successfully applying transform on the file loaded with **
My code:
ERROR:
wyli commentedon Jan 24, 2022
could you ensure that
{"image":img.get_fdata()}is 4D and the 4th dimension represents channelj-sieger commentedon Jan 24, 2022
print(img.get_fdata().shape) --> (512, 512, 31) >> looks like 3rd dimension is the channels
print(val_data['image'].shape) --> torch.Size([1, 21, 342, 208]) >> here 2nd dimension represents the channel as iam using
AsChannelFirstd==> In this case i am facing above error like sequence must have lenght 2.@wyli
I have also tried to make sure 4th dimension as channel by removing
AsChannelFirstd. As per ur above suggestion. But still i am facing same error like sequence must have length 2.wyli commentedon Jan 24, 2022
I see, assuming this is about 3D image processing... given that you have
print(img.get_fdata().shape) --> (512, 512, 31)the script would be
the
AddChanneldtransform with makeval_data['image']a 4D array. and the rest of the pipeline should work with the 4D array.