16.神经网络 - 卷积层

神经网络 - 卷积层

pytorch官网网站卷积层(Convolution Layers):Convolution Layers

nn.Conv1d 一维卷积 Applies a 1D convolution over an input signal composed of several input planes.
nn.Conv2d 二维卷积 Applies a 2D convolution over an input signal composed of several input planes.
nn.Conv3d 三维卷积 Applies a 3D convolution over an input signal composed of several input planes.

图像为二维矩阵,所以讲解 nn.Conv2d:

Conv2d --- PyTorch 1.10 documentation

复制代码
CLASS torch.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)
 
# in_channels 输入通道数
# out_channels 输出通道数
# kernel_size 卷积核大小 
#以上参数需要设置
 
#以下参数提供了默认值
# stride=1 卷积过程中的步进大小
# padding=0 卷积过程中对原始图像进行padding的选项
# dilation=1 每一个卷积核对应位的距离
# groups=1 一般设置为1,很少改动,改动的话为分组卷积
# bias=True 通常为True,对卷积后的结果是否加减一个常数的偏置
# padding_mode='zeros' 选择padding填充的模式

参数含义:

动图(演示地址): [conv_arithmetic/README.md at master · vdumoulin/conv_arithmetic · GitHub](conv_arithmetic/README.md at master · vdumoulin/conv_arithmetic · GitHub)

kernel_size

定义了一个卷积核的大小,若为3则生成一个3×3的卷积核

复制代码
卷积核的参数是从一些分布中进行采样得到的
实际训练过程中,卷积核中的值会不断进行调整

in_channels & out_channels

复制代码
in_channels:输入图片的channel数(彩色图像 in_channels 值为3)
out_channels:输出图片的channel数

in_channels 和 out_channels 都为 1 时,拿一个卷积核在输入图像中进行卷积

out_channels 为 2 时,卷积层会生成两个卷积核(不一定一样),得到两个输出,叠加后作为最后输出

CIFAR10数据集实例

复制代码
# CIFAR10数据集
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
 
dataset = torchvision.datasets.CIFAR10("../data",train=False,transform=torchvision.transforms.ToTensor(),download=True)  # 这里用测试数据集,因为训练数据集太大了
dataloader = DataLoader(dataset,batch_size=64)
 
# 搭建神经网络Tudui
class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        # 因为是彩色图片,所以in_channels=3
        self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=3,stride=1,padding=0)   #卷积层conv1
    def forward(self,x):  #输出为x
        x = self.conv1(x)
        return x
 
tudui = Tudui()  # 初始化网络
# 打印一下网络结构
print(tudui)   #Tudui((conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1)))
 
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
    imgs,targets = data  #经过ToTensor转换,成为tensor数据类型,可以直接送到网络中
    output = tudui(imgs)
    print(imgs.shape)     #输入大小 torch.Size([64, 3, 32, 32])  batch_size=64,in_channels=3(彩色图像),每张图片是32×32的
    print(output.shape)   #经过卷积后的输出大小 torch.Size([64, 6, 30, 30])  卷积后变成6个channels,但原始图像减小,所以是30×30的
    writer.add_images("input",imgs,step)
    
    # 6个channel无法显示。torch.Size([64, 6, 30, 30]) ------> [xxx,3,30,30] 第一个值不知道为多少时写-1,会根据后面值的大小进行计算
    output = torch.reshape(output,(-1,3,30,30))
    writer.add_images("output",output,step)
    step = step + 1

运行后(如果没启用环境),在 Terminal 里启动 pytorch 环境:

复制代码
conda activate pytorch

打开 tensorboard:

复制代码
tensorboard --logdir=logs

打开网址(卷积后得到的输出)

卷积层 vgg16

卷积前后维度计算公式

4861571558)]

卷积前后维度计算公式

相关推荐
MSTcheng.3 分钟前
构建自定义算子库:基于ops-nn和aclnn两阶段模式的创新指南
人工智能·cann
User_芊芊君子6 分钟前
CANN图编译器GE全面解析:构建高效异构计算图的核心引擎
人工智能·深度学习·神经网络
lili-felicity7 分钟前
CANN加速Whisper语音识别推理:流式处理与实时转录优化
人工智能·whisper·语音识别
沈浩(种子思维作者)8 分钟前
系统要活起来就必须开放包容去中心化
人工智能·python·flask·量子计算
行走的小派10 分钟前
引爆AI智能体时代!OPi 6Plus全面适配OpenClaw
人工智能
云边有个稻草人10 分钟前
CANN:解构AIGC底层算力,ops-nn驱动神经网络算子加速
人工智能·神经网络·aigc·cann
爱吃大芒果10 分钟前
CANN神经网络算子库设计思路:ops-nn项目的工程化实现逻辑
人工智能·深度学习·神经网络
人工智能培训21 分钟前
具身智能如何让智能体理解物理定律?
人工智能·多模态学习·具身智能·ai培训·人工智能工程师·物理定律
lili-felicity21 分钟前
CANN加速Stable Diffusion文生图推理:从UNet优化到内存复用
人工智能·aigc
哈__21 分钟前
CANN加速语音合成TTS推理:声学模型与声码器优化
人工智能