RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 8, 600, 800] to have 3 channels, but got 8 channels instead

Mona_Jalal: <p>So, if I have permute line uncommented I get another error but here is the commented version error:</p>
<p><code>RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 8, 600, 800] to have 3 channels, but got 8 channels instead</code><br>
batch size is 8 and the code is:</p>
<pre><code class="lang-auto">network = Network()
network.cuda()    

criterion = nn.MSELoss()
optimizer = optim.Adam(network.parameters(), lr=0.0001)

loss_min = np.inf
num_epochs = 1

start_time = time.time()
for epoch in range(1,num_epochs+1):
    
    loss_train = 0
    loss_test = 0
    running_loss = 0
    
    
    network.train()
    print('size of train loader is: ', len(train_loader))

    for step in range(1, len(train_loader)+1):

        
        batch = next(iter(train_loader))
        images, landmarks = batch['image'], batch['landmarks']
        print(images.shape)
        #images = images.permute(0,3,1,2)
       
        images = images.unsqueeze_(0)

        images = images.cuda()
    
        landmarks = landmarks.view(landmarks.size(0),-1).cuda() 
        norm_image = transforms.Normalize(0.3812, 0.1123) 
        for image in images:
            image = image.float()
            ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;
            image = norm_image(image)
        
        ###removing landmarks normalize because of the following error
        ###ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([8, 8])
       
         
        for i in range(8):
            if(i%2==0):
                landmarks[:,i] = landmarks[:,i]/800
            else:
                landmarks[:,i] = landmarks[:,i]/600
                
        print(landmarks.shape)
        print(landmarks)
        
        

        
        norm_landmarks = transforms.Normalize(0.4949, 0.2165)
        landmarks [landmarks != landmarks] = 0
        landmarks = landmarks.unsqueeze_(0)
        landmarks = norm_landmarks(landmarks)
        
        predictions = network(images)
        
        # clear all the gradients before calculating them
        optimizer.zero_grad()
        
        print('predictions are: ', predictions.float())
        print('landmarks are: ', landmarks.float())
        # find the loss for the current step
        loss_train_step = criterion(predictions.float(), landmarks.float())
        
        
        loss_train_step = loss_train_step.to(torch.float32)
        print("loss_train_step before backward: ", loss_train_step)
        
        # calculate the gradients
        loss_train_step.backward()
        
        # update the parameters
        optimizer.step()
        
        print("loss_train_step after backward: ", loss_train_step)

        
        loss_train += loss_train_step.item()
        
        print("loss_train: ", loss_train)
        running_loss = loss_train/step
        print('step: ', step)
        print('running loss: ', running_loss)
        
        print_overwrite(step, len(train_loader), running_loss, 'train')
        
    network.eval() 
    with torch.no_grad():
        
        for step in range(1,len(test_loader)+1):
            
            batch = next(iter(train_loader))
            images, landmarks = batch['image'], batch['landmarks']
            images = images.permute(0,3,1,2)
            images = images.cuda()
            landmarks = landmarks.view(landmarks.size(0),-1).cuda()
        
            predictions = network(images)

            # find the loss for the current step
            loss_test_step = criterion(predictions, landmarks)

            loss_test += loss_test_step.item()
            running_loss = loss_test/step

            print_overwrite(step, len(test_loader), running_loss, 'Validation')
    
    loss_train /= len(train_loader)
    loss_test /= len(test_loader)
    
    print('\n--------------------------------------------------')
    print('Epoch: {}  Train Loss: {:.4f} Valid Loss: {:.4f}'.format(epoch, loss_train, loss_test))
    print('--------------------------------------------------')
    
    if loss_test &lt; loss_min:
        loss_min = loss_test
        torch.save(network.state_dict(), '../moth_landmarks.pth') 
        print("\nMinimum Valid Loss of {:.4f} at epoch {}/{}".format(loss_min, epoch, num_epochs))
        print('Model Saved\n')
     
print('Training Complete')
print("Total Elapsed Time : {} s".format(time.time()-start_time))
</code></pre>
<p>The complete error is:</p>
<pre><code class="lang-auto">size of train loader is:  90
torch.Size([8, 600, 800])
torch.Size([8, 8])
tensor([[0.6214, 0.4175, 0.7300, 0.2883, 0.4338, 0.2167, 0.5698, 0.5773],
        [0.6296, 0.4045, 0.9138, 0.4100, 0.4232, 0.4242, 0.7422, 0.5297],
        [0.6059, 0.4002, 0.7562, 0.2767, 0.3538, 0.3033, 0.5529, 0.5455],
        [   nan,    nan, 0.7240, 0.2722, 0.3900, 0.2567, 0.5168, 0.5933],
        [0.6147, 0.4081, 0.8538, 0.3400, 0.3663, 0.3150, 0.5142, 0.4875],
        [0.6146, 0.4124, 0.8800, 0.4867, 0.3800, 0.4500, 0.5106, 0.5524],
        [0.6150, 0.3935, 0.8696, 0.5158, 0.4647, 0.5329, 0.6041, 0.5153],
        [   nan,    nan, 0.8850, 0.2817, 0.5112, 0.2183, 0.7184, 0.5436]],
       device='cuda:0', dtype=torch.float64)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-23-0a092d64d0a9&gt; in &lt;module&gt;
     59         landmarks = norm_landmarks(landmarks)
     60 
---&gt; 61         predictions = network(images)
     62 
     63         # clear all the gradients before calculating them

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

