Hello Everyone,
Here is the model:
‘’’
class Net(nn.Module):
def init(self):
super().init()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size= 5)
self.pool = nn.MaxPool2d(kernel_size=2,stride= 2)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size= 5)
self.fc1 = nn.Linear(in_features=400, out_features=120)
self.fc2 = nn.Linear(in_features=120, out_features=84)
self.fc3 = nn.Linear(in_features=84, out_features=10)
def forward(self, x):
con_1_output = self.pool(F.relu(self.conv1(x)))
con_2_output = self.pool(F.relu(self.conv2(con_1_output))) #([4, 16, 5, 5])
flatten_values = torch.flatten(con_2_output, 1) # flatten all dimensions except batch [batch_size, in_features] [4, 400]
fc1_output = F.relu(self.fc1(flatten_values)) # output x.shape = [4, 120]
fc2_output = F.relu(self.fc2(fc1_output))
fc3_output = self.fc3(fc2_output)
return fc3_output
‘’’
I am creating a new model to extract the feature vector of the FC1
new_model = nn.Sequential(*list(save_net.children())[:-2])
Output is:
‘’’
Sequential(
(0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(3): Linear(in_features=400, out_features=120, bias=True)
)
‘’’
Problem: When I am passing the test image in the new model I am getting an error:
‘’’
RuntimeError: mat1 and mat2 shapes cannot be multiplied (640x10 and 400x120)
‘’’