当前位置:   article > 正文

[Pytorch系列-48]:如何查看和修改预定义神经网络的网络架构、网络参数属性_torch 修改网络参数

torch 修改网络参数

作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/121342500


目录

第1章 FineTuning、Transfer Trainning的理论基础与深度解析

第2章 查看预定义神经网络的网络架构

2.1 前置条件

2.2 生成预定义网络实例

2.3 显示网络结构

2.4 查看网络的内部特定结构以及对应的名称

第3章 查看预定义神经网络的参数

3.1 查看模型参数的名称以及结构

3.2 查看模型全部参数的结构以及当前的数值

3.3 无名查看模型参数的是否可训练的属性

3.4 有名查看模型参数的是否可训练的属性

第4章 修改网络的结构与参数

4.1 # 锁定网络参数的训练

4.2 替换全连接网络

4.3 显示全连接网络


第1章 FineTuning、Transfer Trainning的理论基础与深度解析

[人工智能-深度学习-46]:FineTuning(微调)、Transfer Trainning(迁移学习)的理论基础与深度解析_文火冰糖(王文兵)的博客-CSDN博客第1张 前言:常见的工程诉求与特点(1)数据集欠缺个人的数据集小,无法提供向ImageNet这样的大数据集,但又想利用在ImageNet上训练的模型好的模型,为我所用,即基于在一些知名的数据集上训练好的模型,在进一步的训练,以满足自己的应用场景的需求,而无需重头开始训练。(2)分类数多变个人特定的应用,分类的种类与Image(1000种分类)等知名数据集的分类的种类不同,我们想在已经训练好模型的基础上,做适当的重新训练,以支持我们自己的分类数目,如100分类。(3)防止过度训练,即过..https://blog.csdn.net/HiWangWenBing/article/details/121312417

第2章 查看预定义神经网络的网络架构

2.1 前置条件

  1. #环境准备
  2. import numpy as np # numpy数组库
  3. import math # 数学运算库
  4. import matplotlib.pyplot as plt # 画图库
  5. import time as time
  6. import torch # torch基础库
  7. import torch.nn as nn # torch神经网络库
  8. import torch.nn.functional as F
  9. import torchvision.datasets as dataset #公开数据集的下载和管理
  10. import torchvision.transforms as transforms #公开数据集的预处理库,格式转换
  11. import torchvision.utils as utils
  12. import torch.utils.data as data_utils #对数据集进行分批加载的工具集
  13. from PIL import Image #图片显示
  14. from collections import OrderedDict
  15. import torchvision.models as models
  16. print("Hello World")
  17. print(torch.__version__)
  18. print(torch.cuda.is_available())
  19. print(torch.version.cuda)
  20. print(torch.backends.cudnn.version())

2.2 生成预定义网络实例

  1. # 定义升级网络
  2. model = models.resnet101()

2.3 显示网络结构

  1. # 显示网络的全部架构
  2. print(model)
ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (relu): ReLU(inplace=True)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer2): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer3): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (4): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (5): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (6): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (7): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (8): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (9): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (10): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (11): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (12): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (13): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (14): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (15): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (16): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (17): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (18): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (19): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (20): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (21): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (22): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer4): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=1000, bias=True)
)

2.4 查看网络的内部特定结构以及对应的名称

  1. # 查看网络的内部特定结构以及对应的名称,以后续替换相应的层
  2. print(model.conv1) #显示conv1层的信息
  3. print(model.bn1) #显示bn1层的信息
  4. print(model.relu)
  5. print(model.maxpool) #显示maxpool层的信息
  6. print(model.avgpool) #显示avgpool层的信息
  7. print(model.fc) #显示fc层的信息
  8. print(model.fc.in_features) #显示fc层的输入特征的个数
  9. print(model.fc.out_features) #显示fc层的输出特征的个数
Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
ReLU(inplace=True)
MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
AdaptiveAvgPool2d(output_size=(1, 1))
Linear(in_features=2048, out_features=1000, bias=True)
2048
1000

