当前位置: 首页 > 编程笔记 >

Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)

东深
2023-03-14
本文向大家介绍Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程),包括了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) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (intortuple) – Size of the convolving kernel
stride (intortuple,optional) – Stride of the convolution. Default: 1
padding (intortuple,optional) – Zero-padding added to both sides of the input. Default: 0
dilation (intortuple,optional) – Spacing between kernel elements. Default: 1
groups (int,optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool,optional) – If True, adds a learnable bias to the output. Default: True

这个文档中的公式对我来说,并不能看的清楚

一通道卷积核卷积过程:

比如32个卷积核,可以学习32种特征。在有多个卷积核时,如下图所示:输出就为32个feature map

也就是, 当conv2d( in_channels = 1 , out_channels = N)

有N个filter对输入进行滤波。同时输出N个结果即feature map,每个filter滤波输出一个结果.

import torch
from torch.autograd import Variable
##单位矩阵来模拟输入
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的结果
Variable containing:
(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.0710 0.0710 0.0710
 0.0710 0.0710 0.0710
 0.0710 0.0710 0.0710
[torch.FloatTensor of size 1x3x3x3]

# conv2d的参数
[Parameter containing:
(0 ,0 ,.,.) = 
 -0.0789 -0.1932 -0.0990
 0.1571 -0.1784 -0.2334
 0.0311 -0.2595 0.2222

(1 ,0 ,.,.) = 
 -0.0703 -0.3159 -0.3295
 0.0723 0.3019 0.2649
 -0.2217 0.0680 -0.0699

(2 ,0 ,.,.) = 
 -0.0736 -0.1608 0.1905
 0.2738 0.2758 -0.2776
 -0.0246 -0.1781 -0.0279
[torch.FloatTensor of size 3x1x3x3]
, Parameter containing:
 0.3255
-0.0044
 0.0733
[torch.FloatTensor of size 3]
]

验证如下,因为是单位矩阵,所以直接对参数用sum()来模拟卷积过程:

f_p=list(x.parameters())[0]
f_p=f_p.data.numpy()
print("the result of first channel in image:", f_p[0].sum()+(0.3255))

可以看到结果是和(0 ,0 ,.,.) = -0.3065 ....一样的. 说明操作是通过卷积求和的.

the result of first channel in image: -0.306573044777

多通道卷积核卷积过程:

下图展示了在四个通道上的卷积操作,有两个卷积核,生成两个通道。其中需要注意的是,四个通道上每个通道对应一个卷积核,先将w2忽略,只看w1,那么在w1的某位置(i,j)处的值,是由四个通道上(i,j)处的卷积结果相加得到的。 所以最后得到两个feature map, 即输出层的卷积核核个数为 feature map 的个数。

在pytorch 中的展示为

conv2d( in_channels = X(x>1) , out_channels = N)

有N乘X个filter(N组filters,每组X 个)对输入进行滤波。即每次有一组里X个filter对原X个channels分别进行滤波最后相加输出一个结果,最后输出N个结果即feature map。

验证如下:

##单位矩阵来模拟输入
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输出的结果
Variable containing:
(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.4138 0.4138 0.4138
 0.4138 0.4138 0.4138
 0.4138 0.4138 0.4138

(0 ,3 ,.,.) = 
 -0.3981 -0.3981 -0.3981
 -0.3981 -0.3981 -0.3981
 -0.3981 -0.3981 -0.3981
[torch.FloatTensor of size 1x4x3x3]

## x的参数设置
[Parameter containing:
(0 ,0 ,.,.) = 
 -0.0803 0.1473 -0.0762
 0.0284 -0.0050 -0.0246
 0.1438 0.0955 -0.0500

(0 ,1 ,.,.) = 
 0.0716 0.0062 -0.1472
 0.1793 0.0543 -0.1764
 -0.1548 0.1379 0.1143

(0 ,2 ,.,.) = 
 -0.1741 -0.1790 -0.0053
 -0.0612 -0.1856 -0.0858
 -0.0553 0.1621 -0.1822

(1 ,0 ,.,.) = 
 -0.0773 -0.1385 0.1356
 0.1794 -0.0534 -0.1110
 -0.0137 -0.1744 -0.0188

(1 ,1 ,.,.) = 
 -0.0396 0.0149 0.1537
 0.0846 -0.1123 -0.0556
 -0.1047 -0.1783 -0.0630

(1 ,2 ,.,.) = 
 0.1850 0.0325 0.0332
 -0.0487 0.0018 0.1668
 0.0569 0.0267 0.0124

(2 ,0 ,.,.) = 
 0.1880 -0.0152 -0.1088
 -0.0105 0.1805 -0.0343
 -0.1676 0.1249 0.1872

(2 ,1 ,.,.) = 
 0.0299 0.0449 0.1179
 0.1280 -0.1545 0.0593
 -0.1489 0.1378 -0.1495

(2 ,2 ,.,.) = 
 -0.0922 0.1873 -0.1163
 0.0970 -0.0682 -0.1110
 0.0614 -0.1877 0.1918

(3 ,0 ,.,.) = 
 -0.1257 -0.0814 -0.1923
 0.0048 -0.0789 -0.0048
 0.0780 -0.0290 0.1287

(3 ,1 ,.,.) = 
 -0.0649 0.0773 -0.0584
 0.0092 -0.1168 -0.0923
 0.0614 0.1159 0.0134

(3 ,2 ,.,.) = 
 0.0426 -0.1055 0.1022
 -0.0810 0.0540 -0.1011
 0.0698 -0.0799 -0.0786
[torch.FloatTensor of size 4x3x3x3]
, Parameter containing:
-0.1367
-0.0410
 0.0424
 0.1353
[torch.FloatTensor of size 4]
]

因为是单位矩阵,所以直接对参数用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

再更新

import torch
from torch.autograd import Variable
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("output result is:", out[0][0])
print("the result of first channel in image:", f_p[0].sum()+f_b[0])

output result is: Variable containing:
0.6577 0.6577 0.6577
0.6577 0.6577 0.6577
0.6577 0.6577 0.6577
[torch.FloatTensor of size 3x3]

the result of first channel in image: 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("output result :",out[0][0])
print("simlatuate the result:", f_p[0].sum()+f_b[0])

torch.Size([1, 3, 5, 5])
output result : Variable containing:
-0.2087 -0.2087 -0.2087
-0.2087 -0.2087 -0.2087
-0.2087 -0.2087 -0.2087
[torch.FloatTensor of size 3x3]

simlatuate the result: -0.208715

以上这篇Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持小牛知识库。

 类似资料:
  • 然而,我不明白如何扩展这个模型来处理多个通道。每个特征图是否需要三个独立的权重集,并在每种颜色之间共享? 参考本教程的“共享权重”部分:http://deeplearning.net/tutorial/lenet.html特征图中的每个神经元都引用层m-1,颜色是从单独的神经元引用的。我不明白他们在这里表达的关系。神经元是核还是像素?为什么它们引用图像的不同部分? 根据我的例子,一个神经元内核似乎

  • 我在Kinesis数据分析中使用Apache flink。 Flink版本:1.13.2 Jave:1.11 我正在使用来自Kafka的json消息。输入记录示例如下所示 我想使用车辆类型(CAR OR TRUCK)和车辆模型类型(丰田、本田或福特)将这些记录汇总到20秒窗口中。SQL类比(sum(),按车辆类型分组,车辆模型类型) 我使用聚合函数来实现这一点。 在上面的代码中,我也没有看到合并代

  • 本文向大家介绍jquery 表单验证之通过 class验证表单不为空,包括了jquery 表单验证之通过 class验证表单不为空的使用技巧和注意事项,需要的朋友参考一下 在开发系统时,往往都有某些表单数据为必填项,若用jQuery通过ID去验证,不仅会影响效率,还会有所遗漏,不易于后期维护。 本章将介绍如何利用jQuery,通过为表单配置class进行统一验证。(ID一个页面只可以使用一次;cl

  • 我们目前正在从遗留安全子系统迁移到Elytron,并在JBoss EAP 7.3.6中部署了一个基于Struts2的web应用程序,该应用程序应该支持多种“风格”的身份验证。 登录的标准方式应该是,用户以登录表单()手动提供凭据,然后单击相应的按钮。这在我们的设置中与Elytron配合得很好。 第二种可能性是,对Web应用程序受保护内容的GET请求可能包含包含JWT令牌的自定义cookie。这个c

  • 我正在关注TensorFlow的“专家深度列表”教程:https://www.tensorflow.org/tutorials/mnist/pros/ 第二卷积层具有形状【5、5、32、64】;也就是说,它有32个输入,而第一个卷积层有1个输入(该输入是我了解原始图像的灰度值)。 第二个卷积层有32个输入通道意味着什么?这是否意味着在第二层中学习的64个过滤器将全部应用(移位)到每像素具有32个点

  • 本文向大家介绍Vue表单验证插件的制作过程,包括了Vue表单验证插件的制作过程的使用技巧和注意事项,需要的朋友参考一下 前言 前段时间,老大搭好了Vue的开发环境,于是我们愉快地从JQ来到了Vue。这中间做的时候,在表单验证上做的不开心,看到vue的插件章节,感觉自己也能写一个,因此就自己开始写了一个表单验证插件va.js。 当然为什么不找个插件呢? vue-validator呀。 1.我想了下,

  • 我正在尝试编写一个Python脚本,通过Spotify应用程序创建一个Spotify播放列表。我已经在Spotify的开发中心成功创建了一个应用程序,并将必要的变量输入到Spotipy的示例中(见下文)。但是,该脚本没有正确授权。我在服务器运行和不运行的情况下都进行了尝试,但都无济于事。堆栈上有类似的问题,但它们并没有提供完整的解决方案。任何建议都会很有帮助。 这是我通过命令提示符收到的消息: 用

  • 问题内容: 我需要一个存储过程,该过程可以通过发送和来检查在登录尝试时是否是否为有效用户,并查看它们在数据库中是否匹配。有没有简单的方法可以做到这一点? 问题答案: 没有更多的信息,我目前能提供的最好的服务是: 根据您的响应修改查询-这将返回字符串“ true”或“ false”,您可以根据需要将其分别替换为位值1和0。