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.

channels_last doesn't work

See original GitHub issue

🐛Bug

Hi, I’m learning TorchIO and I found channels_last doesn’t do anything with an input tensor (C, W, H, D).

To reproduce

import numpy as np
import torchio as tio

print(f'{tio.__version__=}')

# Input is numpy array
array = np.random.rand(1, 256, 256, 159).astype('float32')
print('\nInput numpy array')
print(array.shape)

print('\ntio.ScalarImage with channels_last=False')
array_ScalarImage = tio.ScalarImage(tensor=array, channels_last=False)
print(array_ScalarImage.shape)

print('\ntio.ScalarImage with channels_last=True')
array_ScalarImage = tio.ScalarImage(tensor=array, channels_last=True)
print(array_ScalarImage.shape)

Expected behavior

I expected it to return the tensor (D, C, W, H) according to the document.

Actual behavior

tio.__version__='0.18.39'

Input numpy array
(1, 256, 256, 159)

tio.ScalarImage with channels_last=False
(1, 256, 256, 159)

tio.ScalarImage with channels_last=True
(1, 256, 256, 159)

System info

Output of python <(curl -s https://raw.githubusercontent.com/fepegar/torchio/master/print_system.py):

Platform: Linux-5.4.0-73-generic-x86_64-with-glibc2.10
TorchIO:  0.18.39
PyTorch:  1.7.1
NumPy:    1.20.3
Python:   3.8.5 (default, Sep  4 2020, 07:30:14)
[GCC 7.3.0]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
romainValacommented, Jun 21, 2021

yes we should add it more explicitly in the doc…

Yes the extra batch dimension is added by pytorch dataloader, and not in the torchio transform

The specific ordering of the spatial dimension (D, H, W) does not really matter, for all the transform … whatever the spatial ordering is you get the same at the end of the transform … it is more a matter for the network inputs, to ensure the image are always presented similarly for MRI data, we use the transform ToCanonical in order to have all data slice in the same spatial axis …

2reactions
romainValacommented, Jun 21, 2021

Hello

this is not a bug.

channels_last argument, is only used when your read image from disk ( nifti file) . In nifti 4D file, the 4D dimension is often the last dimension. If we want this last dimension to be interpreted as the channel dimension, the argument channels_last = True, will just permute the 4 dimension, to be first (channel dimension in pytorch)

If you use tensor as input, then no need of this extra argument, just reorder your tensor first …

Read more comments on GitHub >

github_iconTop Results From Across the Web

PyTorch conv2d doesn't propagate torch.channels_last ...
When I try to use the torch. nn. conv2d operator on tensors which have the channels_last memory format, then the output does not...
Read more >
Channels last mode doesn't work with inputs with 0 batch ...
Bug 2d convolution layers do not work with inputs with zero batch dimension when used with the channels_last format.
Read more >
Channels Last and DataLoader - Memory Format
channels_last' if it's included locally, and if it's located globally then it appears to get stuck on something when inside the training loop–no ......
Read more >
Optimize PyTorch Performance for Speed and Memory ...
I collected and organized several PyTorch tricks and tips to maximize the efficiency of memory usage and minimize the run time. To better...
Read more >
Transform weights from NCHW to NHWC model
The first model ( channels_last ) is exported into TFLite for ... If this doesn't work, another approach could be to create a...
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