Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
今天在看文档的时候,发现pytorch的conv操作不是很明白,于是有了一下记录
首先提出两个问题:
1.输入图片是单通道情况下的filters是如何操作的?即一通道卷积核卷积过程
2.输入图片是多通道情况下的filters是如何操作的?即多通道多个卷积核卷积过程
这里首先贴出官方文档:
classtorch.nn.Conv2d(in_channels,out_channels,kernel_size,stride=1,padding=0,dilation=1,groups=1,bias=True)[source]
Parameters:
in_channels(int)–Numberofchannelsintheinputimage
out_channels(int)–Numberofchannelsproducedbytheconvolution
kernel_size(intortuple)–Sizeoftheconvolvingkernel
stride(intortuple,optional)–Strideoftheconvolution.Default:1
padding(intortuple,optional)–Zero-paddingaddedtobothsidesoftheinput.Default:0
dilation(intortuple,optional)–Spacingbetweenkernelelements.Default:1
groups(int,optional)–Numberofblockedconnectionsfrominputchannelstooutputchannels.Default:1
bias(bool,optional)–IfTrue,addsalearnablebiastotheoutput.Default:True
这个文档中的公式对我来说,并不能看的清楚
一通道卷积核卷积过程:
比如32个卷积核,可以学习32种特征。在有多个卷积核时,如下图所示:输出就为32个featuremap
也就是,当conv2d(in_channels=1,out_channels=N)
有N个filter对输入进行滤波。同时输出N个结果即featuremap,每个filter滤波输出一个结果.
importtorch fromtorch.autogradimportVariable ##单位矩阵来模拟输入 input=torch.ones(1,1,5,5) input=Variable(input) x=torch.nn.Conv2d(in_channels=1,out_channels=3,kernel_size=3,groups=1) out=x(input) print(out) print(list(x.parameters()))
输出out的结果和conv2d的参数如下,可以看到,conv2d是有3个filter加一个bias
#out的结果 Variablecontaining: (0,0,.,.)= -0.3065-0.3065-0.3065 -0.3065-0.3065-0.3065 -0.3065-0.3065-0.3065 (0,1,.,.)= -0.3046-0.3046-0.3046 -0.3046-0.3046-0.3046 -0.3046-0.3046-0.3046 (0,2,.,.)= 0.07100.07100.0710 0.07100.07100.0710 0.07100.07100.0710 [torch.FloatTensorofsize1x3x3x3] #conv2d的参数 [Parametercontaining: (0,0,.,.)= -0.0789-0.1932-0.0990 0.1571-0.1784-0.2334 0.0311-0.25950.2222 (1,0,.,.)= -0.0703-0.3159-0.3295 0.07230.30190.2649 -0.22170.0680-0.0699 (2,0,.,.)= -0.0736-0.16080.1905 0.27380.2758-0.2776 -0.0246-0.1781-0.0279 [torch.FloatTensorofsize3x1x3x3] ,Parametercontaining: 0.3255 -0.0044 0.0733 [torch.FloatTensorofsize3] ]
验证如下,因为是单位矩阵,所以直接对参数用sum()来模拟卷积过程:
f_p=list(x.parameters())[0] f_p=f_p.data.numpy() print("theresultoffirstchannelinimage:",f_p[0].sum()+(0.3255))
可以看到结果是和(0,0,.,.)=-0.3065....一样的.说明操作是通过卷积求和的.
theresultoffirstchannelinimage:-0.306573044777
多通道卷积核卷积过程:
下图展示了在四个通道上的卷积操作,有两个卷积核,生成两个通道。其中需要注意的是,四个通道上每个通道对应一个卷积核,先将w2忽略,只看w1,那么在w1的某位置(i,j)处的值,是由四个通道上(i,j)处的卷积结果相加得到的。所以最后得到两个featuremap,即输出层的卷积核核个数为featuremap的个数。
在pytorch中的展示为
conv2d(in_channels=X(x>1),out_channels=N)
有N乘X个filter(N组filters,每组X个)对输入进行滤波。即每次有一组里X个filter对原X个channels分别进行滤波最后相加输出一个结果,最后输出N个结果即featuremap。
验证如下:
##单位矩阵来模拟输入 input=torch.ones(1,3,5,5) input=Variable(input) x=torch.nn.Conv2d(in_channels=3,out_channels=4,kernel_size=3,groups=1) out=x(input) print(list(x.parameters()))
可以看到共有4*3=12个filter和一个1×4的bias作用在这个(3,5,5)的单位矩阵上
##out输出的结果 Variablecontaining: (0,0,.,.)= -0.6390-0.6390-0.6390 -0.6390-0.6390-0.6390 -0.6390-0.6390-0.6390 (0,1,.,.)= -0.1467-0.1467-0.1467 -0.1467-0.1467-0.1467 -0.1467-0.1467-0.1467 (0,2,.,.)= 0.41380.41380.4138 0.41380.41380.4138 0.41380.41380.4138 (0,3,.,.)= -0.3981-0.3981-0.3981 -0.3981-0.3981-0.3981 -0.3981-0.3981-0.3981 [torch.FloatTensorofsize1x4x3x3] ##x的参数设置 [Parametercontaining: (0,0,.,.)= -0.08030.1473-0.0762 0.0284-0.0050-0.0246 0.14380.0955-0.0500 (0,1,.,.)= 0.07160.0062-0.1472 0.17930.0543-0.1764 -0.15480.13790.1143 (0,2,.,.)= -0.1741-0.1790-0.0053 -0.0612-0.1856-0.0858 -0.05530.1621-0.1822 (1,0,.,.)= -0.0773-0.13850.1356 0.1794-0.0534-0.1110 -0.0137-0.1744-0.0188 (1,1,.,.)= -0.03960.01490.1537 0.0846-0.1123-0.0556 -0.1047-0.1783-0.0630 (1,2,.,.)= 0.18500.03250.0332 -0.04870.00180.1668 0.05690.02670.0124 (2,0,.,.)= 0.1880-0.0152-0.1088 -0.01050.1805-0.0343 -0.16760.12490.1872 (2,1,.,.)= 0.02990.04490.1179 0.1280-0.15450.0593 -0.14890.1378-0.1495 (2,2,.,.)= -0.09220.1873-0.1163 0.0970-0.0682-0.1110 0.0614-0.18770.1918 (3,0,.,.)= -0.1257-0.0814-0.1923 0.0048-0.0789-0.0048 0.0780-0.02900.1287 (3,1,.,.)= -0.06490.0773-0.0584 0.0092-0.1168-0.0923 0.06140.11590.0134 (3,2,.,.)= 0.0426-0.10550.1022 -0.08100.0540-0.1011 0.0698-0.0799-0.0786 [torch.FloatTensorofsize4x3x3x3] ,Parametercontaining: -0.1367 -0.0410 0.0424 0.1353 [torch.FloatTensorofsize4] ]
因为是单位矩阵,所以直接对参数用sum()来模拟卷积过程,结果-0.639065589142与之前的out结果的(0,0,.,.)=-0.6390相同,即conv2d是通过利用4组filters,每组filter对每个通道分别卷积相加得到结果。
f_p=list(x.parameters())[0] f_p=f_p.data.numpy() print(f_p[0].sum()+(-0.1367)) -0.639065589142
再更新
importtorch fromtorch.autogradimportVariable input=torch.ones(1,1,5,5) input=Variable(input) x=torch.nn.Conv2d(in_channels=1,out_channels=3,kernel_size=3,groups=1) out=x(input) f_p=list(x.parameters())[0] f_p=f_p.data.numpy() f_b=list(x.parameters())[1] f_b=f_b.data.numpy() print("outputresultis:",out[0][0]) print("theresultoffirstchannelinimage:",f_p[0].sum()+f_b[0])
outputresultis:Variablecontaining:
0.65770.65770.6577
0.65770.65770.6577
0.65770.65770.6577
[torch.FloatTensorofsize3x3]
theresultoffirstchannelinimage:0.657724
input=torch.ones(1,3,5,5) input=Variable(input) print(input.size()) x=torch.nn.Conv2d(in_channels=3,out_channels=4,kernel_size=3,groups=1) out=x(input) f_p=list(x.parameters())[0] f_b=list(x.parameters())[1] f_p=f_p.data.numpy() f_b=f_b.data.numpy() #print(f_p[...,0]) #print(f_p[...,0].shape) #print(f_p[...,0].sum()+f_b[0]) print("outputresult:",out[0][0]) print("simlatuatetheresult:",f_p[0].sum()+f_b[0])
torch.Size([1,3,5,5])
outputresult:Variablecontaining:
-0.2087-0.2087-0.2087
-0.2087-0.2087-0.2087
-0.2087-0.2087-0.2087
[torch.FloatTensorofsize3x3]
simlatuatetheresult:-0.208715
以上这篇Pytorch.nn.conv2d过程验证方式(单,多通道卷积过程)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。