Runtitme error in resnet18

Hy guys i have an error in runtime.
The code is in below:

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 < 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

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”

I tried in resnet 50 (on encoder) to put self.model.conv1.in_channels = 9 but i still error.
And the error is in resnet 18 (this net take different dimension?)
Can you help me?

Changing the in_channels attribute after creating the module won’t reinitialize the weights and would need to assign a new nn.Conv2d object to self.model.conv1, which accepts 9 input channels.