LeanderK:

Image.fromarray needs a single image, and not the dataset.


I assumed np_arr is just one image, not the entire dataset. Then, you would need to iterate through np_arr to get each image, convert them to image using Image.fromarray() and then apply the transformations on that image.



 LeanderK:

Also, if I follow your code, the transformation is the same for every epoch. It’s also not how it’s used in the tutorial


Everytime __getitem__ is called, a random transformation will be performed on the image.
I have a pseudo code as follows (assume data_arr is the entire dataset, with shape 1000, 784 corresponding to 1000 images of size 28x28)
class CelebA(data.Dataset):

    def __init__(self):
          self.data_arr = ... # define the data-array (load from file)
          self.labels = ... # define the labels

          self.transform = transforms.Compse([
              transforms.RandomCrop(20),
              transforms.RandomHorizontalFlip(),
              transforms.ToTensor()])

    def __getitem(self, index):
         np_arr = self.data_arr[index, :]
         y = self.labels[index]

         ## reshape np_arr to 28x28
         np_arr = np_arr.reshape(28, 28)

         ## convert to PIL-image
         img = Image.fromarray((np_arr*255).astype('uint8'))

         #apply the transformations and return tensors
         return self.transform(img), torch.FloatTensor(y)
         

Note that this code is not complete, and I just wanted to show the general idea on how to make a class Dataset from a numpy array.