卷积是如何计算的

使用代码,看卷积是如何计算的。

torch.nn
torch.nn.functional

srtide 的用法,代表卷积核的步幅

在这里插入图片描述
在这里插入图片描述

import torch
import torch.nn.functional as F
#  这个是输入的一个二维矩阵
input = torch.tensor([[1,2,0,3,1],
								[0,1,2,3,1],
								[1,2,1,0,0],
								[5,2,3,1,1],
								[2,1,0,1,1]
								])

kernel = torch.tensor([[1,2,1],
									[0,1,0],
									[2,1,0]
									])

# 尺寸变化,5*5 ,3*3
input = torch.reshape(input, (1,1,5,5))
kernel  = torch.reshape(kernel, (1,1,3,3))

print(input.shape)
print(kernel.shape)
# stride 代表每次移动的次数或者步幅
output = F.conv2d(input, kernel, stride=1)
print(output)

output2 = F.conv2d(input, kernel, stride=2)
print(output2)


output3 = F.conv2d(input, kernel, stride=1,padding=1)
print(output3)

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

padding 的用法为1的时候,代表把之前的矩阵四周拓展一个像素,

卷积层的使用

图片的一般都是二维矩阵,都使用CONV2D,1D 和3D用的较少

CLASStorch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)[SOURCE

in_channels 输入的通道数
out_channels 输出的通道数
kernel_size 卷积核的大小
stride=1 卷积核的步幅大小
padding =0 对原始数据的一个填充数
dilation =1 卷积核的对应位
groups =1
bias =True 对卷积后的数是否加减一个数
padding_mode=‘zeros’

Parameters:
输入的通道数
in_channels (int) – Number of channels in the input image
输出的通道数 当等于2,就会出现2个卷积核,就会有两个输出
out_channels (int) – Number of channels produced by the convolution
卷积核的大小例如设置为3, 卷积核就是3*3,训练过程中不断的调整。
kernel_size (int or tuple) – Size of the convolving kernel
卷积核移动的步幅
stride (int or tuple, optional) – Stride of the convolution. Default: 1
填充
padding (int, tuple or str, optional) – Padding added to all four sides of the input. Default: 0
填充的地方设置为0
padding_mode (str, optional) – 'zeros', 'reflect', 'replicate' or 'circular'. Default: 'zeros'

dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1

groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
偏置
bias (bool, optional) – If True, adds a learnable bias to the output. Default: True

在这里插入图片描述

动图,蓝色部分为输入图像,青色部分为输出图像

卷积层,

池化层
最常用的nn.MaxPool2d

torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
kernel_size (Union[int, Tuple[int, int]]) – the size of the window to take a max over

stride (Union[int, Tuple[int, int]]) – the stride of the window. Default value is kernel_size

padding (Union[int, Tuple[int, int]]) – Implicit negative infinity padding to be added on both sides

dilation (Union[int, Tuple[int, int]]) – a parameter that controls the stride of elements in the window

return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later

ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape

在这里插入图片描述
cell_mode 为True的时候,要保留最大的值,当cell_mode为False的时候,不保留最大值。

import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)

    def forward(self, input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()

writer = SummaryWriter("../logs_maxpool")
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

为什么池化的作用,保留输入的一个特征,同时把数据量减小,数据量减小了,训练的更快
经过一层卷积之后,来一层池化,然后再来一次非线性激活。

非线性激活层,RELU

import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

input = torch.tensor([[1, -0.5],
                      [-1, 3]])

input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape)

dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.relu1 = ReLU()
        self.sigmoid1 = Sigmoid()

    def forward(self, input):
        output = self.sigmoid1(input)
        return output

tudui = Tudui()

writer = SummaryWriter("../logs_relu")
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, global_step=step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step += 1

writer.close()

非线性变换的主要目的就是网络中引入一些非线性特征,非线性越多,才会训练出符合各种曲线,各种特征的一个模型,

正则化层

线性层

import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.linear1 = Linear(196608, 10)

    def forward(self, input):
        output = self.linear1(input)
        return output

