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

Cerebrus: <p>Hello, I’m a beginner to PyTorch, and I don’t understand why I’m getting the error in the title when I declare <code>images, labels = dataiter.next()</code>. Below is everything relevant</p>
<pre><code class="lang-auto">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
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto"># 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)
</code></pre>
<pre><code class="lang-auto">dataiter = iter(train_loader)
images, labels = dataiter.next()
</code></pre>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
&lt;ipython-input-10-a3862cdf9037&gt; in &lt;module&gt;
      1 # obtain one batch of training images
      2 dataiter = iter(train_loader)
----&gt; 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
--&gt; 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 &lt;listcomp&gt;(.0)
    558         if self.num_workers == 0:  # same-process loading
    559             indices = next(self.sample_iter)  # may raise StopIteration
--&gt; 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)

&lt;ipython-input-6-ee5990aa6797&gt; in __getitem__(self, index)
     16         image = cv2.imread(img_path)
     17         if self.transform is not None:
---&gt; 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:
---&gt; 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         """
--&gt; 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)):
--&gt; 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 &lt;class 'NoneType'&gt;.
</code></pre>

Nikronic: <p>Hi, can you please add printed backtrace of the error?</p>

Cerebrus: <p>The traceback has been added in my original post.</p>

Nikronic: <p>It seems your code is unable to read image. Can you make sure you are doing it correctly by printing type of <code>image</code> in this line:</p>
<aside class="quote no-group" data-username="Cerebrus" data-post="1" data-topic="48670">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cerebrus/48/12361_2.png" class="avatar"> Cerebrus:</div>
<blockquote>
<p>image = cv2.imread(img_path)</p>
</blockquote>
</aside>
<p>By they way, if you are not going to do any specific preprocessing using <code>cv2</code>, I think it is better idea to load your images using <code>Pillow</code> which is the default image processing core of PyTorch, then you can remove first transform layer too:</p>
<aside class="quote no-group" data-username="Cerebrus" data-post="1" data-topic="48670">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cerebrus/48/12361_2.png" class="avatar"> Cerebrus:</div>
<blockquote>
<p>transforms.ToPILImage(),</p>
</blockquote>
</aside>
<p>You can read image by <code>Pillow</code> using this code:</p>
<pre><code class="lang-auto">from PIL import Image
image = Image.open(path)
</code></pre>

Cerebrus: <p>Thank you! Added in</p>
<pre><code class="lang-auto">self.img_ext = img_ext

</code></pre>
<p>and</p>
<pre><code class="lang-auto">from PIL import Image
image = Image.open(path)
</code></pre>
<p>to fix the problem</p>

Nikronic: <p>You’re welcome. Feel free to ask your questions.</p>
<p>Good luck</p>

jimmase: <p><a class="mention" href="/u/cerebrus">@Cerebrus</a> I get the same issue. Please where did you add <code>self.img_ext =img_ext</code> in your code?</p>

jimmase: <p>Can you kindly show the updated version of your <code>DogsDataset class</code> which worked?</p>