Skip to content Skip to sidebar Skip to footer

As_strided: Linking Stepsize (strides Of Conv2d) With As_strided Strides Parameter

I found that for generating (X - x + 1, Y - y + 1) patches of size (x,y) from (X,Y) with stride 1, image requires us to give strides parameter as img.strides * 2 or img.strides + i

Solution 1:

Here's one approach -

def patchify(img, patch_shape, stepsize_x=1, stepsize_y=1): 
    strided = np.lib.stride_tricks.as_strided
    x, y = patch_shape    
    p,q = img.shape[-2:]    
    sp,sq = img.strides[-2:]

    out_shp = img.shape[:-2] + (p-x+1,q-y+1,x,y)
    out_stride = img.strides[:-2] + (sp,sq,sp,sq)

    imgs = strided(img, shape=out_shp, strides=out_stride)
    return imgs[...,::stepsize_x,::stepsize_y,:,:]

Sample runs -

1] Input :

In [156]: np.random.seed(0)

In [157]: img = np.random.randint(11,99,(2,4,4))

In [158]: img
Out[158]: 
array([[[55, 58, 75, 78],
        [78, 20, 94, 32],
        [47, 98, 81, 23],
        [69, 76, 50, 98]],

       [[57, 92, 48, 36],
        [88, 83, 20, 31],
        [91, 80, 90, 58],
        [75, 93, 60, 40]]])

2] Output - Case #1 :

In [159]: patchify(img, (2,2), stepsize_x=1, stepsize_y=1)[0]
Out[159]: 
array([[[[55, 58],
         [78, 20]],

        [[58, 75],
         [20, 94]],

        [[75, 78],
         [94, 32]]],


       [[[78, 20],
         [47, 98]],

        [[20, 94],
         [98, 81]],

        [[94, 32],
         [81, 23]]],


       [[[47, 98],
         [69, 76]],

        [[98, 81],
         [76, 50]],

        [[81, 23],
         [50, 98]]]])

3] Output - Case #2 :

In [160]: patchify(img, (2,2), stepsize_x=2, stepsize_y=1)[0]
Out[160]: 
array([[[[55, 58],
         [78, 20]],

        [[58, 75],
         [20, 94]],

        [[75, 78],
         [94, 32]]],


       [[[47, 98],
         [69, 76]],

        [[98, 81],
         [76, 50]],

        [[81, 23],
         [50, 98]]]])

4] Output - Case #3 :

In [161]: patchify(img, (2,2), stepsize_x=2, stepsize_y=2)[0]
Out[161]: 
array([[[[55, 58],
         [78, 20]],

        [[75, 78],
         [94, 32]]],


       [[[47, 98],
         [69, 76]],

        [[81, 23],
         [50, 98]]]])

Post a Comment for "As_strided: Linking Stepsize (strides Of Conv2d) With As_strided Strides Parameter"