Conv2d中的stride和padding参数的使用

Conv2d中最常用的参数就是in_channels ,out_channels ,kernel_size ,stride ,padding 这5个,往往需要我们手动输入,本文结合代码介绍了stride和padding参数的使用。

目录

前言

一、Conv2d的官方文档

二、开始练习

1.写入数据

2.conv2d中stride练习

3.conv2d中padding 练习

4.完整代码及结果

总结


前言

Convolution Layers 卷积层内有很多的工具,
nn.Conv1d    1维数据处理
nn.Conv2d    2维数据处理,像图片

其中用的最多的还是Conv2d。


一、Conv2d的官方文档

torch.nn.functional.Conv2d(input: Tensor, 
weight: Tensor, 
bias: Optional[Tensor]=None, 
stride: Union[_int, _size]=1, 
padding: str="valid", 
dilation: Union[_int, _size]=1, 
groups: _int=1)

二、开始练习

1.写入数据

使用手动输入数组,可以使用functional下的conv2d来运行一下,明白卷积的原理,

代码如下:

import torch

#输入数据
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]])
#卷积核
kernal = torch.tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])

#数据尺寸
print(input.shape)
print(kernal.shape)

输出结果:
torch.Size([5, 5])
torch.Size([3, 3])

因为conv2d中输入的数据应该有(N, C, H, W)的。.
其中:
N是batch的大小
C是通道数量
H是输入的高度
W是输入的宽度

所以要进行转换。可以使用pyrhon中的尺寸变化torch.reshape().
代码如下:

import torch

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]])

kernal = torch.tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])
input = torch.reshape(input, (1, 1, 5, 5))
kernal = torch.reshape(kernal, (1, 1, 3, 3))
print(input.shape)
print(kernal.shape)

输出结果为:
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])

这样就符合conv2d的输入要求了。

2.conv2d中stride练习

调用conv2d函数了,代码如下:

import torch.nn.functional as F

output = F.conv2d(input, kernal, stride=1, )      移动的步横向为1,纵向为1
print(output)

output2 = F.conv2d(input, kernal, stride=2)     移动的步横向为2,纵向为2
print(output2)

output3 = F.conv2d(input, kernal, stride=(1, 2))    移动的步横向为1,纵向为2
print(output3)

该处使用的url网络请求的数据。

3.conv2d中padding 练习

代码如下:

output4 = F.conv2d(input, kernal, stride=1, padding=1)    横向上下各填充一行,左右各填充一行,默认填充的为0
print(output4)

output5 = F.conv2d(input, kernal, stride=1, padding=(1, 2))    横向上下各填充一行,左右各填充两行,默认填充的为0
print(output5)

4.完整代码及结果

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]])
#卷积核
kernal = torch.tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])

#卷积后的结果


input = torch.reshape(input, (1, 1, 5, 5))   #转换尺寸
kernal = torch.reshape(kernal, (1, 1, 3, 3)) #转换尺寸

print(input.shape)
print(kernal.shape)

#stride步练习
output = F.conv2d(input, kernal, stride=1, )
print(output)

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

output3 = F.conv2d(input, kernal, stride=(1, 2))
print(output3)

#padding练习
output4 = F.conv2d(input, kernal, stride=1, padding=1)
print(output4)

output5 = F.conv2d(input, kernal, stride=1, padding=(1, 2))
print(output5)


输出结果:
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 12, 12],
          [18, 16, 16],
          [13,  9,  3]]]])
tensor([[[[10, 12],
          [13,  3]]]])
tensor([[[[10, 12],
          [18, 16],
          [13,  3]]]])
tensor([[[[ 1,  3,  4, 10,  8],
          [ 5, 10, 12, 12,  6],
          [ 7, 18, 16, 16,  8],
          [11, 13,  9,  3,  4],
          [14, 13,  9,  7,  4]]]])
tensor([[[[ 0,  1,  3,  4, 10,  8,  2],
          [ 1,  5, 10, 12, 12,  6,  1],
          [ 0,  7, 18, 16, 16,  8,  3],
          [ 1, 11, 13,  9,  3,  4,  2],
          [ 5, 14, 13,  9,  7,  4,  1]]]])


总结

以上就是今天要讲的内容,本文仅仅简单介绍了介绍了conv2d中的stride和padding参数的使用,要想学好conv2d,还有很多东西需要学习。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓亮.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值