# How could I convolve a 4D image and a 4D filter with stride?

I want to create a CNN in Python, specifically, only with NumPy, if possible. For optimizing the time of convolution (actually correlation) in the network, I wanna try to use FFT-based convolution. The data that needs to be convoluted (correlated) is a 4D image tensor with shape [batch_size, width, height, channels] and 4D filter tensor [filter_width, filter_height, in_channel, out_channel]. I read a lot of articles about FFT-based convolution, but they aren't doing it in my way. Thus, I need your help.

How could I fft-convolve a 4D image and a 4D filter with stride?

• Hello. It's not clear what your question is. Please, explicitly ask a specific question about your problem. – nbro Sep 10 '20 at 21:23
• Hi, @nbro, I did it. – TheFnafException Sep 10 '20 at 21:26
• Ok, thanks for clarifying that. If you're looking for code, this site is not appropriate to ask your question (i.e. programming questions are generally off-topic here), so your question would be off-topic. You can read more about what is on-topic here: ai.stackexchange.com/help/on-topic, and I suggest that you read it. Are you asking for code? If yes, then probably Stack Overflow is the best place to ask your question. If you want, I can migrate your question to Stack Overflow. – nbro Sep 10 '20 at 21:27
• Actually, No. I want to understand how to FFT-Convolve two 4D tensors with clarifying. – TheFnafException Sep 10 '20 at 21:29
• Thanks, @nbro again). You're really good moderator – TheFnafException Sep 10 '20 at 21:33

## 2 Answers

I think this should work for you: scipy.signal.correlate | SciPy

I used it myself while I was writing a CNN in numpy.

• Thanks, but how to correlate with stride? – TheFnafException Sep 10 '20 at 21:24
• Mm, it doesn't seem optimal, but I don't know a better way that just to correlate and take elements with the corresponding step. – Michael Solotky Sep 10 '20 at 21:29
• Anyway, thanks for good answer. I'll use it in my code. – TheFnafException Sep 10 '20 at 21:30
• Given that the OP apparently wants to implement the correlation/convolution from scratch, then maybe you should spend some words to explain how that can be conceptually done. By the way, welcome to AI SE! Have a look at our on-topic page ai.stackexchange.com/help/on-topic to know more about the type of question we are looking for. – nbro Sep 10 '20 at 21:32

I think what you need to use is 3D convolution operation. Your data is 3D, width, height, and num_channels. Your data is similar to color images with RGB channels. However, since you are trying to consider the correlation amongst channels 2D convolution will not work for you. You can use 3D convolution which is available to use with deep learning tools such as Tensorflow.

• P.S. My data is 4D: [ batch_size, width, height, and num_channels ]. – TheFnafException Sep 16 '20 at 11:13
• Yes, my images are RGB – TheFnafException Sep 16 '20 at 11:13
• What is the 4 dimensions in your data, i only see 3, batch_size is not a dimension of data, isnt it the batch size of the input of the neural network or is it somthing else. – gihan Sep 16 '20 at 14:12