as_strided: Linking stepsize (strides of conv2d) with as_strided strides parameter

烈酒焚心 提交于 2019-12-24 08:59:10

问题


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 + img.strides. I don't know how they quickly compute this knowing the no. of strides in conv2d

But what should I do to get ((X-x)/stride)+1, ((Y-y)/stride)+1 patches of same size from same sized image with stride stride?


From this SO answer with slight modification, with channels and number of images placed in front

def patchify(img, patch_shape):
    a,b,X, Y = img.shape                # a images and b channels
    x, y = patch_shape
    shape = (a, b, X - x + 1, Y - y + 1, x, y)
    a_str, b_str, X_str, Y_str = img.strides
    strides = (a_str, b_str, X_str, Y_str, X_str, Y_str)
    return np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)

I can see that it creates a sliding window with size (x,y) and stride 1 (move 1 pixel to the right and move 1 pixel down). I have trouble correlating the strides parameter which as_strided uses and the strides we usually use for conv2d.

How do I add a parameter to the above function that computes as_strided strides parameter?

def patchify(img, patch_shape, stride):    # stride=stepsize in conv2d eg: 1,2,3,...
    a,b,X,Y = img.shape                    # a images and b channels
    x, y = patch_shape
    shape = (a,b,((X-x)/stride)+1, ((Y-y)/stride)+1, x, y)
    strides = ???                          # strides for as_strided
    return np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)

img is 4d (a, b, X, Y)

  • a=no.of images,
  • b=no.of channels,
  • (X,Y) = width and height

Note: By stride in conv2d I mean stepsize Unfortunately this is also called stride.

Note 2: Since stepsize will usually be the same on both axes, in the code I provided, I've provided only one parameter, however used it for both dimensions.

Playground: What goes in for strides here. I have it running for stepsize=1 here. I noticed that it might not work from the link but it works when pasted in new playground.

This should give a clear idea of what I need:

[[ 0.5488135   0.71518937  0.60276338  0.54488318]
 [ 0.4236548   0.64589411  0.43758721  0.891773  ]
 [ 0.96366276  0.38344152  0.79172504  0.52889492]
 [ 0.56804456  0.92559664  0.07103606  0.0871293 ]]

# patch_size = 2x2
# stride = 1,1

[[[[ 0.5488135   0.71518937]
   [ 0.4236548   0.64589411]]

  [[ 0.71518937  0.60276338]
   [ 0.64589411  0.43758721]]

  [[ 0.60276338  0.54488318]
   [ 0.43758721  0.891773  ]]]


 [[[ 0.4236548   0.64589411]
   [ 0.96366276  0.38344152]]

  [[ 0.64589411  0.43758721]
   [ 0.38344152  0.79172504]]

  [[ 0.43758721  0.891773  ]
   [ 0.79172504  0.52889492]]]


 [[[ 0.96366276  0.38344152]
   [ 0.56804456  0.92559664]]

  [[ 0.38344152  0.79172504]
   [ 0.92559664  0.07103606]]

  [[ 0.79172504  0.52889492]
   [ 0.07103606  0.0871293 ]]]]

# stride = 2,2

[[[[[[ 0.5488135   0.71518937]
     [ 0.4236548   0.64589411]]

    [[ 0.60276338  0.54488318]
     [ 0.43758721  0.891773  ]]]


   [[[ 0.96366276  0.38344152]
     [ 0.56804456  0.92559664]]

    [[ 0.79172504  0.52889492]
     [ 0.07103606  0.0871293 ]]]]]]

# stride = 2,1

[[[[ 0.5488135   0.71518937]
   [ 0.4236548   0.64589411]]

  [[ 0.71518937  0.60276338]
   [ 0.64589411  0.43758721]]

  [[ 0.60276338  0.54488318]
   [ 0.43758721  0.891773  ]]]

 [[[ 0.96366276  0.38344152]
   [ 0.56804456  0.92559664]]

  [[ 0.38344152  0.79172504]
   [ 0.92559664  0.07103606]]

  [[ 0.79172504  0.52889492]
   [ 0.07103606  0.0871293 ]]]]

回答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]]]])


来源:https://stackoverflow.com/questions/47469947/as-strided-linking-stepsize-strides-of-conv2d-with-as-strided-strides-paramet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!