当前位置:网站首页>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 ?
边栏推荐
- How close can QA be to business code QA conducts testability transformation on business code
- Alibaba Sentinel - workflow and principle analysis
- 04 | background login: login method based on account and password (Part 1)
- SAP 中国本地化内容汇总
- 多行文本省略
- 力扣刷题之分数加减运算(每日一题7/27)
- Object转String的几种方法
- 年内首个“三连跌” 95号汽油回归“8元时代“
- C traps and defects Chapter 3 semantic "traps" 3.7 evaluation order
- ROS - create workspace
猜你喜欢

01-sdram: Code of initialization module

照片比例校正工具:DxO ViewPoint 3 直装版

Singleton mode (hungry and lazy)

【C】 Array

Unity 之游戏特效

Wechat's crazy use of glide - life cycle learning

What if MySQL forgets the password

Apache文件管理自学笔记——映射文件夹和基于单ip多域名配置apache虚拟机

How to deploy sentinel cluster of redis

ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?
随机推荐
July 28, 2022 Gu Yujia's study notes
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
Introduction and advanced level of MySQL (11)
2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
Introduction and advanced level of MySQL (12)
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
Singleton and invariant modes of concurrent mode
Digital image processing Chapter 10 - image segmentation
C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters
Watermelon book learning Chapter 6 -- SVM
ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?
Pp-yoloe details
"PHP Basics" output approximate value of PI
Regular expression bypasses WAF
How to deploy sentinel cluster of redis
Engineering boy: under 20 years old, ordinary but not mediocre
美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
C and pointer Chapter 3 semantic "trap" 3.5 null pointer is not a string