Why does Pytorch claim my input has 4 channels?

wdesert: <p>I was trying to train an SRGAN-model on my dataset of text images. Those are 100 160x30 .png files.</p>
<p>Here is the model: <a href="https://github.com/leftthomas/SRGAN/blob/master/model.py" rel="nofollow noopener">https://github.com/leftthomas/SRGAN/blob/master/model.py</a><br>
<a href="https://github.com/leftthomas/SRGAN/blob/master/train.py" rel="nofollow noopener">https://github.com/leftthomas/SRGAN/blob/master/train.py</a><br>
(I didn’t change anything apart from the path to my dataset)</p>
<p>I set the crop_size to 24, the upscale to 2 and start the training, but then the following error occurs:</p>
<p><code>RuntimeError: Given groups=1, weight of size [64, 3, 9, 9], expected input[64, 4, 12, 12] to have 3 channels, but got 4 channels instead</code></p>
<p>I am sure that all of my images have 3 channels, which is confirmed by img.shape.</p>
<p>Nevertheless, I am stuck and,honestly, I have no idea where exactly this fourth channel might be.</p>
<p>Can you please help me figure out what is wrong. I have googled similar discussions, but the most frequent piece of advice there is to make sure the input is not rgba, which in my case is not applicable.</p>
<p>Thanks.</p>

ptrblck: <p>I guess the 4. channel might be the alpha channel (in some images).<br>
If you are using a custom <code>Dataset</code>, you could easily check if and slice the input image if necessary.</p>

wdesert: <p>Thanks for answering. I have checked that several times. None of my images have the alpha channel. This is what makes me wonder…</p>

alex.veuthey: <p>You could try to iterate once over the data loader and check the size of the inputs, just to be sure?</p>
<p>This might help you locate the error more precisely (e.g. if all images have 4 channels, or if they have 3 channels up to this point and are modified later on…)</p>

ianweb100: <p>Hi again,</p>
<p>I had the same issue after loading all graphs direct from seaborn. I follow the face landmarks tutorial and your tips on how to load the data into the data loader.  I successfully load the data, saw the images ( so they were there), but then to my surprise</p>
<p>0 torch.Size([4, 224, 224])<br>
1 torch.Size([4, 224, 224])</p>
<p>So i follow another  topic you had on how to fix this then i used the code below:</p>
<pre><code>data_transforms = transforms.Compose([
transforms.Grayscale(num_output_channels=1),
transforms.Resize(256),
transforms.CenterCrop(256),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
                     std=[0.229, 0.224, 0.225] )])

transformed_dataset = FaceLandmarksDataset(csv_file='metadata.csv',
                                       root_dir="/figures/",
                                       transform = data_transforms )

for i in range(len(transformed_dataset)):
    sample = transformed_dataset[i]

    print(i, sample['image'].size())

    if i == 3:
        break

dataloader = DataLoader(transformed_dataset, batch_size=4,
                    shuffle=True, num_workers=4)
</code></pre>
<p>And I get the following error:</p>
<p>TypeError: img should be PIL Image. Got &lt;class ‘dict’&gt;</p>

Deeply: <aside class="quote no-group" data-username="ianweb100" data-post="5" data-topic="44632">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/9dc877/48.png" class="avatar"> ianweb100:</div>
<blockquote>
<p>FaceLandmarksDataset</p>
</blockquote>
</aside>
<p>seems that you are following the tutorial that uses <a href="http://skimage.io" rel="noopener nofollow ugc">skimage.io</a> to read the image which returns a <code>numpy</code> array, or <a href="https://stackoverflow.com/questions/55976670/skimage-imread-returns-img-arrayndarray-what-are-the-properties" rel="noopener nofollow ugc">some other object</a>. You will either need to transform the image to <code>PIL</code> or, replace <code>skimage</code>  <code>imread</code> with <code>PIL</code> image open, something like:</p>
<pre><code class="lang-auto">from PIL import Image
image = Image.open(img_name)
</code></pre>

ianweb100: <aside class="quote no-group" data-username="ianweb100" data-post="5" data-topic="44632">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/9dc877/48.png" class="avatar"> ianweb100:</div>
<blockquote>
<p>transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] )])</p>
</blockquote>
</aside>
<p>It turns out the issue is on the transforms normalise, i was using 3 channels for a 1 channel data. I now manage to easily load, shuffle and reload the data using imageFolder ( Thanks to Patrick ) and some other techniques I picked here in the forum.</p>
<p>Thanks for all the help.</p>