今天主要讲解 Conv2d
此处补充dilation,叫做空洞卷积:
一般卷积操作时,会一一对应相乘累加求和;
原本是这样紧密相邻做卷积
dilation的话:
会作间隔后卷积:
一、卷积操作:
1 、假设输入图像是5×5 卷积核3×3
2、计算
对应位置相乘然后累加结果:10
3、参数中stride的作用:卷积核移动位数
stride=1时的输出
特别注意
如果stride=2
卷积核走到右边缘时。会从左边缘向下走两步再开始计算
也就是说stride=2时,只有四个输出结果。
二、预处理:
1、再来读文档
一般用上边这个
这次为了理解在下面fun
我们来看参数
input 尺寸要求:要有batch,通道,高宽
weight权重:输出通道,高宽
2、来看下自己数据的尺寸
import torch
input=torch.tensor([[1,2,0,3,1], //这里把数据以tensor类型输入//
[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]])
print(input.shape)
print(kernel.sh
输出结果:
torch.Size([5, 5])
torch.Size([3, 3])
可以看到输出只有高和宽;
不满足input需要的(minibatch,inchannel,高iH,宽iW)
3、因此我们用torch的工具reshape
因为这里是一张图片?所以batch设为1
假设是灰度图所以通道,channel设为1
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]]
)
kernel=torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
input=torch.reshape(input,(1,1,5,5))
kernel=torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(kernel.shape)
看下结果:
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
三、程序实战:
1、
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]]
)
kernel=torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
input=torch.reshape(input,(1,1,5,5))
kernel=torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(kernel.shape)
.//此处开始//
import torch.nn.functional as F
F.conv2d()
2、继续
import torch.nn.functional as F
output=F.conv2d(input,kernel,stride=1)
print(output)
输出结果如下,恰好是刚才算的。
tensor([[[[10, 12, 12],
[18, 16, 16],
[13, 9, 3]]]])
3、我们再看stride=2是啥样的
恰好是4个数,没毛病!
import torch.nn.functional as F
output=F.conv2d(input,kernel,stride=1)
output2=F.conv2d(input,kernel,stride=2)
print(output)
print(output2)
结果:
tensor([[[[10, 12],
[13, 3]]]])
四、来看padding参数:”
1、作用:拓展像素
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓假设padding为1:
空的地方默认值为0
2、代码来看:
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]]
)
kernel=torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
input=torch.reshape(input,(1,1,5,5))
kernel=torch.reshape(kernel,(1,1,3,3))
import torch.nn.functional as F
output=F.conv2d(input,kernel,stride=1)
output2=F.conv2d(input,kernel,stride=2)
print(output)
print(output2)
//原来基础上输入//
output3=F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
结果:
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]]]])
padding一般是更关注于边缘特征。。