tudui = Tudui()

for data in dataloader:
    imgs, targets = data
    print(imgs.shape)
    output = torch.flatten(imgs)
    print(output.shape)
    output = tudui(output)
    print(output.shape)

Sequential

CIFAR 10 model 结构, 的网络结构
在这里插入图片描述

在这里插入图片描述

import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.model1(x)
        return x

tudui = Tudui()
print(tudui)
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)

writer = SummaryWriter("../logs_seq")
writer.add_graph(tudui, input)
writer.close()

损失函数与反向传播
3.1 计算实际输出和目标之间的差距(损失函数)
3.2 为更新输出提供一定的依据(反向传播) grad 梯度下降
当使用反向传播的时候,每个节点,或者每个要更新的参数,都会求出一个对应的梯度,然后对参数进行优化,最终达到Loss降低的目的, 梯度下降
在这里插入图片描述

误差Loss 越小越好

Loss Function用于分析实际输出与目标之间的差距。

1、L1Loss

2、MSELoss 平方差

3、CrossEntropyLoss(交叉熵损失)
当训练分类问题的时候,
在这里插入图片描述
-0.2+ln(exp(0.1)+exp(0.2)+exp(0.3))

# Loss Function:(CrossEntropyLoss)https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss
import torch
from torch import nn


x = torch.tensor([0.1, 0.2, 0.3])
y = torch.tensor([1])
x = torch.reshape(x, (1, 3))
loss_cross = nn.CrossEntropyLoss()

result_cross = loss_cross(x, y)
print(result_cross)   # tensor(1.1019)

运行结果

tensor(1.1019)

Process finished with exit code 0

3.4交叉熵在神经网络的运用

import torch
from torch import nn
import torchvision

# 准备数据集:
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("./7dataset", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=1, drop_last=True)


