I had a look at your code and there still were some issues. This should work now:
class augmented1(nn.Module):
def __init__(self,ni):
super(augmented1, self).__init__()
self.conv1 = conv_layer(ni,ni//2,kernel_size=1)
self.conv2 = conv_layer(ni//2,ni,kernel_size=3)
self.classifier = nn.Linear(ni*7*7,751)
def forward(self,x):
x = self.conv2(self.conv1(x))
x = x.view(x.size(0), -1)
return self.classifier(x)
class augmented2(nn.Module):
def __init__(self,ni):
super(augmented2, self).__init__()
self.conv1 = conv_layer(ni,ni//2,kernel_size=1)
self.conv2 = conv_layer(ni//2,ni,kernel_size=3)
self.classifier = nn.Linear(ni*7*7,1360)
def forward(self,x):
x = self.conv2(self.conv1(x))
x = x.view(x.size(0), -1)
return self.classifier(x)
class hybrid_cnn(nn.Module):
def __init__(self,**kwargs):
super(hybrid_cnn,self).__init__()
resnet = models.resnet50(pretrained=False)
self.base = nn.Sequential(*list(resnet.children())[:-2])
setattr(self,"fc0",augmented1(2048))
setattr(self,"fc1",augmented2(2048))
def forward(self,x):
x = self.base(x)
#x = F.avg_pool2d(x,x.size()[2:])
#print(x.shape)
clf_outputs = {}
num_fcs = 2
for i in range(num_fcs):
clf_outputs["fc%d" %i] = getattr(self, "fc%d" %i)(x)
return clf_outputs, x
model = hybrid_cnn()
n1 = 2048
x = torch.randn(1, 3, 224, 224)
model(x)
In your last code snippet you still passed a flat tensor to the augmentedX
layers, which cannot work, since they expect a 4-dimensional input ([batch_size, channels, height, width]
).
I removed this part.
Also you are using F.avg_pool(x, x.size()[2:])
which yields an output of dimension [batch_size, channels, 1, 1]
.
This cannot work in your sub-models, since you are trying to use a kernel size of 3 in one point.
I removed this part as well.
Now you pass an activation of [batch_size, 2048, 7, 7]
to the sub-modules.
I had to increase the in_features
in both Linear
layers to match this input.