当前位置:网站首页>Detailed explanation of conv2d -- use in arrays and images
Detailed explanation of conv2d -- use in arrays and images
2022-06-30 23:16:00 【Philo`】
conv2d Detailed explanation -- Use in arrays and images
1、 Environmental requirements
1、 Need to install Pytorch rely on
2、 Official documents conv2d
3、 Picture needs CIFAR10 Data sets
2、 Principle explanation
The original 2D data , Operate through convolution kernel , Get the result of the operation , Specific operation steps :

Through convolution kernel , Overwrite input data , Multiply the selected data and add them , Then you get the output data 
Calculate repeatedly till the end , Get the output
Only when the convolution kernel is safely covered on the original image , But you can also continue to move around , Not completely covered , As long as there is coverage, it can be calculated , Make up for the extra space 0 that will do ;
Here is also left and right up and down, moving one grid by one , You can also move two spaces at a time ;
The two situations mentioned above , yes conv2d Medium padding Parameters and stride The parameter is not the default value
3、 Function requirements
The function prototype :
Parameter requirements :
The data required to be entered on the latest official website is int That's it , This is for picture data , In array data , need tensor data type , See the following examples for detailed differences
- The input requirement is tensor data type , And need minibatch And input channels , The original two-dimensional array has no , Need to use reshape To transform
- Convolution kernel is also the same requirement
3、 Example use
3.1、 Array
Code :
import torch
import torchvision
import torch.nn.functional as F
# input data
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]])
print(" original input shape",input.shape) # torch.Size([5, 5])
input = torch.reshape(input,(1,1,5,5)) # Format conversion , Add the first two parameters ,batchsize=1,channel=1, The data is 5*5 torch.Size([1, 1, 5, 5])
print("torch.shape After shape",input.shape)
# Convolution kernel
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
kernel = torch.reshape(kernel,(1,1,3,3))
# The default convolution uses ,padding=0,stride=1
output1 = F.conv2d(input,kernel)
print(" Default convolution ",output1)
# padding = 1,stride = 1
output2 = F.conv2d(input,kernel,padding = 1,stride = 1)
print("padding = 1,stride = 1",output2)
# padding =1,stride = 2
output3 = F.conv2d(input,kernel,padding =1,stride = 2)
print("padding =1,stride = 2",output3)
Output :
3.2、 picture
Code :
import torch
from torch import nn
from torch.nn import Conv2d
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# download=False, The data set here has been downloaded , You don't have to download it every time you run , Can be the first time , Change it to True Download
# "./datasetvision" Is the storage path
# transform=torchvision.transforms.ToTensor() Use picture data torchvision Format conversion
dataset = torchvision.datasets.CIFAR10("./datasetvision",train=False,
transform=torchvision.transforms.ToTensor(),download=False)
# Data preprocessing ,batch_size=64 Indicates that the number of data obtained each time is 64 Zhang
dataloader = DataLoader(dataset,batch_size=64)
# Simple neural network definition
class ConNet(nn.Module):
def __init__(self):
super(ConNet, self).__init__()
# Input channel Because it's a color image RGB So the input channel is 3 layer , Output 6 layer , The convolution layer is 3*3
self.conv2d = Conv2d(in_channels=3,out_channels=6,kernel_size=3,stride=1,padding=0)
# Define the specific function body
def forward(self,x):
result = self.conv2d(x)
return result
Work = ConNet()
print(Work) # Print out the neural network structure : ConNet((conv2d): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1)))
# Use tensorboard Name the folder
write = SummaryWriter("logsConv2d")
# data yes dataloader Tuples in
step = 0
for data in dataloader:
imgs,target = data
# print(" original image ",imgs.shape) # The difference between before and after
# print(output.shape)
write.add_images("input",imgs,step) # Put the initial image in tensorboard Contrast
output = Work(imgs) # Perform image convolution
output = torch.reshape(output,(-1,3,30,30)) # This is because of convolution , Define the output channel as 6 Channels ,board I don't know how to show , So use reshape convert
write.add_images("output",output,step)
step = step+1
write.close()
result :

边栏推荐
- Arthas debugging problem determination Toolkit
- MaxPool2d详解--在数组和图像中的应用
- QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory
- In 2022, the latest JCR officially released the list of the latest global impact factors (top 600)
- Detailed explanation of conv2d of pytorch
- CNN classic network model details -lenet-5 (pytorch Implementation)
- 76页智慧物流园区综合解决方案2022(附下载)
- 异步过渡方案—Generator
- [fundamentals of wireless communication-13]: illustrated mobile communication technology and application development-1-overview
- Zero sample and small sample learning
猜你喜欢

Solution to the conflict between unique index and logical deletion

Redis' transaction and locking mechanism

在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言

Architecture of IM integrated messaging system sharing 100000 TPS

分享十万级TPS的IM即时通讯综合消息系统的架构

MIT doctoral dissertation optimization theory and machine learning practice

Swift5.0 ----Swift FrameWork的创建及使用

Zero sample and small sample learning

Two way data binding in wechat applet

One revolution, two forces and three links: the "carbon reduction" road map behind the industrial energy efficiency improvement action plan
随机推荐
Fund sales code of conduct and information management
How to use robots Txt and its detailed explanation
基金客户服务
Netease cloud sign in lottery? That year I could sign in for 365 days. No? Look.
微信小程序中的数据双向绑定
Braces on the left of latex braces in latex multiline formula
基金销售行为规范及信息管理
Swift5.0 ----Swift FrameWork的创建及使用
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
Redis的事务和锁机制
Pycharm is very fast to learn from installation to full armament. There are so many pictures because it is too detailed!
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Attached construction tutorial
[Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)
Redis - 01 缓存:如何利用读缓存提高系统性能?
Architecture of IM integrated messaging system sharing 100000 TPS
What are the contents and processes of software validation testing? How much does it cost to confirm the test report?
Flitter - sort list sort
机器学习编译入门课程学习笔记第二讲 张量程序抽象
Sm2246en+ SanDisk 15131