备注:

默认输出是1000分类

第3章 查看预定义神经网络的参数

3.1 查看模型参数的名称以及结构

  1. for name, parameters in model.named_parameters():
  2. print(name, ':', parameters.size())
conv1.weight : torch.Size([64, 3, 7, 7])
bn1.weight : torch.Size([64])
bn1.bias : torch.Size([64])
layer1.0.conv1.weight : torch.Size([64, 64, 1, 1])
layer1.0.bn1.weight : torch.Size([64])
layer1.0.bn1.bias : torch.Size([64])
layer1.0.conv2.weight : torch.Size([64, 64, 3, 3])
layer1.0.bn2.weight : torch.Size([64])
layer1.0.bn2.bias : torch.Size([64])
layer1.0.conv3.weight : torch.Size([256, 64, 1, 1])
layer1.0.bn3.weight : torch.Size([256])
layer1.0.bn3.bias : torch.Size([256])
layer1.0.downsample.0.weight : torch.Size([256, 64, 1, 1])
layer1.0.downsample.1.weight : torch.Size([256])
layer1.0.downsample.1.bias : torch.Size([256])
layer1.1.conv1.weight : torch.Size([64, 256, 1, 1])
layer1.1.bn1.weight : torch.Size([64])
layer1.1.bn1.bias : torch.Size([64])
layer1.1.conv2.weight : torch.Size([64, 64, 3, 3])
layer1.1.bn2.weight : torch.Size([64])
layer1.1.bn2.bias : torch.Size([64])
layer1.1.conv3.weight : torch.Size([256, 64, 1, 1])
layer1.1.bn3.weight : torch.Size([256])
layer1.1.bn3.bias : torch.Size([256])
layer1.2.conv1.weight : torch.Size([64, 256, 1, 1])
layer1.2.bn1.weight : torch.Size([64])
layer1.2.bn1.bias : torch.Size([64])
layer1.2.conv2.weight : torch.Size([64, 64, 3, 3])
layer1.2.bn2.weight : torch.Size([64])
layer1.2.bn2.bias : torch.Size([64])
layer1.2.conv3.weight : torch.Size([256, 64, 1, 1])
layer1.2.bn3.weight : torch.Size([256])
layer1.2.bn3.bias : torch.Size([256])
layer2.0.conv1.weight : torch.Size([128, 256, 1, 1])
layer2.0.bn1.weight : torch.Size([128])
layer2.0.bn1.bias : torch.Size([128])
layer2.0.conv2.weight : torch.Size([128, 128, 3, 3])
layer2.0.bn2.weight : torch.Size([128])
layer2.0.bn2.bias : torch.Size([128])
layer2.0.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.0.bn3.weight : torch.Size([512])
layer2.0.bn3.bias : torch.Size([512])
layer2.0.downsample.0.weight : torch.Size([512, 256, 1, 1])
layer2.0.downsample.1.weight : torch.Size([512])
layer2.0.downsample.1.bias : torch.Size([512])
layer2.1.conv1.weight : torch.Size([128, 512, 1, 1])
layer2.1.bn1.weight : torch.Size([128])
layer2.1.bn1.bias : torch.Size([128])
layer2.1.conv2.weight : torch.Size([128, 128, 3, 3])
layer2.1.bn2.weight : torch.Size([128])
layer2.1.bn2.bias : torch.Size([128])
layer2.1.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.1.bn3.weight : torch.Size([512])
layer2.1.bn3.bias : torch.Size([512])
layer2.2.conv1.weight : torch.Size([128, 512, 1, 1])
layer2.2.bn1.weight : torch.Size([128])
layer2.2.bn1.bias : torch.Size([128])
layer2.2.conv2.weight : torch.Size([128, 128, 3, 3])
layer2.2.bn2.weight : torch.Size([128])
layer2.2.bn2.bias : torch.Size([128])
layer2.2.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.2.bn3.weight : torch.Size([512])
layer2.2.bn3.bias : torch.Size([512])
layer2.3.conv1.weight : torch.Size([128, 512, 1, 1])
layer2.3.bn1.weight : torch.Size([128])
layer2.3.bn1.bias : torch.Size([128])
layer2.3.conv2.weight : torch.Size([128, 128, 3, 3])
layer2.3.bn2.weight : torch.Size([128])
layer2.3.bn2.bias : torch.Size([128])
layer2.3.conv3.weight : torch.Size([512, 128, 1, 1])
layer2.3.bn3.weight : torch.Size([512])
layer2.3.bn3.bias : torch.Size([512])
layer3.0.conv1.weight : torch.Size([256, 512, 1, 1])
layer3.0.bn1.weight : torch.Size([256])
layer3.0.bn1.bias : torch.Size([256])
layer3.0.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.0.bn2.weight : torch.Size([256])
layer3.0.bn2.bias : torch.Size([256])
layer3.0.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.0.bn3.weight : torch.Size([1024])
layer3.0.bn3.bias : torch.Size([1024])
layer3.0.downsample.0.weight : torch.Size([1024, 512, 1, 1])
layer3.0.downsample.1.weight : torch.Size([1024])
layer3.0.downsample.1.bias : torch.Size([1024])
layer3.1.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.1.bn1.weight : torch.Size([256])
layer3.1.bn1.bias : torch.Size([256])
layer3.1.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.1.bn2.weight : torch.Size([256])
layer3.1.bn2.bias : torch.Size([256])
layer3.1.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.1.bn3.weight : torch.Size([1024])
layer3.1.bn3.bias : torch.Size([1024])
layer3.2.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.2.bn1.weight : torch.Size([256])
layer3.2.bn1.bias : torch.Size([256])
layer3.2.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.2.bn2.weight : torch.Size([256])
layer3.2.bn2.bias : torch.Size([256])
layer3.2.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.2.bn3.weight : torch.Size([1024])
layer3.2.bn3.bias : torch.Size([1024])
layer3.3.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.3.bn1.weight : torch.Size([256])
layer3.3.bn1.bias : torch.Size([256])
layer3.3.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.3.bn2.weight : torch.Size([256])
layer3.3.bn2.bias : torch.Size([256])
layer3.3.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.3.bn3.weight : torch.Size([1024])
layer3.3.bn3.bias : torch.Size([1024])
layer3.4.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.4.bn1.weight : torch.Size([256])
layer3.4.bn1.bias : torch.Size([256])
layer3.4.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.4.bn2.weight : torch.Size([256])
layer3.4.bn2.bias : torch.Size([256])
layer3.4.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.4.bn3.weight : torch.Size([1024])
layer3.4.bn3.bias : torch.Size([1024])
layer3.5.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.5.bn1.weight : torch.Size([256])
layer3.5.bn1.bias : torch.Size([256])
layer3.5.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.5.bn2.weight : torch.Size([256])
layer3.5.bn2.bias : torch.Size([256])
layer3.5.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.5.bn3.weight : torch.Size([1024])
layer3.5.bn3.bias : torch.Size([1024])
layer3.6.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.6.bn1.weight : torch.Size([256])
layer3.6.bn1.bias : torch.Size([256])
layer3.6.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.6.bn2.weight : torch.Size([256])
layer3.6.bn2.bias : torch.Size([256])
layer3.6.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.6.bn3.weight : torch.Size([1024])
layer3.6.bn3.bias : torch.Size([1024])
layer3.7.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.7.bn1.weight : torch.Size([256])
layer3.7.bn1.bias : torch.Size([256])
layer3.7.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.7.bn2.weight : torch.Size([256])
layer3.7.bn2.bias : torch.Size([256])
layer3.7.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.7.bn3.weight : torch.Size([1024])
layer3.7.bn3.bias : torch.Size([1024])
layer3.8.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.8.bn1.weight : torch.Size([256])
layer3.8.bn1.bias : torch.Size([256])
layer3.8.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.8.bn2.weight : torch.Size([256])
layer3.8.bn2.bias : torch.Size([256])
layer3.8.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.8.bn3.weight : torch.Size([1024])
layer3.8.bn3.bias : torch.Size([1024])
layer3.9.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.9.bn1.weight : torch.Size([256])
layer3.9.bn1.bias : torch.Size([256])
layer3.9.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.9.bn2.weight : torch.Size([256])
layer3.9.bn2.bias : torch.Size([256])
layer3.9.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.9.bn3.weight : torch.Size([1024])
layer3.9.bn3.bias : torch.Size([1024])
layer3.10.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.10.bn1.weight : torch.Size([256])
layer3.10.bn1.bias : torch.Size([256])
layer3.10.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.10.bn2.weight : torch.Size([256])
layer3.10.bn2.bias : torch.Size([256])
layer3.10.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.10.bn3.weight : torch.Size([1024])
layer3.10.bn3.bias : torch.Size([1024])
layer3.11.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.11.bn1.weight : torch.Size([256])
layer3.11.bn1.bias : torch.Size([256])
layer3.11.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.11.bn2.weight : torch.Size([256])
layer3.11.bn2.bias : torch.Size([256])
layer3.11.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.11.bn3.weight : torch.Size([1024])
layer3.11.bn3.bias : torch.Size([1024])
layer3.12.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.12.bn1.weight : torch.Size([256])
layer3.12.bn1.bias : torch.Size([256])
layer3.12.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.12.bn2.weight : torch.Size([256])
layer3.12.bn2.bias : torch.Size([256])
layer3.12.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.12.bn3.weight : torch.Size([1024])
layer3.12.bn3.bias : torch.Size([1024])
layer3.13.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.13.bn1.weight : torch.Size([256])
layer3.13.bn1.bias : torch.Size([256])
layer3.13.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.13.bn2.weight : torch.Size([256])
layer3.13.bn2.bias : torch.Size([256])
layer3.13.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.13.bn3.weight : torch.Size([1024])
layer3.13.bn3.bias : torch.Size([1024])
layer3.14.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.14.bn1.weight : torch.Size([256])
layer3.14.bn1.bias : torch.Size([256])
layer3.14.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.14.bn2.weight : torch.Size([256])
layer3.14.bn2.bias : torch.Size([256])
layer3.14.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.14.bn3.weight : torch.Size([1024])
layer3.14.bn3.bias : torch.Size([1024])
layer3.15.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.15.bn1.weight : torch.Size([256])
layer3.15.bn1.bias : torch.Size([256])
layer3.15.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.15.bn2.weight : torch.Size([256])
layer3.15.bn2.bias : torch.Size([256])
layer3.15.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.15.bn3.weight : torch.Size([1024])
layer3.15.bn3.bias : torch.Size([1024])
layer3.16.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.16.bn1.weight : torch.Size([256])
layer3.16.bn1.bias : torch.Size([256])
layer3.16.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.16.bn2.weight : torch.Size([256])
layer3.16.bn2.bias : torch.Size([256])
layer3.16.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.16.bn3.weight : torch.Size([1024])
layer3.16.bn3.bias : torch.Size([1024])
layer3.17.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.17.bn1.weight : torch.Size([256])
layer3.17.bn1.bias : torch.Size([256])
layer3.17.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.17.bn2.weight : torch.Size([256])
layer3.17.bn2.bias : torch.Size([256])
layer3.17.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.17.bn3.weight : torch.Size([1024])
layer3.17.bn3.bias : torch.Size([1024])
layer3.18.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.18.bn1.weight : torch.Size([256])
layer3.18.bn1.bias : torch.Size([256])
layer3.18.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.18.bn2.weight : torch.Size([256])
layer3.18.bn2.bias : torch.Size([256])
layer3.18.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.18.bn3.weight : torch.Size([1024])
layer3.18.bn3.bias : torch.Size([1024])
layer3.19.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.19.bn1.weight : torch.Size([256])
layer3.19.bn1.bias : torch.Size([256])
layer3.19.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.19.bn2.weight : torch.Size([256])
layer3.19.bn2.bias : torch.Size([256])
layer3.19.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.19.bn3.weight : torch.Size([1024])
layer3.19.bn3.bias : torch.Size([1024])
layer3.20.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.20.bn1.weight : torch.Size([256])
layer3.20.bn1.bias : torch.Size([256])
layer3.20.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.20.bn2.weight : torch.Size([256])
layer3.20.bn2.bias : torch.Size([256])
layer3.20.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.20.bn3.weight : torch.Size([1024])
layer3.20.bn3.bias : torch.Size([1024])
layer3.21.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.21.bn1.weight : torch.Size([256])
layer3.21.bn1.bias : torch.Size([256])
layer3.21.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.21.bn2.weight : torch.Size([256])
layer3.21.bn2.bias : torch.Size([256])
layer3.21.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.21.bn3.weight : torch.Size([1024])
layer3.21.bn3.bias : torch.Size([1024])
layer3.22.conv1.weight : torch.Size([256, 1024, 1, 1])
layer3.22.bn1.weight : torch.Size([256])
layer3.22.bn1.bias : torch.Size([256])
layer3.22.conv2.weight : torch.Size([256, 256, 3, 3])
layer3.22.bn2.weight : torch.Size([256])
layer3.22.bn2.bias : torch.Size([256])
layer3.22.conv3.weight : torch.Size([1024, 256, 1, 1])
layer3.22.bn3.weight : torch.Size([1024])
layer3.22.bn3.bias : torch.Size([1024])
layer4.0.conv1.weight : torch.Size([512, 1024, 1, 1])
layer4.0.bn1.weight : torch.Size([512])
layer4.0.bn1.bias : torch.Size([512])
layer4.0.conv2.weight : torch.Size([512, 512, 3, 3])
layer4.0.bn2.weight : torch.Size([512])
layer4.0.bn2.bias : torch.Size([512])
layer4.0.conv3.weight : torch.Size([2048, 512, 1, 1])
layer4.0.bn3.weight : torch.Size([2048])
layer4.0.bn3.bias : torch.Size([2048])
layer4.0.downsample.0.weight : torch.Size([2048, 1024, 1, 1])
layer4.0.downsample.1.weight : torch.Size([2048])
layer4.0.downsample.1.bias : torch.Size([2048])
layer4.1.conv1.weight : torch.Size([512, 2048, 1, 1])
layer4.1.bn1.weight : torch.Size([512])
layer4.1.bn1.bias : torch.Size([512])
layer4.1.conv2.weight : torch.Size([512, 512, 3, 3])
layer4.1.bn2.weight : torch.Size([512])
layer4.1.bn2.bias : torch.Size([512])
layer4.1.conv3.weight : torch.Size([2048, 512, 1, 1])
layer4.1.bn3.weight : torch.Size([2048])
layer4.1.bn3.bias : torch.Size([2048])
layer4.2.conv1.weight : torch.Size([512, 2048, 1, 1])
layer4.2.bn1.weight : torch.Size([512])
layer4.2.bn1.bias : torch.Size([512])
layer4.2.conv2.weight : torch.Size([512, 512, 3, 3])
layer4.2.bn2.weight : torch.Size([512])
layer4.2.bn2.bias : torch.Size([512])
layer4.2.conv3.weight : torch.Size([2048, 512, 1, 1])
layer4.2.bn3.weight : torch.Size([2048])
layer4.2.bn3.bias : torch.Size([2048])
fc.weight : torch.Size([1000, 2048])
fc.bias : torch.Size([1000])

