[SciPy-User] downsampling with max in 2d

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] downsampling with max in 2d

Dan Stowell-4
Hi all,

I would like to downsample 2D arrays using max-pooling. In other words,
to downsample by a factor of 2 would reduce

 a b c d
 e f g h
 i j k l
 m n o p

to

 max(a,b,e,f) max(c,d,g,h)
 max(i,j,m,n) max(k,l,o,p)


I've searched through numpy and scipy and not found a method for this,
but I'd be grateful for pointers if I've missed it.
In the meantime, I wrote the following function. If you can spot a
faster/better ways to achieve this, please do say:


def maxdownsample2d(data, factor):
        """Supply a 2D numpy array, and an integer factor by which to
downsample (by nonoverlapping maxpooling) in both directions"""
        # factor might not be exact fit, so we trim to this.
        trimrows = int(np.floor(data.shape[0] / float(factor))) * factor
        trimcols = int(np.floor(data.shape[1] / float(factor))) * factor
        first = True
        for coff in range(factor):
                for roff in range(factor):
                        hopped = data[roff:trimrows:factor, coff:trimcols:factor]
                        if first:
                                maxsofar = hopped
                                first = False
                        else:
                                maxsofar = np.maximum(maxsofar, hopped)
        return maxsofar



Best
Dan
--
Dan Stowell
Postdoctoral Research Assistant
Centre for Digital Music
Queen Mary, University of London
Mile End Road, London E1 4NS
http://c4dm.eecs.qmul.ac.uk/people/dans.htm
http://www.mcld.co.uk/
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: downsampling with max in 2d

josef.pktd


On Thu, Feb 6, 2014 at 3:54 PM, Dan Stowell <[hidden email]> wrote:
Hi all,

I would like to downsample 2D arrays using max-pooling. In other words,
to downsample by a factor of 2 would reduce

 a b c d
 e f g h
 i j k l
 m n o p

to

 max(a,b,e,f) max(c,d,g,h)
 max(i,j,m,n) max(k,l,o,p)


I've searched through numpy and scipy and not found a method for this,
but I'd be grateful for pointers if I've missed it.
In the meantime, I wrote the following function. If you can spot a
faster/better ways to achieve this, please do say:


def maxdownsample2d(data, factor):
        """Supply a 2D numpy array, and an integer factor by which to
downsample (by nonoverlapping maxpooling) in both directions"""
        # factor might not be exact fit, so we trim to this.
        trimrows = int(np.floor(data.shape[0] / float(factor))) * factor
        trimcols = int(np.floor(data.shape[1] / float(factor))) * factor
        first = True
        for coff in range(factor):
                for roff in range(factor):
                        hopped = data[roff:trimrows:factor, coff:trimcols:factor]
                        if first:
                                maxsofar = hopped
                                first = False
                        else:
                                maxsofar = np.maximum(maxsofar, hopped)
        return maxsofar


 
ndimage has a maximum filter and then slice, but it does a lot of redundant calculation
 
I always wanted to use reduceat (and didn't find many opportunities)
 
>>> ar = np.random.randn(24,24)
>>> cut = np.arange(0, 24, 4)
>>> y1 = np.maximum.reduceat(ar, cut)
>>> y2 = np.maximum.reduceat(y1, cut,axis=1)
 
>>> y2 - maxdownsample2d(ar, 4)
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])
 
Josef
 

Best
Dan
--
Dan Stowell
Postdoctoral Research Assistant
Centre for Digital Music
Queen Mary, University of London
Mile End Road, London E1 4NS
http://c4dm.eecs.qmul.ac.uk/people/dans.htm
http://www.mcld.co.uk/
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: downsampling with max in 2d

Christoph Deil

On 06 Feb 2014, at 22:14, [hidden email] wrote:



On Thu, Feb 6, 2014 at 3:54 PM, Dan Stowell <[hidden email]> wrote:
Hi all,

I would like to downsample 2D arrays using max-pooling. In other words,
to downsample by a factor of 2 would reduce

 a b c d
 e f g h
 i j k l
 m n o p

to

 max(a,b,e,f) max(c,d,g,h)
 max(i,j,m,n) max(k,l,o,p)


I've searched through numpy and scipy and not found a method for this,
but I'd be grateful for pointers if I've missed it.

Hi Dan,

scikit-image has a flexible function that let’s you easily do this:

Christoph

In the meantime, I wrote the following function. If you can spot a
faster/better ways to achieve this, please do say:


def maxdownsample2d(data, factor):
        """Supply a 2D numpy array, and an integer factor by which to
downsample (by nonoverlapping maxpooling) in both directions"""
        # factor might not be exact fit, so we trim to this.
        trimrows = int(np.floor(data.shape[0] / float(factor))) * factor
        trimcols = int(np.floor(data.shape[1] / float(factor))) * factor
        first = True
        for coff in range(factor):
                for roff in range(factor):
                        hopped = data[roff:trimrows:factor, coff:trimcols:factor]
                        if first:
                                maxsofar = hopped
                                first = False
                        else:
                                maxsofar = np.maximum(maxsofar, hopped)
        return maxsofar


 
ndimage has a maximum filter and then slice, but it does a lot of redundant calculation
 
I always wanted to use reduceat (and didn't find many opportunities)
 
>>> ar = np.random.randn(24,24)
>>> cut = np.arange(0, 24, 4)
>>> y1 = np.maximum.reduceat(ar, cut)
>>> y2 = np.maximum.reduceat(y1, cut,axis=1)
 
>>> y2 - maxdownsample2d(ar, 4)
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])
 
Josef
 

Best
Dan
--
Dan Stowell
Postdoctoral Research Assistant
Centre for Digital Music
Queen Mary, University of London
Mile End Road, London E1 4NS
http://c4dm.eecs.qmul.ac.uk/people/dans.htm
http://www.mcld.co.uk/
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: downsampling with max in 2d

Jerome Kieffer
In reply to this post by Dan Stowell-4
On Thu, 06 Feb 2014 20:54:11 +0000
Dan Stowell <[hidden email]> wrote:

> Hi all,
>
> I would like to downsample 2D arrays using max-pooling. In other words,
> to downsample by a factor of 2 would reduce
>

This is very close to binning ... Here are a couple of pure numpy implementations:

if your binning is small (up to 8x8): go for a double loop like this:
        out = numpy.zeros(tuple(outputSize))
        for i in xrange(binsize[0]):
            for j in xrange(binsize[1]):
                out = numpy.maximum(input_img[i::binsize[0], j::binsize[1]], out)

If you order is large
        temp = input_img.copy()
        temp.shape = (outputSize[0], binsize[0], outputSize[1], binsize[1])
        out = temp.max(axis=3).max(axis=1)

Anyway the size of the input array does not matter.
HTH.
--
Jérôme Kieffer
tel +33 476 882 445
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user