如何使用numpy在二维数组上执行最大/平均池化 [英] how to perform max/mean pooling on a 2d array using numpy
本文介绍了如何使用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]])
推荐答案
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屋!
查看全文