0

As far as I know, there is no way to read in 64-bit TIFF images using Pytorch's default image loaders. But, I know that rasterio has this ability. I have a custom utils file that implements several classes and functions from torchvision.datasets. It works fine for PNG files. But to accommodate the 64-bit TIFF images, I added the following two functions to this file:

def accimage_loader(path: str) -> Any:
    import torch
    import numpy as np
    import accimage
    import rasterio

    try:
        return accimage.Image(path)
    except:
        #64 bit tif
        openedImage = rasterio.open(path).read()
        #convert to numpy array if downsample is necessary for transform
        openedImage = np.asarray(openedImage)

        #convert to tensor
        openedImage = torch.tensor(openedImage).numpy().transpose(1,2,0)
        openedImage = torch.tensor(openedImage)
        return openedImage

def default_loader(path: str) -> Any:
    from torchvision import get_image_backend

    if get_image_backend() == "accimage":
        return accimage_loader(path)
    else:
        return pil_loader(path)

These are very similar to the accimage_loader() and default_loader() in torchvision.datasets.folder: https://pytorch.org/vision/stable/_modules/torchvision/datasets/folder.html#ImageFolder. In my code, I define the training/testing sets using my CustomImageFolder() class in my utils file (not shown). Then, I pass it to the torch.utils.data.DataLoader() class. For example,

trainLoader = torch.utils.data.DataLoader(training_set, batch_size=8, sampler=trainSampler)

It is throwing the error when I try to train my network. Specifically, at the start of the for-loop:

for batch_idx, (data, labels) in enumerate(trainLoader):
.
.
.

The error says

raise TypeError(f"Input image tensor permitted channel values are {permitted}, but found {c}")
TypeError: Input image tensor permitted channel values are [3], but found 128

Not sure what this means... Any suggestions/insights?

Short of just converting the images into an accepted data format (like PNG), does anyone have any suggestions as to how I can go about reading in these 64-bit TIFF files?

  • It probably means you have high-dimensionality, or multi-spectral data. Use `exiftool` to check and click [edit] to add the output to your question. See https://stackoverflow.com/a/73170738/2836621 – Mark Setchell Aug 31 '22 at 11:19

1 Answers1

0

I am not sure, but, maybe it means it expects an image which has at maximum 3 channels like RGB (Red, Green, Blue) images. Maybe, in your image you have more than 3 channels. For example, it could have these channels: Red, Green, Blue, NIR or ... .

best wishes

john
  • 9
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 11 '22 at 08:36