TypeError: pic should be Tensor or ndarray. Got <class 'NoneType'>

Hello, I’m a beginner to PyTorch, and I don’t understand why I’m getting the error in the title when I declare images, labels = dataiter.next(). Below is everything relevant

class DogsDataset(Dataset):
    def __init__(self, df_data, data_dir = './',  transform=None):
        super().__init__()
        self.df = df_data.values
        self.data_dir = data_dir
        # self.img_ext = img_ext
        self.transform = transform

    def __len__(self):
        return len(self.df)
    
    def __getitem__(self, index):
        img_name,label = self.df[index]
        img_path = os.path.join(self.data_dir, img_name)
        image = cv2.imread(img_path)
        if self.transform is not None:
            image = self.transform(image)
        return image, label
data_transform = transforms.Compose([
        transforms.ToPILImage(),
        transforms.RandomHorizontalFlip(),
        transforms.Resize(32),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])])

#added
df_labels = pd.read_csv(LABELS_CSV_PATH)
train_data = DogsDataset(df_data=df_labels, data_dir=TRAIN_IMG_PATH, transform=data_transform)
# Set Batch Size
batch_size = 64

# Percentage of training set to use as validation
valid_size = 0.2

# obtain training indices that will be used for validation
num_train = len(train_data)
indices = list(range(num_train))
np.random.shuffle(indices)
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]

# Create Samplers
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)

# prepare data loaders (combine dataset and sampler)
train_loader = DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
valid_loader = DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler)
dataiter = iter(train_loader)
images, labels = dataiter.next()
TypeError                                 Traceback (most recent call last)
<ipython-input-10-a3862cdf9037> in <module>
      1 # obtain one batch of training images
      2 dataiter = iter(train_loader)
----> 3 images, labels = dataiter.next()
      4 print(images)

/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    558         if self.num_workers == 0:  # same-process loading
    559             indices = next(self.sample_iter)  # may raise StopIteration
--> 560             batch = self.collate_fn([self.dataset[i] for i in indices])
    561             if self.pin_memory:
    562                 batch = _utils.pin_memory.pin_memory_batch(batch)

/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py in <listcomp>(.0)
    558         if self.num_workers == 0:  # same-process loading
    559             indices = next(self.sample_iter)  # may raise StopIteration
--> 560             batch = self.collate_fn([self.dataset[i] for i in indices])
    561             if self.pin_memory:
    562                 batch = _utils.pin_memory.pin_memory_batch(batch)

<ipython-input-6-ee5990aa6797> in __getitem__(self, index)
     16         image = cv2.imread(img_path)
     17         if self.transform is not None:
---> 18             image = self.transform(image)
     19         return image, label

/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py in __call__(self, img)
     59     def __call__(self, img):
     60         for t in self.transforms:
---> 61             img = t(img)
     62         return img
     63 

/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py in __call__(self, pic)
    125 
    126         """
--> 127         return F.to_pil_image(pic, self.mode)
    128 
    129     def __repr__(self):

/opt/conda/lib/python3.6/site-packages/torchvision/transforms/functional.py in to_pil_image(pic, mode)
    110     """
    111     if not(isinstance(pic, torch.Tensor) or isinstance(pic, np.ndarray)):
--> 112         raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))
    113 
    114     elif isinstance(pic, torch.Tensor):

TypeError: pic should be Tensor or ndarray. Got <class 'NoneType'>.

Hi, can you please add printed backtrace of the error?

The traceback has been added in my original post.

It seems your code is unable to read image. Can you make sure you are doing it correctly by printing type of image in this line:

By they way, if you are not going to do any specific preprocessing using cv2, I think it is better idea to load your images using Pillow which is the default image processing core of PyTorch, then you can remove first transform layer too:

You can read image by Pillow using this code:

from PIL import Image
image = Image.open(path)

Thank you! Added in

self.img_ext = img_ext

and

from PIL import Image
image = Image.open(path)

to fix the problem

You’re welcome. Feel free to ask your questions.

Good luck

@Cerebrus I get the same issue. Please where did you add self.img_ext =img_ext in your code?

Can you kindly show the updated version of your DogsDataset class which worked?