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.

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:

  1. Create a PyTorch loader, similar like here: https://github.com/albu/albumentations/blob/master/notebooks/migrating_from_torchvision_to_albumentations.ipynb
  2. 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
AntixKcommented, Jun 3, 2020

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 of ToTensorV2 in the first place?

0reactions
Dipetcommented, Jun 3, 2020

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 in CHW, but library works in HWC, so we need transpose.

Read more comments on GitHub >

github_iconTop 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 >

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