I’m building a binary classification model based on 2 inputs: images and numeric data.
I’ve encountered a PyTorch tutorial on building such a model and built my model in a similar way:
class MixedNetwork(nn.Module):
def __init__(self):
super(MixedNetwork, self).__init__()
image_modules = list(models.resnet50().children())[:-1]
self.image_features = nn.Sequential(*image_modules)
self.landmark_features = nn.Sequential(
nn.Linear(in_features=96, out_features=192,bias=False),
nn.ReLU(inplace=True),
nn.Dropout(p=0.25),
nn.Linear(in_features=192,out_features=1000,bias=False),
nn.ReLU(inplace=True),
nn.Dropout(p=0.25))
self.combined_features = nn.Sequential(
nn.Linear(3048, 512),
nn.ReLU(),
nn.Linear(512, 32),
nn.ReLU(),
nn.Linear(32,1))
def forward(self, image, landmarks):
a = self.image_features(image)
# print("shape of a", a.shape)
b = self.landmark_features(landmarks)
# print("shape of b", b.shape)
x = torch.cat((a.view(a.size(0), -1), b.view(b.size(0), -1)), dim=1)
x = self.combined_features(x)
x = torch.sigmoid(x)
return x
How can I make sure that the model is using pre-trained resnet50 weights?
Specify the weights in models.resnet50 to the desired set of pretrained weights or use the older pretrained=True argument to use the pretrained resnet.
torchvision.models.vgg16 uses an adaptive pooling layer to avoid shape mismatch errors in the linear layer. Could you describe if and what you’ve changed in the model which raises the shape mismatch?
It’s unclear to me how you are executing the model, so please post a minimal and executable code snippet to reproduce the error.
Based on the already posted code snippet I would assume you are breaking the forward pass by trying to wrap some modules into an nn.Sequential container without making sure the functional API calls from the original model’s forward method are also used.
Your code is unfortunately not executable, so please update it so that I could run it without any dataset dependencies to reproduce the issue and debug it further.