作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客
本文网址:https://blog.csdn.net/HiWangWenBing/article/details/121342500
目录
第1章 FineTuning、Transfer Trainning的理论基础与深度解析
第1章 FineTuning、Transfer Trainning的理论基础与深度解析
第2章 查看预定义神经网络的网络架构
2.1 前置条件
#环境准备
import numpy as np # numpy数组库
import math # 数学运算库
import matplotlib.pyplot as plt # 画图库
import time as time
import torch # torch基础库
import torch.nn as nn # torch神经网络库
import torch.nn.functional as F
import torchvision.datasets as dataset #公开数据集的下载和管理
import torchvision.transforms as transforms #公开数据集的预处理库,格式转换
import torchvision.utils as utils
import torch.utils.data as data_utils #对数据集进行分批加载的工具集
from PIL import Image #图片显示
from collections import OrderedDict
import torchvision.models as models
print("Hello World")
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.version.cuda)
print(torch.backends.cudnn.version())
2.2 生成预定义网络实例
# 定义升级网络
model = models.resnet101()
2.3 显示网络结构
# 显示网络的全部架构
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 查看网络的内部特定结构以及对应的名称
# 查看网络的内部特定结构以及对应的名称,以后续替换相应的层
print(model.conv1) #显示conv1层的信息
print(model.bn1) #显示bn1层的信息
print(model.relu)
print(model.maxpool) #显示maxpool层的信息
print(model.avgpool) #显示avgpool层的信息
print(model.fc) #显示fc层的信息
print(model.fc.in_features) #显示fc层的输入特征的个数
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 查看模型参数的名称以及结构
for name, parameters in model.named_parameters():
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 查看模型全部参数的结构以及当前的数值
# 查看模型全部参数的结构以及当前的数值
for parameters in model.parameters():
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 无名查看模型参数的是否可训练的属性
# 无名查看模型参数的是否可训练的属性
for param in model.parameters():
print(param.name, param.requires_grad)
None True None True None True None True None True ........
3.4 有名查看模型参数的是否可训练的属性
# 有名查看模型参数的是否可训练的属性
for name, parameters in model.named_parameters():
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 # 锁定网络参数的训练
# 锁定网络参数的训练
for param in model.parameters():
param.requires_grad = False
# 有查看模型参数的是否可训练的属性
for name, parameters in model.named_parameters():
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 替换全连接网络
#替换升级网络的全连接层
model.fc = nn.Sequential(nn.Linear(in_features = 2048, out_features = 100))
# 有查看模型参数的是否可训练的属性
for name, parameters in model.named_parameters():
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