question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"arbitrary number of leading dimensions" only supports up to 4 dimensions

See original GitHub issue

📚 The doc issue

I was attempting do an affine transform of medical imaging data organized using two channel dimensions (SLICE and SEQUENCE) for a total of five: BATCH x SLICE x SEQUENCE x HEIGHT x WIDTH, but torchvision.transforms.functional.affine only supports up to four dimensions.

import torchvision
x = torch.empty((1, 320, 320))
x_t = torchvision.transforms.functional.affine(x, angle=0, translate=(0,0), scale=1, shear=0)
print(x.shape, x_t.shape)

x = torch.empty((1, 1, 320, 320))
x_t = torchvision.transforms.functional.affine(x, angle=0, translate=(0,0), scale=1, shear=0)
print(x.shape, x_t.shape)

x = torch.empty((1, 1, 1, 320, 320))
x_t = torchvision.transforms.functional.affine(x, angle=0, translate=(0,0), scale=1, shear=0)
print(x.shape, x_t.shape)

x = torch.empty((1, 1, 1, 1, 320, 320))
x_t = torchvision.transforms.functional.affine(x, angle=0, translate=(0,0), scale=1, shear=0)
print(x.shape, x_t.shape)

Resulting output and error message:

torch.Size([1, 320, 320]) torch.Size([1, 320, 320])
torch.Size([1, 1, 320, 320]) torch.Size([1, 1, 320, 320])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-211-2ee8b4ff09ce> in <module>
      9 
     10 x = torch.empty((1, 1, 1, 320, 320))
---> 11 x_t = torchvision.transforms.functional.affine(x, angle=0, translate=(0,0), scale=1, shear=0)
     12 print(x.shape, x_t.shape)
     13 

~/bmelseg/venv/lib/python3.6/site-packages/torchvision/transforms/functional.py in affine(img, angle, translate, scale, shear, interpolation, fill, resample, fillcolor)
   1123     translate_f = [1.0 * t for t in translate]
   1124     matrix = _get_inverse_affine_matrix([0.0, 0.0], angle, translate_f, scale, shear)
-> 1125     return F_t.affine(img, matrix=matrix, interpolation=interpolation.value, fill=fill)
   1126 
   1127 

~/bmelseg/venv/lib/python3.6/site-packages/torchvision/transforms/functional_tensor.py in affine(img, matrix, interpolation, fill)
    696     # grid will be generated on the same device as theta and img
    697     grid = _gen_affine_grid(theta, w=shape[-1], h=shape[-2], ow=shape[-1], oh=shape[-2])
--> 698     return _apply_grid_transform(img, grid, interpolation, fill=fill)
    699 
    700 

~/bmelseg/venv/lib/python3.6/site-packages/torchvision/transforms/functional_tensor.py in _apply_grid_transform(img, grid, mode, fill)
    645         img = torch.cat((img, dummy), dim=1)
    646 
--> 647     img = grid_sample(img, grid, mode=mode, padding_mode="zeros", align_corners=False)
    648 
    649     # Fill with required color

~/bmelseg/venv/lib/python3.6/site-packages/torch/nn/functional.py in grid_sample(input, grid, mode, padding_mode, align_corners)
   4009         align_corners = False
   4010 
-> 4011     return torch.grid_sampler(input, grid, mode_enum, padding_mode_enum, align_corners)
   4012 
   4013 

RuntimeError: grid_sampler(): expected 4D or 5D input and grid with same number of dimensions, but got input with sizes [1, 1, 1, 320, 320] and grid with sizes [1, 320, 320, 2]

cc @vfdev-5 @datumbox

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
vfdev-5commented, May 13, 2022

@asy51 I think a quick workaround using torchvision.transforms.functional.affine is to reshape input tensor to 3D or 4D as

in_shape = input_image.shape # (BATCH x SLICE x SEQUENCE x HEIGHT x WIDTH)
reshaped_image = input_image.reshape(-1, in_shape[-3], in_shape[-2], in_shape[-1])
out_image = torchvision.transforms.functional.affine(reshaped_image, angle=0, translate=(0,0), scale=1, shear=0)
out_image = out_image.reshape(in_shape)

I agree that we could provide a support for that in stable and prototype APIs

IIRC, you also wanted to fix this on the prototype transforms, right?

yes, related issue is https://github.com/pytorch/vision/issues/5664

1reaction
datumboxcommented, May 13, 2022

We should investigate fixes for this. The new kernels of new transforms should support Videos/Image-sequences.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for torchvision.transforms.functional - PyTorch
This is only supported if ``size`` is an int (or a sequence of length 1 in ... where ... means an arbitrary number...
Read more >
Incompatibilities with MATLAB in Variable-Size Support for ...
For variable -size N-D arrays, the size function can return a different result in generated code than in MATLAB ®. In generated code,...
Read more >
Dimensions and Measures, Blue and Green - Tableau Help
Note: With a cube (multidimensional) data source, the options for changing data roles are limited. In Tableau Desktop, cubes are supported only on...
Read more >
Dimensions and metrics - Analytics Help
Not every metric can be combined with every dimension. Each dimension and metric has a scope: user-level, session-level, or hit-level. In most cases,...
Read more >
Do we really live in only three dimensions? - CMS Experiment
Hot on the heels of the Standard Model, some physicists are working to support an idea called string theory. This attempts to tie...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found