Runtitme error in resnet18

Giuseppe: <p>Hy guys i have an error in runtime.<br>
The code is in below:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
from torchvision.models import resnet50
from torchvision.models import resnet18

class Encoder(nn.Module):

    def __init__(self):
        super(Encoder, self).__init__()
        self.model = resnet50(pretrained = True)
        ct = 0
        for child in self.model.children():
            ct += 1
            if ct &lt; 10:
                for param in child.parameters():
                    param.requires_grad = False

        self.model.fc = nn.Sequential(
                nn.Linear(self.model.fc.in_features,1024),
                nn.ReLU(),
                nn.Linear(1024, 512),
                nn.ReLU(),
                nn.Linear(512, 64),
                nn.ReLU())
    def forward(self, x):
        to_return = self.model(x)
        return to_return

class AutoEncoder(nn.Module):
    def __init__(self, enc):
        super(AutoEncoder, self).__init__()
        self.encoder = enc
        self.decoder = nn.Sequential(
            nn.Conv2d(1, 128, 3, padding=1),
            nn.Upsample(scale_factor=4, mode='bilinear'),
            nn.ReLU(),
            nn.Conv2d(128, 256, 3, padding=1),
            nn.Upsample(scale_factor=2, mode='bilinear'),
            nn.ReLU(),
            nn.Conv2d(256, 512, 3, padding=1),
            nn.Upsample(scale_factor=3.5,  mode='bilinear'),
            nn.ReLU(),
            nn.Conv2d(512, 2048, 3, padding=1),
            nn.ReLU(),
            nn.Conv2d(2048, 9, 3, padding=1),
        )

    def forward(self, x):

        print("x prinma dell'encoder",x.shape)
        code = self.encoder(x)
        print("x dopo dell'encoder", code.shape)
        code2 = code.view(1, 1, 8, 8)

        reconstructed = self.decoder(code2)

        return code, reconstructed

class MovementOrientation(nn.Module):
    def __init__(self):
        super(MovementOrientation, self).__init__()
        self.model = resnet18(pretrained = True)
        self.model.fc = nn.Linear(self.model.fc.in_features, 4)

    def forward(self, x):

        mov_ori = self.model(x)

        return mov_ori

class final_model(nn.Module):
    def __init__(self):
        super(final_model, self).__init__()
        encoder = Encoder()
        self.AutoEncoder = AutoEncoder(encoder)
        self.MovementOrientation = MovementOrientation()

    def forward(self, x):
        code, rec = self.AutoEncoder(x)
        MovementOrientation = self.MovementOrientation(rec)

        return rec, MovementOrientation
</code></pre>
<p>The error is “RuntimeError: Given groups=1, weight of size 64 3 7 7, expected input[1, 9, 224, 224] to have 3 channels, but got 9 channels instead”</p>
<p>I tried in resnet 50 (on encoder) to put  self.model.conv1.in_channels = 9 but i still error.<br>
And the error is in resnet 18 (this net take different dimension?)<br>
Can you help me?</p>

ptrblck: <aside class="quote no-group" data-username="Giuseppe" data-post="1" data-topic="95666">
<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/giuseppe/48/14328_2.png" class="avatar"> Giuseppe:</div>
<blockquote>
<p>I tried in resnet 50 (on encoder) to put self.model.conv1.in_channels = 9 but i still error.</p>
</blockquote>
</aside>
<p>Changing the <code>in_channels</code> attribute after creating the module won’t reinitialize the weights and would need to assign a new <code>nn.Conv2d</code> object to <code>self.model.conv1</code>, which accepts 9 input channels.</p>