简体   繁体   English

PyTorch中的图像特征提取

[英]Image Feature Extraction in PyTorch

I am going through difficulties to understand this code snippet.我很难理解这个代码片段。

import torch
import torch.nn as nn
import torchvision.models as models

def ResNet152(out_features = 10):
      return getattr(models, "resnet152")(pretrained=False, num_classes = out_features)

def VGG(out_features = 10):
      return getattr(models, "vgg19")(pretrained=False, num_classes = out_features)

In this code segment, features for an input image is extracted by ResNet152 and Vgg19 model.在此代码段中,输入图像的特征由 ResNet152 和 Vgg19 model 提取。 But I have the question, from whether which part of these models the features are being extracted whether the part is last pooling layer or the layer before the classification layer or something else.但我有一个问题,是从这些模型的哪个部分提取特征,无论该部分是最后一个池化层还是分类层之前的层或其他什么。

Note that getattr(models, 'resnet152') is equivalent to models.resent152 .请注意, getattr(models, 'resnet152')等效于models.resent152

Hence, the code below is returning the model itself.因此,下面的代码返回 model 本身。

getattr(models, "resnet152")(pretrained=False, num_classes = out_features)
# is same as
models.resnet152(pretrained=False, num_classes = out_features)

Now, if you look at the structure of the model by simply printing it, the last layer is a fully-connected layer, so that is what you're getting as features here.现在,如果您通过简单的打印来查看 model 的结构,最后一层是全连接层,所以这就是您在这里得到的特征。

print(ResNet152())

ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
...
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=10, bias=True)
)

The same is the case for VGG() . VGG()也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM