Is it possible to extend “im2col” and “col2im” to N-D images?

孤街浪徒 提交于 2019-12-11 18:48:28

问题


"Im2col" has already been implemented, Implement MATLAB's im2col 'sliding' in Python, efficiently for 2-D images in Python. I was wondering whether it is possible to extend this to arbitrary N-D images? Many applications involve high-dimensional data (e.g. convolutions, filtering, max pooling, etc.).


回答1:


So the purpose of this question was really just to post my solution to this problem publicly. I could not seem to find such a solution on Google, so I decided to take a stab at it myself. Turns out the implementation is actually quite simple to extend from "Approach #2" in the post referenced in my question!

Efficient Implementation of N-D "im2col"

def im2col(im, win, strides = 1):
    # Dimensions
    ext_shp = tuple(np.subtract(im.shape, win) + 1)
    shp = tuple(win) + ext_shp
    strd = im.strides*2
    win_len = np.prod(win)
    try:
        len(strides)
    except:
        strides = [strides]*im.ndim
    strides = [min(i, s) for i, s in zip(im.shape, strides)]

    # Stack all possible patches as an N-D array using a strided view followed by reshaping
    col = np.lib.stride_tricks.as_strided(im, shape = shp, strides = strd).reshape(win_len, -1).reshape(-1, *ext_shp)

    # Extract patches with stride and reshape into columns
    slcs = tuple([slice(None, None, None)] + [slice(None, None, s) for s in strides])
    col = col[slcs].reshape(win_len, -1)

    return col

Efficient Implementation of N-D "col2im"

def col2im(col, im_shp, win, strides = 1):
    # Dimensions
    try:
        len(strides)
    except:
        strides = [strides]*len(im_shp)
    strides = [min(i, s) for i, s in zip(im_shp, strides)]

    # Reshape columns into image
    if col.ndim > 1:
        im = col.reshape((-1, ) + tuple(np.subtract(im_shp, win)//np.array(strides) + 1))[0]
    else:
        im = col.reshape(tuple(np.subtract(im_shp, win)//np.array(strides) + 1))

    return im

Verification That It Works

Let's define an arbitrary 3-D input:

x = np.arange(216).reshape(6, 6, 6)
print(x)

[[[  0   1   2   3   4   5]
  [  6   7   8   9  10  11]
  [ 12  13  14  15  16  17]
  [ 18  19  20  21  22  23]
  [ 24  25  26  27  28  29]
  [ 30  31  32  33  34  35]]

 [[ 36  37  38  39  40  41]
  [ 42  43  44  45  46  47]
  [ 48  49  50  51  52  53]
  [ 54  55  56  57  58  59]
  [ 60  61  62  63  64  65]
  [ 66  67  68  69  70  71]]

 [[ 72  73  74  75  76  77]
  [ 78  79  80  81  82  83]
  [ 84  85  86  87  88  89]
  [ 90  91  92  93  94  95]
  [ 96  97  98  99 100 101]
  [102 103 104 105 106 107]]

 [[108 109 110 111 112 113]
  [114 115 116 117 118 119]
  [120 121 122 123 124 125]
  [126 127 128 129 130 131]
  [132 133 134 135 136 137]
  [138 139 140 141 142 143]]

 [[144 145 146 147 148 149]
  [150 151 152 153 154 155]
  [156 157 158 159 160 161]
  [162 163 164 165 166 167]
  [168 169 170 171 172 173]
  [174 175 176 177 178 179]]

 [[180 181 182 183 184 185]
  [186 187 188 189 190 191]
  [192 193 194 195 196 197]
  [198 199 200 201 202 203]
  [204 205 206 207 208 209]
  [210 211 212 213 214 215]]]

Let's extract all the patches with a non-uniform window and equal stride:

y = im2col(x, [1, 3, 2], strides = [1, 3, 2])
print(y.T) # transposed for ease of visualization

[[  0   1   6   7  12  13]
 [  2   3   8   9  14  15]
 [  4   5  10  11  16  17]
 [ 18  19  24  25  30  31]
 [ 20  21  26  27  32  33]
 [ 22  23  28  29  34  35]
 [ 36  37  42  43  48  49]
 [ 38  39  44  45  50  51]
 [ 40  41  46  47  52  53]
 [ 54  55  60  61  66  67]
 [ 56  57  62  63  68  69]
 [ 58  59  64  65  70  71]
 [ 72  73  78  79  84  85]
 [ 74  75  80  81  86  87]
 [ 76  77  82  83  88  89]
 [ 90  91  96  97 102 103]
 [ 92  93  98  99 104 105]
 [ 94  95 100 101 106 107]
 [108 109 114 115 120 121]
 [110 111 116 117 122 123]
 [112 113 118 119 124 125]
 [126 127 132 133 138 139]
 [128 129 134 135 140 141]
 [130 131 136 137 142 143]
 [144 145 150 151 156 157]
 [146 147 152 153 158 159]
 [148 149 154 155 160 161]
 [162 163 168 169 174 175]
 [164 165 170 171 176 177]
 [166 167 172 173 178 179]
 [180 181 186 187 192 193]
 [182 183 188 189 194 195]
 [184 185 190 191 196 197]
 [198 199 204 205 210 211]
 [200 201 206 207 212 213]
 [202 203 208 209 214 215]]

Let's convert this back to a (downsampled) image:

z = col2im(y, x.shape, [1, 3, 2], strides = [1, 3, 2])
print(z)

[[[  0   2   4]
  [ 18  20  22]]

 [[ 36  38  40]
  [ 54  56  58]]

 [[ 72  74  76]
  [ 90  92  94]]

 [[108 110 112]
  [126 128 130]]

 [[144 146 148]
  [162 164 166]]

 [[180 182 184]
  [198 200 202]]]

As you can see, the final output is indeed the downsampled image that we expect (you can easily check this by going value by value). The dimensionality and strides I chose were purely illustrative. There's no reason why the window size has to be the same as your stride or that you can't go higher than 3 dimensions.

Applications

If you want to use this practically, all you have to do is intercept the output of im2col before turning it back into an image. For example, if you want to do pooling, you could take the mean or the maximum across the 0th axis. If you want to do a convolution, you just need to multiply this by your flattened convolutional filter.

There may be more efficient alternatives to this already implemented under the hood of Tensorflow, etc. that are faster than "im2col." This is not meant to be the MOST efficient implementation. And of course, you could possibly optimize my code further by eliminating the intermediate reshaping step in "im2col," but it wasn't immediately obvious to me so I just left it at that. If you have a better solution, let me know. Anyways, hope this helps someone else looking for the same answer!



来源:https://stackoverflow.com/questions/58126125/is-it-possible-to-extend-im2col-and-col2im-to-n-d-images

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