# 搭建神经网络:
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.model1 = nn.Sequential(
            nn.Conv2d(3, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(1024, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model1(x)
        return x


if __name__ == "__main__":
    loss = nn.CrossEntropyLoss()
    model_1 = Model()
    for data in dataloader:
        imgs, targets = data
        outputs = model_1(imgs)
        result_loss = loss(outputs, targets)
        print(result_loss)   # 计算神经网络输出与实际的误差

        # 反向传播,梯度下降
        result_loss.backward()   # 能够生成梯度
        print("ok")

优化器
反向传播可以求出每个需要调节的参数,每个参数对应的梯度,有了梯度就可以利用优化器,这个优化器对参数进行调整,以达到整体误差降低的目的。
Torch.optim
设置

# 官方文档:https://pytorch.org/docs/stable/optim.html
# lr(learning rate):学习速率
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader


dataset = torchvision.datasets.CIFAR10("../7. dataset", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=1, drop_last=True)


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.model1 = nn.Sequential(
            nn.Conv2d(3, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(1024, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model1(x)
        return x


loss = nn.CrossEntropyLoss()
model_1 = Model()

# 1. 设置优化器:(设置随机梯度下降优化器)
optim = torch.optim.SGD(model_1.parameters(), lr=0.01)  # lr不能设置得太大或太小,太大模型会不稳定,太小模型训练会很慢(推荐开始选择较大的lr,后面选择较小的lr)

for epcoh in range(5):   # 循环几次就是尽行几轮学习
    running_loss = 0.0   # 保存整体误差总和
    for data in dataloader:  # 仅进行了一轮的学习
        imgs, targets = data
        outputs = model_1(imgs)
        result_loss = loss(outputs, targets)
        optim.zero_grad()  # 2. 调用优化器,让网络中的每一个参数的梯度置零
        result_loss.backward()  # 反向传播,求出每一个节点的梯度
        optim.step()  # 3. 调用优化器,对每一个参数进行调优
        # print(result_loss)
        running_loss += result_loss   # 整体误差总和
    print(running_loss)

现有网络模型的使用
安装scipy
卸载了
pip uninstall scipy

pip install scipy
下载数据集ImageNet,数据集太大了

  1. 加载VGG模型
  2. pretrained=False
# 文档:https://arxiv.org/abs/1409.1556
# 官方地址:https://pytorch.org/vision/stable/models.html#classification
# 常用 VGG16 或 VGG19
# 需要安装scipy模块:pip install scipy
import torchvision.datasets

# train_data = torchvision.datasets.ImageNet("../19. dataset_image_net", split="train", download=True,
#                                            transform=torchvision.transforms.ToTensor())
# 由于ImageNet的训练数据集不被公开访问了,所以不能这样下载了(该数据集有140多G,太大了)

vgg16_false = torchvision.models.vgg16(pretrained=False)   # 加载网络模型,不会下载(等同于前面我们写的Model()网络架构) 参数是默认的参数
# vgg16_true = torchvision.models.vgg16(pretrained=True)   # 会从网上下载网络模型,参数是已经被训练好的参数
print(vgg16_false)
  1. pretrained=True
# 文档:https://arxiv.org/abs/1409.1556
# 官方地址:https://pytorch.org/vision/stable/models.html#classification
# 常用 VGG16 或 VGG19
# 需要安装scipy模块:pip install scipy
import torchvision.datasets

# train_data = torchvision.datasets.ImageNet("../19. dataset", split="train", download=True,
#                                            transform=torchvision.transforms.ToTensor())
# 由于ImageNet的训练数据集不被公开访问了,所以不能这样下载了(该数据集有140多G,太大了)

# vgg16_false = torchvision.models.vgg16(pretrained=False)   # 加载网络模型,不会下载(等同于前面我们写的Model()网络架构) 参数是默认的参数
vgg16_true = torchvision.models.vgg16(pretrained=True)   # 会从网上下载网络模型,参数是已经被训练好的参数
# Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to C:\Users\lv/.cache\torch\hub\checkpoints\vgg16-397923af.pth

# print(vgg16_false)
print(vgg16_true)

运行结果,从运行结果中可以看出,最后的结果可以看出,也是分类的模型,

D:\Anaconda\envs\pytorch\python.exe "C:/Users/lv/Desktop/Pytorch/1. Pytorch的使用(第一次)/src/19. VGG模型_pretrained(现有网络模型的使用).py"
Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to C:\Users\lv/.cache\torch\hub\checkpoints\vgg16-397923af.pth
100.0%
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)

Process finished with exit code 0


一般情况使用vgg16作为一个前置的网络结构,在vgg16后面加一些网络特征,去实现一个功能。
2. 修改VGG网络模型

# 文档:https://arxiv.org/abs/1409.1556
# 官方地址:https://pytorch.org/vision/stable/models.html#classification
# 常用 VGG16 或 VGG19
# 需要安装scipy模块:pip install scipy
import torchvision.datasets
from torch import nn

# train_data = torchvision.datasets.ImageNet("../19. dataset", split="train", download=True,
#                                            transform=torchvision.transforms.ToTensor())
# 由于ImageNet的训练数据集不被公开访问了,所以不能这样下载了(该数据集有140多G,太大了)

vgg16_false = torchvision.models.vgg16(pretrained=False)   # 加载网络模型,不会下载(等同于前面我们写的Model()网络架构) 参数是默认的参数
vgg16_true = torchvision.models.vgg16(pretrained=True)   # 会从网上下载网络模型,参数是已经被训练好的参数
# Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to C:\Users\lv/.cache\torch\hub\checkpoints\vgg16-397923af.pth

# print(vgg16_false)
# print(vgg16_true)

train_data = torchvision.datasets.CIFAR10("../19. dataset", train=True, transform=torchvision.transforms.ToTensor(), download=True)
# 添加操作:
vgg16_true.add_module('add_linear1', nn.Linear(1000, 10))   # 添加一个线性层(添加在最后面)
# print(vgg16_true)
vgg16_true.classifier.add_module("add_linear2", nn.Linear(1000, 10))  # 添加到classifier里面
print(vgg16_true)

print(vgg16_false)
# 修改操作:
vgg16_false.classifier[6] = nn.Linear(in_features=4096, out_features=10)
print(vgg16_false)

运行结果

D:\Anaconda\envs\pytorch\python.exe "C:/Users/lv/Desktop/Pytorch/1. Pytorch的使用(第一次)/src/19. VGG模型_pretrained(现有网络模型的使用).py"
Files already downloaded and verified
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
    (add_linear2): Linear(in_features=1000, out_features=10, bias=True)
  )
  (add_linear1): Linear(in_features=1000, out_features=10, bias=True)
)
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=10, bias=True)
  )
)

