Given groups=1, weight of size [40, 3, 3, 3], expected input[16, 32, 33, 5] to have 3 channels, but got 32 channels instead

Hacking_Pirate: <p>Hi,<br>
I am creating a model for image classification but getting this error:<br>
<code>Given groups=1, weight of size [40, 3, 3, 3], expected input[16, 32, 33, 5] to have 3 channels, but got 32 channels instead</code><br>
I am using an efficientnet_b3 model.<br>
Here is my dataset class:</p>
<pre><code class="lang-auto">class HolidayDataset(Dataset):
    def __init__(self, train_path, df, transform=None):
        self.train_path = train_path
        self.df = df
        self.transform = transform
        
    def __len__(self):
        return len(self.df)
    
    def __getitem__(self, idx):
        img_name = str(self.df.Image[idx])
        target = str(self.df.Class[idx])
        img_path = self.train_path + img_name
        img = Image.open(img_path)
        img = np.array(img)
        if target == "Miscellaneous":
            target = 0
        elif target == "Christmas_Tree":
            target = 1
        elif target == "Jacket":
            target = 2
        elif target == "Candle":
            target = 3
        elif target == "Airplane":
            target = 4
        elif target == "Snowman":
            target = 5
        if self.transform:
            img = self.transform(image=img)["image"]
        return {
            "img": torch.tensor(img, dtype=torch.float),
            "target": torch.tensor(target, dtype=torch.float)
        }
</code></pre>

Hacking_Pirate: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> do you have any idea?<br>
Thanks in advanced! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

ptrblck: <p>The error is raised by an <code>nn.Conv2d</code> layer, which expects an input with 3 channels, while it seems you are trying to pass an input tensor with 5 channels (in a wrong memory layout) to this layer.<br>
Assuming my assumption is correct and your input contains 5 channels, you would have to first permute it to the channels-first format via <code>input = input.permute(0, 3, 1, 2)</code> and then either make sure to use 3 channels in the input tensor or change the first conv layer to accept 5 channels.</p>

Hacking_Pirate: <p>Thanks for your response.<br>
But the shape of the image is correct <code>torch.Size([32, 32, 3])</code></p>

ptrblck: <p>The posted input is still in channels-last format and thus wrong.<br>
Are you seeing the same or a different error, if your permute the input?</p>

Hacking_Pirate: <aside class="quote no-group" data-username="ptrblck" data-post="3" data-topic="107577">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>input = input.permute(0, 3, 1, 2)</p>
</blockquote>
</aside>
<p>Now I am seeing this ```ValueError: Target size (torch.Size([16])) must be the same as input size (torch.Size([16, 6]))</p>
<pre><code class="lang-auto"></code></pre>

Hacking_Pirate: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks I fixed that error but now I am getting this</p>
<pre><code class="lang-auto">RuntimeError: stack expects each tensor to be equal size, but got [64, 64, 3] at entry 0 and [64, 64] at entry 9
</code></pre>

ptrblck: <p>I don’t know, which loss function you are using, but assume you’ve solved the shape mismatch error.<br>
Could you post the complete stack trace from the new error, as I also don’t know which part of the code raises it.<br>
If this error is raised from the <code>DataLoader</code>, make sure that all images are 3 channel images in the channels-first format. Currently it seems <code>entry0</code> is still in channels-last and <code>entry9</code> is a grayscale image without a channel dimension.</p>

Hacking_Pirate: <p>I am using nn.CrossEntropyLoss()<br>
Here is the full stack trace:</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-16-2f18cc448813&gt; in &lt;module&gt;
----&gt; 1 run_training()

&lt;ipython-input-15-8e23b7e5c3d9&gt; in run_training()
     12         print(f'training on: {device}')
     13         for epoch in tqdm(range(CFG["epochs"])):
---&gt; 14             trn_loss = train_epoch(train_loader, model, loss_fn, optimizer, device)
     15             with torch.no_grad():
     16                 val_loss, valid_preds = valid_epoch(val_loader, model, loss_fn, device)

&lt;ipython-input-13-c839bbc89ccd&gt; in train_epoch(dataloader, model, loss_fn, optimizer, device, scheduler)
      2     model.train()
      3     final_loss = 0
----&gt; 4     for data in dataloader:
      5         img = data["img"].to(device)
      6         img = img.permute(0, 3, 1, 2)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    433         if self._sampler_iter is None:
    434             self._reset()
--&gt; 435         data = self._next_data()
    436         self._num_yielded += 1
    437         if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
   1083             else:
   1084                 del self._task_info[idx]
-&gt; 1085                 return self._process_data(data)
   1086 
   1087     def _try_put_index(self):

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
   1109         self._try_put_index()
   1110         if isinstance(data, ExceptionWrapper):
-&gt; 1111             data.reraise()
   1112         return data
   1113 

/opt/conda/lib/python3.7/site-packages/torch/_utils.py in reraise(self)
    426             # have message field
    427             raise self.exc_type(message=msg)
--&gt; 428         raise self.exc_type(msg)
    429 
    430 

RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 73, in default_collate
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 73, in &lt;dictcomp&gt;
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [64, 64, 3] at entry 0 and [64, 64] at entry 9
</code></pre>

ptrblck: <p>I think my assumption is correct and you would have to make sure all images have the same shape so that the <code>DataLoader</code> can stack them to create a batch.</p>
<p><code>nn.CrossEntropyLoss</code> expects an output in the shape <code>[batch_size, nb_classes]</code> and a target in <code>[batch_size]</code> containing class indices in the range <code>[0, nb_classes-1]</code> for a multi-classification use case.</p>

Hacking_Pirate: <p>Thanks, I fixed it. Turns out the way I was opening the image was wrong, I was using PIL.Image.open. Now I am using CV2 to open then and convert BGR2RGB.</p>