当前位置:网站首页>MaxPool2d详解--在数组和图像中的应用
MaxPool2d详解--在数组和图像中的应用
2022-06-30 23:05:00 【Philo`】
1、环境要求
1、需要安装Pytorch依赖
2、官方文档conv2d
3、图片需要CIFAR10数据集
2、原理讲解
用卷积核覆盖在原始数据上,选择原始数据中被卷积核覆盖的最大值

选择卷积核覆盖时的最大值,ceil_mode控制卷积核超出原始数据后是否进行保留
3、函数要求
函数:
参数要求
kernel_size设置卷积核大小的属性stride和conv2d中的stride一样,是控制移动步幅的属性,这里注意,conv2d默认值是1,但是MaxPool2d默认值是卷积核大小padding设置原始数据周围填充的属性dilation:表明给原始数据之间添加0的属性ceil_mode控制当卷积核超过原始图像时,是否对max进行保留
4、例子
4.1、数组
代码:
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("前",input.shape) # torch.Size([5, 5]),不满足输入的条件,需要进行格式转换
input = torch.reshape(input,(-1,1,5,5))
print("后",input.shape) # 后 torch.Size([1, 1, 5, 5]) 一个bach_size,
class ConNet(Module):
def __init__(self):
super(ConNet, self).__init__()
# 池化层使用,设置卷积核为3*3,超出的部分保留数据
self.maxpool = MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool(input)
return output
# 实例化对象
Work = ConNet()
# 神经网络调用
output = Work(input)
print(output)
结果:
4.2、图像
代码:
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__()
# 池化层使用,设置卷积核为3*3,超出的部分保留数据
self.maxpool = MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool(input)
return output
# 实例化对象
Work = ConNet()
# CIFAR10数据使用
dataset = torchvision.datasets.CIFAR10("./datasetvision",train=False,download=False,transform=torchvision.transforms.ToTensor())
# 数据加载
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()
结果:

4.3、Conv2d+MaxPool2d图像
代码:
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__()
# 池化层使用,设置卷积核为3*3,超出的部分保留数据
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
# 实例化对象
Work = ConNet()
# CIFAR10数据使用
dataset = torchvision.datasets.CIFAR10("./datasetvision",train=False,download=False,transform=torchvision.transforms.ToTensor())
# 数据加载
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()
结果:

边栏推荐
- What is flush software? In addition, is it safe to open an account online now?
- Spark - understand partitioner in one article
- AtCoder Beginner Contest 255
- Cas classique multithreadé
- Youfu network hybrid cloud accelerates enterprise digital transformation and upgrading
- Kubevela 1.4: make application delivery safer, easier to use, and more transparent
- Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance
- Asynchronous transition scenario - generator
- 一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图
- “飞桨+辨影相机”成为AI界的“预制菜”,工业AI质检落地更简单
猜你喜欢

How to change the win11 computer name? Win11 method of changing computer name

一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图

理想中的接口自动化项目

As the public cloud market enters the deep water, can the calm Amazon cloud still sit still?

QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory

What does the &?

shell 同时执行多任务下载视频

206页上海BIM技术应用与发展报告2021

2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main() { ints := make

Redis - 01 缓存:如何利用读缓存提高系统性能?
随机推荐
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
手机上怎么开股票账户?另外,手机开户安全么?
CTFSHOW框架复现篇
How do I open a stock account on my mobile phone? In addition, is it safe to open a mobile account?
QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory
What are database OLAP and OLTP? Same and different? Applicable scenarios
微信小程序通过点击事件传参(data-)
[golang] golang implements the string interception function substr
微信小程序中的数据双向绑定
对于产业互联网的粗浅认识,最终将产业互联网的发展带入到了消费互联网的怪圈之中
The sandbox is being deployed on the polygon network
35 giant technology companies jointly form the meta universe standard Forum Organization
多線程經典案例
Redis - 01 缓存:如何利用读缓存提高系统性能?
leetcode:104. Maximum depth of binary tree
Swift5.0 ----Swift FrameWork的创建及使用
Strictly minor spanning tree
基金销售行为规范及信息管理
Qlineedit of QT notes (74) specifies the input type
Golang application ━ installation, configuration and use of Hugo blog system