Based on torch ResNet here,vision/resnet.py at a75fdd4180683f7953d97ebbcc92d24682690f96 · pytorch/vision · GitHub 2, try inheritance, such as,
class resnet18_fe(models.resnet.ResNet):
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        
        # erase layers you want
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x