I want to add layer normalization function just after AdaptiveAvgPool2d layer and L2 normalization after fc layer. i wanted my fc layer output to be 200 so tried not to include fc layer instead of it make new fc layer, but it did not remove fc layers comes with pretrained model, i am using googlenet.
my code:
class GoogleNet(nn.Module):
def __init__(self):
super(GoogleNet,self).__init__()
self.model = googlenet_pytorch.GoogLeNet.from_pretrained('googlenet')
self.fc = nn.Linear(1024,200, bias=False),
def forward(self, x):
batch_size ,_,_,_ =x.shape
x = self.model.extract_features(x)
x = self.model._avg_pooling(x)
x = F.layer_norm(x,x.size[1:],elementwise_affine=False)
x = self.fc(x)
x = F.normalize(x, p=2, dim=1)
return x
Output I am getting:
.....
.....
.....
(aux1): InceptionAux(
(conv): BasicConv2d(
(conv): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)
)
(fc1): Linear(in_features=2048, out_features=1024, bias=True)
(fc2): Linear(in_features=1024, out_features=1000, bias=True)
)
(aux2): InceptionAux(
(conv): BasicConv2d(
(conv): Conv2d(528, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)
)
(fc1): Linear(in_features=2048, out_features=1024, bias=True)
(fc2): Linear(in_features=1024, out_features=1000, bias=True)
)
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
(dropout): Dropout(p=0.2, inplace=False)
(fc): Linear(in_features=1024, out_features=1000, bias=True)
)
)
Output I want:
......
......
......
(aux1): None
(aux2): None
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
** layer normalization here**
(dropout): Dropout(p=0.2, inplace=False)
(fc): Linear(in_features=1024, out_features=200, bias=False)
**L2 normalization here**
)
If someone needs to know the solution code for this problem, with the help of iacob's answer I solved it, I added it as an answer.