当前位置:网站首页>3.1 common neural network layer (I) image correlation layer
3.1 common neural network layer (I) image correlation layer
2022-07-29 03:22:00 【smiling0927】
Image correlation layer mainly includes convolution layer (Conv)、 Pooling layer (Pool) etc. , These layers can be divided into one dimension in practical use (1D)、 A two-dimensional (2D) And 3D (3D), Pooling mode is divided into average pooling (AvgPool)、 Maximum pooling (MaxPool)、 Adaptive pooling (AdaptiveAvgPool) etc. . Convolution layer in addition to the commonly used forward convolution , And then there's deconvolution (TransposeConv).
from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
import matplotlib.pyplot as plt
import torch as t
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable as V
to_tensor=ToTensor()#image-->tensor
to_pil=ToPILImage()# and transforms.ToPILImage Will be Tensor or numpy.ndarray Turn into PIL.Image
lena=Image.open('/home/yangyuting/Desktop/20180925183958930.jpeg')
lena = lena.convert('L')# Convert the image to grayscale
plt.figure("pic")
plt.imshow(lena)
plt.show()The image after convolution of the image :
# The input is a batch,batch_size=1
input=to_tensor(lena).unsqueeze(0)
# Sharpen the convolution kernel
kernel=t.ones(3,3)/-9
kernel[1][1]=1# The convolution kernel is centered on 1, Surrounded by 1/9 Convolution kernel
conv=nn.Conv2d(1,1,(3,3),1,bias=False)#(in_channels, out_channels, kernel_size, stride=1)
conv.weight.data=kernel.view(1,1,3,3)#(batch_size,in_channels, kernel_size)
out=conv(V(input))
to_pil(out.data.squeeze(0))
plt.figure("pic")
plt.imshow(to_pil(out.data.squeeze(0)))
plt.show()Linear: Fully connected layer
# Input batch_size=2, dimension =3
input=V(t.randn(2,3))
linear=nn.Linear(3,4)
h=linear(input)
print(h)BatchNorm: Batch normalization layer , It is divided into 1D,2D, 3 D
#4channel, The initialization standard deviation is 4, The mean for 0
bn=nn.BatchNorm1d(4)
bn.weight.data=t.ones(4)*4
bn.bias.data=t.zeros(4)
bn_out=bn(h)
bn_out.mean(0),bn_out.var(0,unbiased=False)Dropout:dropout layer , To prevent over fitting , Also divided into 1D,2D, 3 D
from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
import matplotlib.pyplot as plt
import torch as t
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable as V
to_tensor=ToTensor()#image-->tensor
to_pil=ToPILImage()
lena=Image.open('/home/yangyuting/Desktop/20180925183958930.jpeg')
lena = lena.convert('L')
# plt.figure("pic")
# plt.imshow(lena)
# plt.show()
# The input is a batch,batch_size=1
input=to_tensor(lena).unsqueeze(0)
# Sharpen the convolution kernel
kernel=t.ones(3,3)/-9
kernel[1][1]=1
conv=nn.Conv2d(1,1,(3,3),1,bias=False)
conv.weight.data=kernel.view(1,1,3,3)
out=conv(V(input))
to_pil(out.data.squeeze(0))
# plt.figure("pic")
# plt.imshow(to_pil(out.data.squeeze(0)))
# plt.show()
pool=nn.AvgPool2d(2,2)
list(pool.parameters())
out=pool(V(input))
to_pil(out.data.squeeze(0))
# plt.figure("pic")
# plt.imshow(to_pil(out.data.squeeze(0)))
# plt.show()
# Fully connected layer Linear
# Input batch_size=2, dimension =3
input=V(t.randn(2,3))
linear=nn.Linear(3,4)
h=linear(input)
#print(h)
#4channel, The initialization standard deviation is 4, The mean for 0
bn=nn.BatchNorm1d(4)
bn.weight.data=t.ones(4)*4
bn.bias.data=t.zeros(4)
bn_out=bn(h)
bn_out.mean(0),bn_out.var(0,unbiased=False)
print(bn_out)
# Each element is represented by 0.5 The probability of abandoning # About half of the numbers become 0
dropout=nn.Dropout(0.5)
O=dropout(bn_out)
print(O)
m = nn.Dropout (p=0.2)
input = t.randn (3, 4)
print(input)
output = m (input)
print(output)
Output :
tensor([[-3.9998, -3.9999, 3.9996, 3.9998],
[ 3.9998, 3.9999, -3.9996, -3.9998]],
grad_fn=<NativeBatchNormBackward>)
tensor([[-0.0000, -7.9997, 7.9993, 0.0000],
[ 0.0000, 7.9997, -0.0000, -0.0000]], grad_fn=<MulBackward0>)
tensor([[-0.6623, 1.3807, 2.1652, -0.1931],
[ 0.6697, -0.2446, 1.3638, 0.5675],
[ 1.9312, 0.8611, 0.7551, 0.2626]])
tensor([[-0.8278, 1.7259, 2.7066, -0.2414],
[ 0.8371, -0.0000, 1.7047, 0.7094],
[ 2.4140, 0.0000, 0.9439, 0.0000]])
Be careful :input Part of it becomes 0 in the future , Other vectors are scaled , That is to multiply by 1/(1-p). Therefore, it will be found that during the experiment ,input and output It's not a simple partial zero . How to explain in the image remains to be studied ?
边栏推荐
- Matlab learning -- structured programs and user-defined functions
- Example analysis of while, repeat and loop loops in MySQL process control
- How close can QA be to business code QA conducts testability transformation on business code
- Shell script summary
- Reproduce 20 character short domain name bypass and XSS related knowledge points
- 13_ UE4 advanced_ Montage animation realizes attack while walking
- Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
- Apache文件管理自学笔记——映射文件夹和基于单ip多域名配置apache虚拟机
- Calculation of array serial number of force deduction questions (daily question 7/28)
- 「PHP基础知识」输出圆周率的近似值
猜你喜欢

Inventory of domestic and foreign project collaborative management software: SAAS and customization become a trend

Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names

The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled

Example analysis of while, repeat and loop loops in MySQL process control

STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
![[robot learning] matlab kinematics and ADMAS dynamics analysis of manipulator gripper](/img/e1/a67048ea69bfe6064651d459311a88.png)
[robot learning] matlab kinematics and ADMAS dynamics analysis of manipulator gripper

基于单片机烟雾温湿度甲醛监测设计

Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind

How does DataGrid export and recover the entire database data, using a single SQL file

力扣刷题之数组序号计算(每日一题7/28)
随机推荐
i. MX 8m plus integrated dedicated neural processing engine (NPU)
一种简单通用的获取函数栈空间大小的方法
SAP 中国本地化内容汇总
LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路
Bingbing learning notes: operator overloading -- implementation of date class
Incremental real-time disaster recovery notes
Swing V2: towards a larger model with larger capacity and higher resolution
腾讯云使用pem登录
【C】 Array
Introduction and advanced MySQL (XIV)
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Photo scale correction tool: DxO viewpoint 3 direct mount version
STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)
Mathematical modeling -- analytic hierarchy process model
Flask的创建的流程day05-06之创建项目
CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
Server operation management system
C traps and defects Chapter 3 semantic "traps" 3.2 pointers to non arrays
ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?