如何使用 numpy 在二维数组上执行最大/平均池化 [英] how to perform max/mean pooling on a 2d array using numpy

查看:79
本文介绍了如何使用 numpy 在二维数组上执行最大/平均池化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 2D(M x N) 矩阵和一个 2D Kernel(K x L),我如何返回一个矩阵,该矩阵是在图像上使用给定内核的最大或平均池化的结果?

Given a 2D(M x N) matrix, and a 2D Kernel(K x L), how do i return a matrix that is the result of max or mean pooling using the given kernel over the image?

如果可能,我想使用 numpy.

I'd like to use numpy if possible.

注意:M、N、K、L 既可以是偶数也可以是奇数,并且它们不需要完全可以被彼此整除,例如:7x5 矩阵和 2x2 核.

Note: M, N, K, L can be both even or odd and they need not be perfectly divisible by each other, eg: 7x5 matrix and 2x2 kernel.

例如最大池化:

matrix:
array([[  20,  200,   -5,   23],
       [ -13,  134,  119,  100],
       [ 120,   32,   49,   25],
       [-120,   12,   09,   23]])
kernel: 2 x 2
soln:
array([[  200,  119],
       [  120,   49]])

推荐答案

你可以使用 scikit-image block_reduce:

You could use scikit-image block_reduce:

import numpy as np
import skimage.measure

a = np.array([
      [  20,  200,   -5,   23],
      [ -13,  134,  119,  100],
      [ 120,   32,   49,   25],
      [-120,   12,    9,   23]
])
skimage.measure.block_reduce(a, (2,2), np.max)

给出:

array([[200, 119],
       [120,  49]])

这篇关于如何使用 numpy 在二维数组上执行最大/平均池化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