PyTorch如何選出指定的Model Layer結構?

import torchvision.models as models
import torch.nn as nn
resnet18 = models.resnet18(pretrained=True)
#方法1和方法2都取出了ResNet18的前8層結構,只是slicing的方法不同而已,且前8層剛好位在classifier之前。要將第8層output的特徵拿來做t-SNE或是GradCAM輸出都可以features = nn.Sequential(*(list(resnet18.children())[0:8])) #方法1
features = nn.Sequential(*(list(resnet18.children())[:-2])) #方法2

可使用的 Pre-trained model

# alexnet = models.alexnet(pretrained=False)
# squeezenet = models.squeezenet1_0(pretrained=False)
# vgg16 = models.vgg16(pretrained=False)
# densenet = models.densenet161(pretrained=False)
# inception = models.inception_v3(pretrained=False)
# googlenet = models.googlenet(pretrained=False)
# shufflenet = models.shufflenet_v2_x1_0(pretrained=False)
# mobilenet_v2 = models.mobilenet_v2(pretrained=False)
# mobilenet_v3_large = models.mobilenet_v3_large(pretrained=False)
# mobilenet_v3_small = models.mobilenet_v3_small(pretrained=False)
# resnext50_32x4d = models.resnext50_32x4d(pretrained=False)
# wide_resnet50_2 = models.wide_resnet50_2(pretrained=False)
# mnasnet = models.mnasnet1_0(pretrained=False)