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?