0

im2col has already been efficiently implemented in python with code like:

def im2col_sliding_strided(A, BSZ, stepsize=1):
    # Parameters
    m,n = A.shape
    s0, s1 = A.strides
    nrows = m-BSZ[0]+1
    ncols = n-BSZ[1]+1
    shp = BSZ[0],BSZ[1],nrows,ncols
    strd = s0,s1,s0,s1

    out_view = np.lib.stride_tricks.as_strided(A, shape=shp, strides=strd)
    return out_view.reshape(BSZ[0]*BSZ[1],-1)[:,::stepsize]

which is comes from the Implement MATLAB's im2col 'sliding' in Python

But it doesn't have solution for the column to image back. Is there any method to speed it up?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    If the result is a `view`, there's no need to convert back. Any in-place changes to the view will appear in A`. – hpaulj Aug 05 '20 at 21:57
  • @prune, I don't see this as off-topic. It's based on an earlier SO, and a common speedup 'trick', `as_strided` – hpaulj Aug 05 '20 at 22:02
  • @hpaulj I've read things over twice, thanks to your note. It's now borderline in my mind -- which is plenty of doubt about my original position. I'm withdrawing my objections. Thanks. – Prune Aug 05 '20 at 22:07
  • I just tested this. The `as_strided` does create a view, but the final `reshape` has to make a copy. So the result is not a `view`. – hpaulj Aug 05 '20 at 23:18
  • I was thinking of a simpler `as_strided` case where @Divakar pointed out that it's just a view: [inverse function of numpy as strided](https://stackoverflow.com/questions/62447526/inverse-function-of-numpy-as-strided) – hpaulj Aug 05 '20 at 23:21

0 Answers0