15.土堆说卷积操作(stride、padding)

土堆说卷积操作(可选看)

卷积神经网络中Padding和Stride的概念,Padding用于解决图像边缘信息丢失问题,保持输出矩阵尺寸;Stride则影响卷积的步进,改变输出大小。通过调整这两者,可以控制卷积层的输出特性。

这节来讲解卷积层 :Convolution Layers

先进入pytorch官方网站地址:torch.nn --- PyTorch 1.8.1 documentation

主要讲解 nn.Conve2d ,pytorch官方网站地址:Conv2d --- PyTorch 1.8.1 documentation

torch.nn 和 torch.nn.functional 的区别:前者是后者的封装,更利于使用

点击 torch.nn.functional - Convolution functions - conv2d 查看参数

stride(步进)

可以是单个数,或元组(sH,sW) --- 控制横向步进和纵向步进

卷积操作

卷积操作介绍

当 stride = 2 时,横向和纵向都是2,输出是一个2×2的矩阵

卷积操作实战

要求输入的维度 & reshape函数

  • input:尺寸要求是batch,几个通道,高,宽(4个参数)
  • weight:尺寸要求是输出,in_channels(groups一般为1),高,宽(4个参数)

使用 torch.reshape 函数,将输入改变为要求输入的维度

实现上图代码

复制代码
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]])   #将二维矩阵转为tensor数据类型
# 卷积核kernel
kernel = torch.tensor([[1,2,1],
                       [0,1,0],
                       [2,1,0]])
 
# 尺寸只有高和宽,不符合要求
print(input.shape)  #5×5
print(kernel.shape)  #3×3
 
# 尺寸变换为四个数字
input = torch.reshape(input,(1,1,5,5))  #通道数为1,batch大小为1
kernel = torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(kernel.shape)
 
output = F.conv2d(input,kernel,stride=1)  # .conv2d(input:Tensor, weight:Tensor, stride)
print(output)

输出结果为:

当将步进 stride 改为 2 时:

复制代码
output2 = F.conv2d(input,kernel,stride=2)
print(output2)

padding(填充)

在输入图像左右两边进行填充,决定填充有多大。可以为一个数或一个元组(分别指定高和宽,即纵向和横向每次填充的大小)。默认情况下不进行填充

padding=1:将输入图像左右上下两边都拓展一个像素,空的地方默认为0

代码实现:

复制代码
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)

d(input,kernel,stride=1,padding=1)

print(output3)

复制代码
[外链图片转存中...(img-xxjUZlUQ-1724861517755)]
相关推荐
夏天是冰红茶3 小时前
DINO原理详解
人工智能·深度学习·机器学习
吴佳浩5 小时前
Python入门指南(六) - 搭建你的第一个YOLO检测API
人工智能·后端·python
SHIPKING3936 小时前
【AI应用开发设计指南】基于163邮箱SMTP服务实现验证登录
人工智能
yong99906 小时前
基于SIFT特征提取与匹配的MATLAB图像拼接
人工智能·计算机视觉·matlab
知秋一叶1236 小时前
Miloco 深度打通 Home Assistant,实现设备级精准控制
人工智能·智能家居
春日见7 小时前
在虚拟机上面无法正启动机械臂的控制launch文件
linux·运维·服务器·人工智能·驱动开发·ubuntu
————A7 小时前
强化学习----->轨迹、回报、折扣因子和回合
人工智能·python
CareyWYR7 小时前
每周AI论文速递(251215-251219)
人工智能
weixin_409383128 小时前
在kaggle训练Qwen/Qwen2.5-1.5B-Instruct 通过中二时期qq空间记录作为训练数据 训练出中二的模型为目标 第一次训练 好像太二了
人工智能·深度学习·机器学习·qwen
JoannaJuanCV8 小时前
自动驾驶—CARLA仿真(22)manual_control_steeringwheel demo
人工智能·自动驾驶·pygame·carla