import torch
import torchvision
from torch import nn
from torchsummary import summary
model = torchvision.models.resnet18(pretrained=False)
model.avgpool = nn.Identity()
model.fc = nn.Identity()
x = torch.randn(size=(2,3,128,128))
y = model(x)
print(x.shape)
print(y.shape)
the output is
torch.Size([2, 3, 128, 128])
torch.Size([2, 8192])
The shape of the last few layers(using torchsummary ):
BatchNorm2d-64 [2, 512, 4, 4]
ReLU-65 [2, 512, 4, 4]
BasicBlock-66 [2, 512, 4, 4]
Identity-67 [2, 512, 4, 4]
Identity-68 [2, 8192]
I don’t understand why the last identity layer flattens the image