Python: how to optimize convolution in Python?

Viewed 27

Let's suppose to have a matrix A of 0 and 1 like the following

import numpy as np
A = np.random.randint(0,2,size = (1867,2066))

I would like to make a convolution of the array A with a kernel like the following

k_radius =  np.ones((n,n))

with n = 200 it takes an infinite time.

In the figure below I show the computational time by increasing the value of n with n < 100.

I use scipy.signal.convolve2d

import time
from scipy.signal import convolve2d
n = np.arange(1,100, 10)
T = []
for i in n:
    start = time.time()
    k_radius =  np.ones((i,i))
    C = convolve2d(A, k_radius,  mode='same', boundary='fill', fillvalue=0)
    end = time.time()
    T.append(end-start)

enter image description here

Is there a way to speed up the process with multiprocessing or others?

0 Answers
Related