ToTensorV2 assumes image dimension as 3
See original GitHub issue🐛 Bug
albumentations.pytorch.ToTensorV2 assumes that input image has 3 dimensions since it calls
torch.from_numpy(img.transpose(2, 0, 1))
The transpose operation throws a ValueError axes don’t match array when any image other than 3 dimensional is provided.
To Reproduce
Steps to reproduce the behavior:
- Create a PyTorch loader, similar like here: https://github.com/albu/albumentations/blob/master/notebooks/migrating_from_torchvision_to_albumentations.ipynb
- Apply the transformation in getitem self._transforms(image=image)[‘image’]
Expected behavior
Transpose and ToTensor calls should be separated. It blocks users from using grayscale or any high dimensional input data.
Environment
- Albumentations version (e.g., 0.1.8): 0.4.3
- Python version (e.g., 3.7): 3.6.8
- OS (e.g., Linux): CentOS 7
- How you installed albumentations (
conda
,pip
, source): pip
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Transforms (pytorch.transforms) - Albumentations
Convert image and mask to torch.Tensor and divide by 255 if image or mask are uint8 type. This transform is now removed from...
Read more >using ImageFolder with albumentations in pytorch
You need to use ToTensorV2 transformation as the final one: ... Exception: sample = self.transform(image=np.array(sample))["image"] if ...
Read more >Overview of Albumentations: Open-source library for ...
This library accepts images as NumPy arrays, segmentation masks as NumPy arrays, ... from albumentations.pytorch import ToTensorV2class ...
Read more >Image segmentation - Paperspace Blog
In this article, we will define image segmentation, discover the right metrics to use ... Using IoU implies that we have two images...
Read more >Albumentation transformations for train and test dataset
(Ayushman Buragohain) November 27, 2020, 3:48pm #1 ... valid_augs contain Resize, Normalization and ToTensorV2 albumentations transformations.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, I get that but a function named
ToTensor
must do… just that… convert to tensor. Adding extra (undocumented) functionality only leads to confusion.I already have a pre-processed dataset in CHW format and I would like to use that for TTA. Using the current
ToTensorV2
unnecessarily requires me to transpose the channels just to pass to the function. Of course, I can have a workaround by having my own ToTensor, but doesn’t that forego the idea ofToTensorV2
in the first place?If you need only to convert image to tensor you has function
torch.from_numpy
. This transform is necessary to convert images from the library format to the torch image format. And torch work with images inCHW
, but library works inHWC
, so we need transpose.