If I have the dataset as two arrays X and y as images and labels, both are numpy arrays. I want to apply transforms (like those from models given by the pretrainedmodels package), how can apply them on my data, especially as the way as datasets.ImageFolder.
My numpy arrays are converted from PIL Images, and I found how to convert numpy arrays to dataset loaders here.
Thanks @yvanscher. But it seems that transforms accept img as input, not tensors. See the pil_loader function and DatasetFolder.__getitem__ method here.
Now I decide to save the PIL image in the numpy arrays, without converting to numpy array with np.array(img). Then I might be able to use transform, and then TensorDataset and DataLoader …
You can also write a custom transformer which is very easy:
def __init__(self, size):
assert isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
if isinstance(size, int):
self._size = (size, size)
self._size = size
def __call__(self, img: np.ndarray):
resize_image = skimage.transform.resize(img, self._size)
# the resize will return a float64 array
I love this image augmentation library called imgaug. Sooooo nice!
I’m super late to the party but I came across this great repo that rewrote the base class to just use numpy arrays for the transformations
Please look at the following example:
import numpy as np
from torchvision import transforms
X = np.random.randint(low=0, high=255, size=(32,32,3), dtype=np.unit8) # an image in ndarray format
your_transforms = transforms.Compose([transforms.CenterCrop(10),transforms.ToTensor(),]) # a set of transformations
if isinstance(X, np.ndarray):
Now given what you found on the other page, we can do this:
from torchvision.datasets import VisionDataset
def __init__(self, images, targets, transform=None, target_transform=None):
if isinstance(images, np.ndarray):
super(BasicVisionDataset, self).__init__(root=None, transform=transform, target_transform=target_transform)
assert len(images) == len(targets)
self.images = images
self.targets = targets
def __getitem__(self, index):
return self.transform(self.images[index]), self.targets[index]
Please let me know if this works.