-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
meta tensor #4077
meta tensor #4077
Conversation
Signed-off-by: Richard Brown <[email protected]>
This is non-breaking, I think we should try to merge this into the for the naming of key classes and modules: |
Should |
@wyli Is there much point merging this into |
yes it'll be convenient for creating demos or experimenting with metatensor using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rijobro ,
Thanks for your initial cool idea and quick PR.
If plan to merge to dev
branch, I have 3 minor concerns:
(1) As this is a very important base classes, I feel we should write very detailed doc-string with example code and explanation for every arg. Refer to transform
:
https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/transform.py
(2) MetaObj
is the base class for Metadata, it should not be aware of any tensor
or numpy
specific properties.
(2) Let's try to define fewer APIs in the MetaObj
base class to keep it simple and easy to extend.
What do you think?
Thanks.
Hi @vfdev-5 , We are trying to extend PyTorch Tensor with medical metadata properties. Thanks in advance. |
@Nic-Ma thanks for pinging ! I'll check the PR in details a bit later, right now few comments/info (in case if helpful):
|
And as @vfdev-5 mentioned in another conversation, this Thanks. |
Signed-off-by: Richard Brown <[email protected]>
for more information, see https://pre-commit.ci
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
Hi, think I've tried to resolve any outstanding issues. I also added testing for pickling, torchscript and amp. |
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
@wyli @Nic-Ma I can't reproduce this error anywhere and I've tried on quite a few different machines (MacOS, Windows, 3xUbuntu). Can any of you reproduce? |
I can replicate with MacOS py3.8 and with |
This reverts commit 840e7df. Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
@wyli let me know when you've bumped the min pytorch version, hopefully this PR will be good to go at that point. |
Hi @vfdev-5 have you had the chance to look this PR over? Would be great to get your insight. |
Hi @rijobro , I synced with @vfdev-5 offline, he tried to review this PR several times, but is still not clear about the context and initial intent of this PR, could you please help provide some design doc or usage examples to help him better understand it? Thanks in advance. |
Sure @Nic-Ma. Hi @vfdev-5, we would like to attach our meta data to our images. Looking at the pytorch docs for extending Previously, we used dictionaries of data containing images, their metadata and the transforms that have been applied to them, e.g., Looking to the future, we would like our transforms to be able to update metadata when they are applied, so we need to first tidy things up, as there isn't scope for adding extra functionality as things stand. Hence, what would have previously been: transform = LoadImaged("img", "seg")
data = {"img": some_filename, "seg": some_other_filename}
data = transform(data)
print(data) # {"img": ..., "img_meta_dict": ..., "seg": ..., "seg_meta_dict": ...} is now print(data) # {"img": ..., "seg": ...} and now "img" has the meta data and affine transformation matrix that can be extracted with: im = data["img"]
print(im.meta) # dictionary with meta data
print(im.affine) # 4x4 or 3x3 torch.Tensor Now that every image has its attached affine transformation matrix, it should be much easier to make transforms that correctly update it. We allow users to disable this functionality in case they aren't interested in it, and it is causing them bother for whatever reason. |
@rijobro thanks a lot for details! In terms of implementation, I'll check and comment out in the code. Another point I was wondering as far as I understand tensor subclassing feature but not sure if this point is related to your idea. When subclassing torch tensor with t = torch.tensor([1,2,3])
affine = torch.eye(4)
meta = {"some": "info"}
m = MetaTensor(t, affine=affine, meta=meta)
res = m.sum()
print(res)
> tensor(6)
MetaData
some: info
affine: tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]) If this is expected and intended behaviour, please ignore this point. EDIT: t = torch.tensor([1, 2, 3])
affine = torch.eye(4)
meta = {"some": "info"}
m1 = MetaTensor(t, affine=affine, meta=meta)
t = torch.tensor([10, 20, 30])
affine = torch.eye(5)
meta = {"some1": "info2"}
m2 = MetaTensor(t, affine=affine, meta=meta)
r1 = m1 + m2
r2 = m2 + m1
# r1.meta != r2.meta |
that's a good point, perhaps we need this to differentiate w/o the batch dim for the collate... (the collate related feature is not included in this PR, we can consider this in the follow-ups.) |
/build |
@vfdev-5 thanks for the useful input. The way that we currently have it, yes, if So Also, yes, things get a bit confusing when combining meta data. But if |
@wyli for collating and decollating, I've been messing around with This would be a follow up PR. What do you think? It's maybe a little hacky. The alternative is to put that logic into the collation and decollation functions, which is what I did in the original PR. |
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, it looks good to me, I put some minor docstring updates here rijobro#3
/build |
Add padding to filter to ensure same size after anti-aliasing Use replicate padding insteadof zero padding to avoid artifacts for non-zero boundary Reuse GaussianSmooth 4073 Enhance DynUNet doc-strings (Project-MONAI#4102) * Fix doc strings error Signed-off-by: Yiheng Wang <[email protected]> * remove duplicate places Signed-off-by: Yiheng Wang <[email protected]> 4105 drops pt16 support (Project-MONAI#4106) * update sys req Signed-off-by: Wenqi Li <[email protected]> * temp test Signed-off-by: Wenqi Li <[email protected]> * update code for torch>=1.7 Signed-off-by: Wenqi Li <[email protected]> * temp tests Signed-off-by: Wenqi Li <[email protected]> * fixes tests Signed-off-by: Wenqi Li <[email protected]> * autofix Signed-off-by: Wenqi Li <[email protected]> * fixes import Signed-off-by: Wenqi Li <[email protected]> * clear cache Signed-off-by: Wenqi Li <[email protected]> * update based on comments Signed-off-by: Wenqi Li <[email protected]> * remove temp cmd Signed-off-by: Wenqi Li <[email protected]> Make `pixelshuffle` scriptable (Project-MONAI#4109) * Update the existing functionality to comply with the `torchscript.jit.script` function. Signed-off-by: Ramon Emiliani <[email protected]> meta tensor (Project-MONAI#4077) * meta tensor Signed-off-by: Richard Brown <[email protected]> 4084 Add kwargs for `Tensor.to()` in engines (Project-MONAI#4112) * [DLMED] add kwargs for to() API Signed-off-by: Nic Ma <[email protected]> * [MONAI] python code formatting Signed-off-by: monai-bot <[email protected]> * [DLMED] fix typo Signed-off-by: Nic Ma <[email protected]> * [DLMED] fix flake8 Signed-off-by: Nic Ma <[email protected]> * [DLMED] update according to comments Signed-off-by: Nic Ma <[email protected]> Co-authored-by: monai-bot <[email protected]> fixes pytorch version tests (Project-MONAI#4127) Signed-off-by: Wenqi Li <[email protected]> update meta tensor api (Project-MONAI#4131) * update meta tensor api Signed-off-by: Wenqi Li <[email protected]> * update based on comments Signed-off-by: Wenqi Li <[email protected]> runtests.sh isort (Project-MONAI#4134) Signed-off-by: Richard Brown <[email protected]> update citation (Project-MONAI#4133) Signed-off-by: Wenqi Li <[email protected]> `ToMetaTensor` and `FromMetaTensor` transforms (Project-MONAI#4115) to and from meta no skip if before pytorch 1.7 (Project-MONAI#4139) * no skip if before pytorch 1.7 Signed-off-by: Richard Brown <[email protected]> * fix Signed-off-by: Richard Brown <[email protected]> * fix Signed-off-by: Richard Brown <[email protected]> [DLMED] fix file name in meta (Project-MONAI#4145) Signed-off-by: Nic Ma <[email protected]> 4116 Add support for advanced args of AMP (Project-MONAI#4132) * [DLMED] fix typo in bundle scripts Signed-off-by: Nic Ma <[email protected]> * [DLMED] add support for AMP args Signed-off-by: Nic Ma <[email protected]> * [MONAI] python code formatting Signed-off-by: monai-bot <[email protected]> * [DLMED] fix flake8 Signed-off-by: Nic Ma <[email protected]> Co-authored-by: monai-bot <[email protected]> New wsireader (Project-MONAI#4147) `MetaTensor`: collate; decollate; dataset; dataloader; out=; indexing and iterating across batches (Project-MONAI#4137) `MetaTensor`: collate; decollate; dataset; dataloader; out=; indexing and iterating across batches (Project-MONAI#4137)
Description
First PR to MetaTensor branch.
As promised, we'll merge to
MetaTensor
branch via PRs and code reviews. This is the first PR and just adds the object and some tests.MetaObj
allows for subclassingnp.ndarray
in the future should we want to.Status
Ready
Types of changes
make html
command in thedocs/
folder.