Process finished with exit code 0

模型的保存
模型的加载
完整的模型训练套路-GPU训练
完整的模型验证套路
在看下github上

模型的保存,有两种方式

import torch
import torchvision


vgg16 = torchvision.models.vgg16(pretrained=False)

# 保存方式1,保存模型结构+模型参数
torch.save(vgg16, "vgg16_method1.pth")

# 保存方式2  模型参数 官方推荐
torch.save(vgg16.state_dict(), "vgg16_method2.pth")

模型加载也有对应的两种方式



import torch

# 方式一,保存方式1,加载模型
model = torch.load("vgg16_method1.pth")
# print(model)

# 方式二,加载模型
model = torch.load("vgg16_method2.pth")

print(model)

完整的模型验证套路

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/793441.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SpringBoot相关

SpringBoot 1. what springboot也是spring公司开发的一款框架。为了简化spring项目的初始化搭建的。 spring项目搭建的缺点: 配置麻烦依赖繁多tomcat启动慢 2 .springboot的特点(why) 自动配置 springboot的自动配置是一个运行时(更准确地说,是应用程…

【学术会议征稿】第三届人工智能与智能信息处理国际学术会议(AIIIP 2024)

第三届人工智能与智能信息处理国际学术会议(AIIIP 2024) 2024 3rd International Conference on Artificial Intelligence and Intelligent Information Processing 第三届人工智能与智能信息处理国际学术会议(AIIIP 2024)将于…

SAP 物料批量执行MD02代码分享

通常我们在运行MRP的时候要么就是跑MD01整个工厂层级的,要么就是单个物料层级的MRP用MD02去单个物料去执行。 HANA后有了MRPLIVE,可以支持多物料的运行MRP,但是无法控制MRP的运行参数,根据采购类型跑出对应的单据,F跑出的采购申请,E跑出的是计划订单。 需求是要可以批量运…

网上下载的视频怎么转成mp4格式?教你一招轻松解决

网上拥有许多的视频资源,动漫,短视频,影视等等都层出不穷,很多小伙伴都会把一些视频进行下载下来,不过下载下来的视频都不是MP4格式的,在兼容性方面会比较的麻烦,会有播放器不支持的情况&#x…

SpringBoot新手快速入门系列教程十:基于Docker Compose,部署一个简单的项目

前述: 本篇教程将略过很多docker下载环境配置的基础步骤,如果您对docker不太熟悉请参考我的上一个教程:SpringBoot新手快速入门系列教程九:基于docker容器,部署一个简单的项目 使用 Docker Compose 支持部署 Docker 项…

Elon Musk开源Grok

转载自:AILab基地 早在6天前,马斯克就发文称xAI将开源Grok 图片 13小时前,马斯克开源了旗下公司X的Grok训练模型,并喊话OpenAI,你名字里的Open到底在哪里 图片 下面是xai-org的GitHub开源地址[https://github.com/x…

shein测试开发会问些啥?

🏆本文收录于《CSDN问答解惑-》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&…

【大模型搭建】部署Ollama,pull失败,手动构建方案

Ollama 是一个专注于简化大规模机器学习模型开发的框架。它提供了一系列工具来帮助开发者轻松地定义、训练和部署大型语言模型。 优点: • 提供了简洁的API,易于上手。 • 支持多种硬件加速选项,如GPU和TPU。 • 内置了许多预训练模型&#x…

常见点云处理总结汇总

点云处理是一系列操作和技术,用于分析和处理三维点云数据,以提取有用的信息并生成可视化结果。以下是常见的点云处理方法及其简要说明: 1. 点云预处理 滤波:去除噪声和无效点,如使用统计滤波、半径滤波等。下采样&a…

IDEA自动生成测试类以及测试方法(JUnit测试工具)

IDEA自动生成测试类以及测试方法 (1) 光标移至需要生成测试类的类后面,右击 Go To → Test, 或者直接使用快捷键 Ctrl Shift T 。 (2)点击 Create New Test (3)设置测试库和测试方法,点击 OK &#xff…

赋能 Web3 与 AI 的未来,TARS 协议如何重塑去中心化生态?

TARS 协议如何在 Web3 生态中引领 AI 技术的变革?作为新兴的模块化 AI 平台,TARS 通过整合先进的 AI 模型和区块链技术,为用户提供了更加智能、安全和高效的解决方案。让我们一起回顾第 16 期 TinTinAMA 的精彩内容吧! 在 TinTinL…

一场夏测杀出个“双冠王”,极越01成为纯电SUV标杆

文 | AUTO芯球 作者 | 雷慢 万万没想到,懂车帝夏测运动会杀出一匹最大的黑马,竟然是极越01。 当前正在进行的懂车帝夏测运动会,在“纯电SUV/MPV续航达成率”赛事中,极越01以85.8%的续航达成率获得第一名。并且由于赛制规则限制…

WebRTC批量发送消息API接口的特性有哪些?

WebRTC批量发送消息api接口怎么样?接口性能怎么用? WebRTC技术允许浏览器和移动应用进行实时通信。通过WebRTC,开发者可以构建视频、语音、数据共享等应用。AokSend将重点探讨WebRTC批量发送消息API接口的特性。 WebRTC批量发送消息API接口…

《昇思25天学习打卡营第1天|QuickStart》

说在前面 曾经接触过华为的910B服务级显卡,当时基于910B做了一些开发的工作,但是总感觉做的事情太低层,想要能自顶向下的了解下,因此开始了MindSpore的学习。另外也想给予提供的显卡,简单尝试下llm模型的训练&#xf…

静态时序分析:Leaf Cell(叶单元)

相关阅读​​​​​​​静态时序分析https://blog.csdn.net/weixin_45791458/category_12567571.html 在DC中,leaf cell(叶单元)有时会出现在描述中,例如set_input_delay的-reference_pin选项的参数,就必须是一个端口或…

元组列表之案例

1.列表推导式 基本语法: [表达式 for语句1 if 语句1 for语句2 if语句2 ........ ] 1.零到九的平方列表 a [i*i for i in range(10)] print(a) 2.for 循环前面加if else #如果是偶数乘以2,如果是奇数直接输出 a [i*2 if i%2 0 else i for i in ran…

windows下安装和使用nacos

概述 Nacos致力于帮助您发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发 现、服务配置、服务元数据及流且管理 Nacos官方文档:https://nacos.io/zh-cn/docs/quick-start.html Nacos下载地址:https://n…

入门PHP就来我这(高级)24 ~ Session判断用户登录

有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享 路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。 上一篇我们介绍了Session管理部分的概念,本文通过session来改写一些用户登录&…

Unity发布webgl之后修改StreamingAssets 内的配置文件读取到的还是之前的配置文件的解决方案

问题描述 unity发布webgl之后,修改在StreamingAssets 中的配置信息,修改之后读取的还是之前的配置信息 读取配置文件的代码IEnumerator IE_WebGL_LoadWebSocketServerCopnfig(){var uri new System.Uri(Path.Combine(Application.streamingAssetsPath…

和鲸101计划夏令营火热进行中!北中医助阵医学数据探索

上周,和鲸社区 2024 夏令营已经正式开营! 从 2021 年开始,和鲸社区在每年暑假期间都会为大家提供集中化、系统化的数据科学相关的技能实践和培训,每年都有几千名同学借此机会积累宝贵的实战经验,丰富个人简历作品&…