DenseNet, Sizes of tensors must match
would you know how I can adapt this code so that sizes of tensors must match because I have this error: x = torch.cat([x1,x2],1) RuntimeError: Sizes of tensors must match except in dimension 0. Got 32 and 1 (The offending index is 0).
My images are size 416x416.
Thank you in advance for your help,
num_classes = 20
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
                
        self.inc = models.inception_v3(pretrained=True)
        self.inc.aux_logits = False

        for child in list(self.inc.children())[:-5]:
            for param in child.parameters():
                param.requires_grad = False

        self.inc.fc = nn.Sequential()
                    
        self.dens121 = models.densenet121(pretrained=True)

        for child in list(self.dens121.children())[:-6]:
            for param in child.parameters():
                param.requires_grad = False

        self.dens121 = nn.Sequential(*list(self.dens121.children())[:-1])
           
        self.SiLU = nn.SiLU()      
        self.linear = nn.Linear(4096, num_classes)
        self.dropout = nn.Dropout(0.2)
        
    def forward(self, x):
        x1 = self.SiLU(self.dens121(x))
        x1 = x1.view(-1, 2048)
        
        x2 = self.inc(x).view(-1, 2048)
        x = torch.cat([x1,x2],1)

        return self.linear(self.dropout(x))


The shapes of the two tensors are very different and that's why the torch.cat() fails. I tried to run your code with the following example:
def forward(self, x):
    x1 = self.SiLU(self.dens121(x))
    x1 = x1.view(-1, 2048)
        
    x2 = self.inc(x).view(-1, 2048)
    print(x1.shape, x2.shape)
    x = torch.cat([x1,x2], dim=1)

    return self.linear(self.dropout(x))

Here's the driver code
inputs = torch.randn(2, 3, 416, 416)
model = Net()
outputs = model(inputs)

The shapes of x1 of x2 are as follows:
torch.Size([169, 2048]) torch.Size([2, 2048])

Either your DenseNet should output the same shape as the output of Inceptionv3 or vice-versa. The output from DenseNet is of shape torch.Size([2, 1024, 13, 13]) and the output from Inceptionv3 is of shape torch.Size([2, 2048]).
EDIT
Add this line to the init method:
self.conv_reshape= nn.Conv2d(1024, 2048, kernel_size=13, stride=1)

Add these lines to your forward():
x1 = self.SiLU(self.dens121(x))

out = self.conv_reshape(x1)
x1 = out.view(-1, out.size(1))

x2 = self.inc(x).view(-1, 2048)

