I cannot reproduce this issue by using an input tensor of [batch_size, 3, 416, 416]
and am running into a shape mismatch error before.
Changing x.view(-1, 2048)
to x.view(x.size(0), -1)
and adapting the expected in_features
in self.linear
works:
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(175104, num_classes)
self.dropout = nn.Dropout(0.2)
def forward(self, x):
x1 = self.SiLU(self.dens121(x))
x1 = x1.view(x.size(0), -1)
x2 = self.inc(x).view(x.size(0), -1)
x = torch.cat([x1,x2],1)
return self.linear(self.dropout(x))
model = Net()
x = torch.randn(2, 3, 416, 416)
out = model(x)