当前位置:网站首页>Maxpool2d explanation -- Application in arrays and images
Maxpool2d explanation -- Application in arrays and images
2022-06-30 23:16:00 【Philo`】
MaxPool2d Detailed explanation -- Applications 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
Cover the original data with convolution kernel , Select the maximum value covered by convolution kernel in the original data

Select the maximum value of convolution kernel coverage ,ceil_mode Controls whether the convolution kernel is retained after exceeding the original data
3、 Function requirements
function :
Parameter requirements 
kernel_sizeSet convolution kernel size attributestrideand conv2d Medium stride equally , Is an attribute that controls the movement stride ,Note here ,conv2d The default value is 1, however MaxPool2d The default value is the convolution kernel sizepaddingSet the properties filled around the original datadilation:Indicates the addition of... Between the original data 0 Properties ofceil_modeControls when the convolution kernel exceeds the original image , Whether the max Make a reservation
4、 Example
4.1、 Array
Code :
import torch
import torchvision
from torch.nn import Module,MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
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]],dtype=torch.float32)
print(" front ",input.shape) # torch.Size([5, 5]), The input conditions are not met , Format conversion required
input = torch.reshape(input,(-1,1,5,5))
print(" after ",input.shape) # after torch.Size([1, 1, 5, 5]) One bach_size,
class ConNet(Module):
def __init__(self):
super(ConNet, self).__init__()
# Use of pool layer , Set convolution kernel to 3*3, The excess part retains the data
self.maxpool = MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool(input)
return output
# Instantiate objects
Work = ConNet()
# Neural network call
output = Work(input)
print(output)
result :
4.2、 Images
Code :
import torch
import torchvision
from torch.nn import Module,MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
class ConNet(Module):
def __init__(self):
super(ConNet, self).__init__()
# Use of pool layer , Set convolution kernel to 3*3, The excess part retains the data
self.maxpool = MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool(input)
return output
# Instantiate objects
Work = ConNet()
# CIFAR10 Data usage
dataset = torchvision.datasets.CIFAR10("./datasetvision",train=False,download=False,transform=torchvision.transforms.ToTensor())
# Data loading
dataloader = DataLoader(dataset,batch_size=64)
writer = SummaryWriter("logs_MaxPool")
step = 0
for data in dataloader:
imgs,target = data
writer.add_images("input",imgs,step)
output = Work(imgs)
writer.add_images("output",output,step)
step = step + 1
writer.close()
result :

4.3、Conv2d+MaxPool2d Images
Code :
import torch
import torchvision
from torch.nn import Module,MaxPool2d,Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
class ConNet(Module):
def __init__(self):
super(ConNet, self).__init__()
# Use of pool layer , Set convolution kernel to 3*3, The excess part retains the data
self.maxpool = MaxPool2d(kernel_size=3,ceil_mode=True)
self.conv2d = Conv2d(in_channels=3,out_channels=3,kernel_size=3,stride=1,padding=0)
def forward(self,input):
output = self.conv2d(input)
output = self.maxpool(output)
return output
# Instantiate objects
Work = ConNet()
# CIFAR10 Data usage
dataset = torchvision.datasets.CIFAR10("./datasetvision",train=False,download=False,transform=torchvision.transforms.ToTensor())
# Data loading
dataloader = DataLoader(dataset,batch_size=64)
writer = SummaryWriter("logs_MaxPoolAndConv2d")
step = 0
for data in dataloader:
imgs,target = data
writer.add_images("input",imgs,step)
output = Work(imgs)
writer.add_images("output",output,step)
step = step + 1
writer.close()
result :

边栏推荐
- AtCoder Beginner Contest 257
- Cas classique multithreadé
- Warmup preheating learning rate "suggestions collection"
- What does the software test report contain? How to obtain high quality software test reports?
- During telecommuting, the project team punched in the wechat group | solicited papers from the community
- Introduction to digital transformation solutions for enterprises going to sea
- Solution to the conflict between unique index and logical deletion
- As the public cloud market enters the deep water, can the calm Amazon cloud still sit still?
- Reason why wechat payment wxpaypubhelper V3 callback XML is empty
- Redis - 01 缓存:如何利用读缓存提高系统性能?
猜你喜欢
随机推荐
35家巨头科技公司联合组成元宇宙标准论坛组织
The sandbox is being deployed on the polygon network
Meet the streamnational | yangzike: what made me give up Dachang offer
flutter - sort List排序
leetcode:104. Maximum depth of binary tree
206页上海BIM技术应用与发展报告2021
如何使用 DataAnt 监控 Apache APISIX
云游戏| 云计算推动游戏行业进入“新纪元”
RIDE:获取图片base64
Asynchronous transition scenario - generator
Cloud games | cloud computing drives the game industry into a "new era"
MIT博士论文 | 优化理论与机器学习实践
如何区分平台安全和网上炒作?网络投机有哪些止损技巧?
Reason why wechat payment wxpaypubhelper V3 callback XML is empty
Redis的事务和锁机制
C language array interception, C string by array interception method (c/s)
[Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
How cloud computing can protect online education in the post epidemic Era
DNS server setup, forwarding, master-slave configuration
What does project management really manage?

![[leetcode] [SQL] notes](/img/8d/160a03b9176b8ccd8d52f59d4bb47f.png)






