gputools icon indicating copy to clipboard operation
gputools copied to clipboard

Convolve without preserving the size ("valid" convolution)

Open jaggiK opened this issue 2 years ago • 1 comments

Hi,

Thanks for building this great package.

Convolve seems to produce same output size as input size commonly referred as "same" convolution. Is there a way to perform convolution "valid" convolution where output size is shrinked?

jaggiK avatar May 17 '23 21:05 jaggiK

hi,

convolve does not support "valid" convolution out of the box, but it should be easy to define it yourself:


import numpy as np
from gputools.convolve import convolve

def convolve_valid(x,h, **kwargs):
    y = convolve(x, h, **kwargs)
    # extract valid region
    valid_slices = tuple(slice(s//2, -(s-s//2) if s>1 else None) for s in h.shape)
    return y[valid_slices]

x = np.random.rand(128,128)
h = np.random.rand(3,11)

res = convolve_validx,h)

Hope that helps!

maweigert avatar May 18 '23 07:05 maweigert