https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-of-size-64-3-7-7-expected-input-3-1-224-224-to-have-3-channels-but-got-1-channels-instead/30153/28

QIQI: <p>Reported this error while I was running, can you help me fix it<br>
D:\Python3.8.6\lib\site-packages\torchvision\transforms\transforms.py:287: UserWarning:<br>
Argument interpolation should be of type InterpolationMode instead of int. Please, use I<br>
nterpolationMode enum.<br>
warnings.warn(</p>
<h1>
<a name="generator-parameters-902346-1" class="anchor" href="#generator-parameters-902346-1"></a>generator parameters: 902346</h1>
<h1>
<a name="discriminator-parameters-5215425-2" class="anchor" href="#discriminator-parameters-5215425-2"></a>discriminator parameters: 5215425</h1>
<p>0%|                                                           | 0/13 [00:05&lt;?, ?it/s]<br>
Traceback (most recent call last):<br>
File “train.py”, line 79, in <br>
fake_img = netG(z)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _cal<br>
l_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\63288\Desktop\CF\SRGAN-master\model.py”, line 91, in forward<br>
x = self.mct1(x)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _cal<br>
l_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\63288\Desktop\CF\SRGAN-master\model.py”, line 17, in forward<br>
out1 = self.conv1(x)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _cal<br>
l_impl<br>
return forward_call(*input, **kwargs)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\conv.py”, line 446, in forward<br>
return self._conv_forward(input, self.weight, self.bias)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\conv.py”, line 442, in _conv_f<br>
orward<br>
return F.conv2d(input, weight, bias, self.stride,<br>
RuntimeError: Given groups=1, weight of size [64, 64, 1, 1], expected input[64, 3, 22, 2<br>
2] to have 64 channels, but got 3 channels instead</p>

ptrblck: <aside class="quote no-group" data-username="QIQI" data-post="1" data-topic="173359">
<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/q/e68b1a/48.png" class="avatar"> QIQI:</div>
<blockquote>
<p>RuntimeError: Given groups=1, weight of size [64, 64, 1, 1], expected input[64, 3, 22, 2<br>
2] to have 64 channels, but got 3 channels instead</p>
</blockquote>
</aside>
<p><code>self.conv1</code> expects an input with 64 channels while your activation has 3 channels.<br>
Check where this conv layer is used and either change its <code>in_channels</code> or make sure the expected input is passed to it.</p>

QIQI: <p>Can you please make it clearer? Or can you tell me what to change to, because I’m just starting to learn convolution and don’t quite understand it yet</p>

ptrblck: <p>Your <code>netG</code> contains a module called <code>self.mct1</code>, which then calls into <code>self.conv1</code> with an input in the shape <code>[64, 3, 22, 22]</code> while it expects an input with <code>64</code> channels.<br>
Here is a small code snippet showing the error:</p>
<pre><code class="lang-python"># fails
conv1 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=1)
x = torch.randn(64, 3, 22, 22)
out = conv1(x)
# RuntimeError: Given groups=1, weight of size [64, 64, 1, 1], expected input[64, 3, 22, 22] to have 64 channels, but got 3 channels instead

# works
conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=1)
x = torch.randn(64, 3, 22, 22)
out = conv1(x)
</code></pre>

QIQI: <p>Still an error, I changed part of the code:<br>
class MCT(nn.Module):<br>
def <strong>init</strong>(self, channels):<br>
super(MCT, self).<strong>init</strong>()<br>
# 使用 3 个卷积层代替单个卷积层<br>
self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=1, stride=1, padding=4)<br>
self.conv2 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=4)<br>
self.conv3 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=5, stride=1, padding=4)<br>
Another error was reported:<br>
UserWarning: Argument interpolation should be of type InterpolationMode instead of int. Please, use Interpola<br>
tionMode enum.<br>
warnings.warn(</p>
<h1>
<a name="generator-parameters-765706-1" class="anchor" href="#generator-parameters-765706-1"></a>generator parameters: 765706</h1>
<h1>
<a name="discriminator-parameters-5215425-2" class="anchor" href="#discriminator-parameters-5215425-2"></a>discriminator parameters: 5215425</h1>
<p>0%|                                                                                                                                                           | 0/13 [00:05&lt;?, ?it/s]<br>
Traceback (most recent call last):<br>
File “train.py”, line 79, in <br>
fake_img = netG(z)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\63288\Desktop\CF\SRGAN-master\model.py”, line 92, in forward<br>
x = self.mct1(x)<br>
File “D:\Python3.8.6\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\63288\Desktop\CF\SRGAN-master\model.py”, line 31, in forward<br>
out = torch.cat((out1, out2, out3), dim=1)<br>
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 30 but got size 28 for tensor number 1 in the list.</p>

ptrblck: <p>The new error is:</p>
<pre><code class="lang-python">out = torch.cat((out1, out2, out3), dim=1)
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 30 but got size 28 for tensor number 1 in the list.
</code></pre>
<p>so make sure all tensors have the same shape except in <code>dim1</code>.</p>

QIQI: <p>So how do I change it? Do you have any good suggestions?</p>

QIQI: <p>I consulted a lot of information, I still will not fix this error, if you have time, please help me change and answer it, thank you very much!</p>