3.2 查看模型全部参数的结构以及当前的数值

  1. # 查看模型全部参数的结构以及当前的数值
  2. for parameters in model.parameters():
  3. print(parameters)
Parameter containing:
tensor([[[[ 0.0129,  0.0295,  0.0005,  ...,  0.0436, -0.0144,  0.0082],
          [ 0.0123,  0.0027, -0.0260,  ..., -0.0539, -0.0083, -0.0259],
          [ 0.0027, -0.0140,  0.0041,  ..., -0.0145,  0.0109, -0.0182],
          ...,
          [ 0.0128, -0.0022,  0.0388,  ..., -0.0116,  0.0571, -0.0283],
          [-0.0015, -0.0179, -0.0010,  ..., -0.0110,  0.0009,  0.0310],
          [ 0.0100, -0.0215,  0.0241,  ..., -0.0019, -0.0834, -0.0293]],

         [[ 0.0086,  0.0038,  0.0213,  ...,  0.0403,  0.0004, -0.0281],
          [-0.0243,  0.0175, -0.0021,  ..., -0.0457, -0.0118, -0.0098],
          [-0.0215,  0.0212,  0.0349,  ..., -0.0090, -0.0021, -0.0105],
          

.....................

3.3 无名查看模型参数的是否可训练的属性


  1. # 无名查看模型参数的是否可训练的属性
  2. for param in model.parameters():
  3. print(param.name, param.requires_grad)
None True
None True
None True
None True
None True
........

3.4 有名查看模型参数的是否可训练的属性

  1. # 有名查看模型参数的是否可训练的属性
  2. for name, parameters in model.named_parameters():
  3. print(name, ':', parameters.requires_grad)

......

layer4.2.bn1.weight : True
layer4.2.bn1.bias : True
layer4.2.conv2.weight : True
layer4.2.bn2.weight : True
layer4.2.bn2.bias : True
layer4.2.conv3.weight : True
layer4.2.bn3.weight : True
layer4.2.bn3.bias : True
fc.weight : True
fc.bias : True

此时全连接参数是可训练的。

第4章 修改网络的结构与参数

4.1 # 锁定网络参数的训练

  1. # 锁定网络参数的训练
  2. for param in model.parameters():
  3. param.requires_grad = False
  4. # 有查看模型参数的是否可训练的属性
  5. for name, parameters in model.named_parameters():
  6. print(name, ':', parameters.requires_grad)
layer4.2.conv1.weight : False
layer4.2.bn1.weight : False
layer4.2.bn1.bias : False
layer4.2.conv2.weight : False
layer4.2.bn2.weight : False
layer4.2.bn2.bias : False
layer4.2.conv3.weight : False
layer4.2.bn3.weight : False
layer4.2.bn3.bias : False
fc.weight : False
fc.bias : False

备注:所有参数不可训练

4.2 替换全连接网络

  1. #替换升级网络的全连接层
  2. model.fc = nn.Sequential(nn.Linear(in_features = 2048, out_features = 100))
  3. # 有查看模型参数的是否可训练的属性
  4. for name, parameters in model.named_parameters():
  5. print(name, ':', parameters.requires_grad)
layer4.2.conv1.weight : False
layer4.2.bn1.weight : False
layer4.2.bn1.bias : False
layer4.2.conv2.weight : False
layer4.2.bn2.weight : False
layer4.2.bn2.bias : False
layer4.2.conv3.weight : False
layer4.2.bn3.weight : False
layer4.2.bn3.bias : False
fc.0.weight : True
fc.0.bias : True

备注:只替换了全连接网络

4.3 显示全连接网络

print(model.fc)      #显示fc层的信息
Sequential(
  (0): Linear(in_features=2048, out_features=100, bias=True)
)

作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/121342500

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/217576
推荐阅读
相关标签
  

闽ICP备14008679号