&lt;ipython-input-10-46116d2a7101&gt; in forward(self, x)
     10     def forward(self, x):
     11         x = x.float()
---&gt; 12         out = self.model(x)
     13         return out

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

~/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py in forward(self, x)
    218 
    219     def forward(self, x):
--&gt; 220         return self._forward_impl(x)
    221 
    222 

~/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)
    201     def _forward_impl(self, x):
    202         # See note [TorchScript super()]
--&gt; 203         x = self.conv1(x)
    204         x = self.bn1(x)
    205         x = self.relu(x)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
    417 
    418     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 419         return self._conv_forward(input, self.weight)
    420 
    421 class Conv3d(_ConvNd):

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    414                             _pair(0), self.dilation, self.groups)
    415         return F.conv2d(input, weight, self.bias, self.stride,
--&gt; 416                         self.padding, self.dilation, self.groups)
    417 
    418     def forward(self, input: Tensor) -&gt; Tensor:

RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 8, 600, 800] to have 3 channels, but got 8 channels instead
</code></pre>
<p>How should I exactly learn how to fix this error since I keep having similar errors.</p>
<pre><code class="lang-auto">$ identify -verbose  frame410.png
Image: frame410.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 800x600+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Grayscale
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    Pixels: 480000
    Gray:
      min: 17  (0.0666667)
      max: 176 (0.690196)
      mean: 97.3367 (0.381713)
      standard deviation: 28.8465 (0.113123)
      kurtosis: -0.0480917
      skewness: 0.427879
      entropy: 0.926528
  Colors: 160
</code></pre>
<p>This is the info of one of my frames.</p>
<p>If I comment the unsqueez line:<br>
<code>##images = images.unsqueeze_(0)</code><br>
I have:</p>
<pre><code class="lang-auto">size of train loader is:  90
torch.Size([8, 600, 800])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-24-ed8d85973797&gt; in &lt;module&gt;
     36             image = image.float()
     37             ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;
---&gt; 38             image = norm_image(image)
     39 
     40         ###removing landmarks normalize because of the following error

~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py in __call__(self, tensor)
    210             Tensor: Normalized Tensor image.
    211         """
--&gt; 212         return F.normalize(tensor, self.mean, self.std, self.inplace)
    213 
    214     def __repr__(self):

~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
    282     if tensor.ndimension() != 3:
    283         raise ValueError('Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = '
--&gt; 284                          '{}.'.format(tensor.size()))
    285 
    286     if not inplace:

ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([600, 800]).
</code></pre>
<p>I understand this is because resnet is expecting 3 channel images. If I don’t want to change the resnet code, and also don’t want to change the images, is there another hack to use within the pytorch or something like below is the only hack?</p>
<pre><code class="lang-auto">import cv2
import numpy as np
img = cv2.imread('110249.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = np.zeros_like(img)
img2[:,:,0] = gray
img2[:,:,1] = gray
img2[:,:,2] = gray
cv2.imwrite('110249.jpg', img2)
</code></pre>

Sanjayvarma11: <p>It seems the input to model should have 3 channels but the input is having 8 channels.<br>
if you look at your input size the size is torch.Size([8, 600, 800]).It means you are having 8 channels but you are saying batch size is 8.so if your input is black and white so firstly try to apply unsqueeze(1) to that image_tensor  so your shape becomes [8,1,600,800].Then reply me what image are you using like black and white or color etc</p>

Mona_Jalal: <aside class="quote no-group" data-username="Sanjayvarma11" data-post="2" data-topic="99582">
<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/sanjayvarma11/48/20339_2.png" class="avatar"> Sanjayvarma11:</div>
<blockquote>
<p>unsqueeze(1)</p>
</blockquote>
</aside>
<p>This is the error I get with unsqueeze(1) Thanks for your reply</p>
<p><code>RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[8, 1, 600, 800] to have 3 channels, but got 1 channels instead</code></p>
<p>I don’t want to change the code of resnet.</p>

fadetoblack: <p>A simple fix for this is to copy your input across three channels so that your input shape is [8,3,600,800] instead of [8,1,600,800].</p>

Mona_Jalal: <p>how can I do this in pytorch?</p>

fadetoblack: <pre><code class="lang-auto"># x is your image with shape [8,1,600,800]
x2 = torch.cat((x,x,x),1)
</code></pre>

Mona_Jalal: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/e/5ebc95a3e249223876bc2560a5f83e61e1144e3a.png" data-download-href="https://discuss.pytorch.org/uploads/default/5ebc95a3e249223876bc2560a5f83e61e1144e3a" title="Screenshot_2020-10-16_03-23-51"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/e/5ebc95a3e249223876bc2560a5f83e61e1144e3a_2_690x281.png" alt="Screenshot_2020-10-16_03-23-51" data-base62-sha1="dw4VcVDDo5khy54PfsYGHrDBdP4" width="690" height="281" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/e/5ebc95a3e249223876bc2560a5f83e61e1144e3a_2_690x281.png, https://discuss.pytorch.org/uploads/default/original/3X/5/e/5ebc95a3e249223876bc2560a5f83e61e1144e3a.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/e/5ebc95a3e249223876bc2560a5f83e61e1144e3a.png 2x" data-dominant-color="E7EAE9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot_2020-10-16_03-23-51</span><span class="informations">996×406 34.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">##[8, 600, 800] --&gt; [8,3,600,800]
t1 = torch.rand((8,600,800))
t1 = t1.unsqueeze(1)
t1 = torch.cat((t1, t1, t1), 1)
t1.shape
</code></pre>