The other similar questions don't work for me. My setup is much simpler but I still get this error when using tensorflow. I am convolving a 2d array representing a point source: a 512 x 512 array with the middle point set to 1, with a 256x256 array representing an imaging system. The convolution should be the point spread function of the system. When doing the tf.conv2d, i keep getting the error in the title. I make sure that the sizes of the arrays are consistent with tensorflow. ie, [1 512 512 1] for the image and [1 256 256 1] for the kernel
def convolve(arr, kernel):
#arr: 512 x 512 2d array
#kernel: 256 x 256 2d array
# make arr 4d
f = tf.cast(tf.reshape(arr, [1, arr.shape[0], arr.shape[1], 1]), tf.float32)
# make kernel 4d
h = tf.cast(tf.reshape(kernel, [1, kernel.shape[0], kernel.shape[1], 1]), tf.float32)
return tf.nn.conv2d(f, h, strides=[1, 1, 1, 1], padding="VALID")
point_source = np.zeros((512,512))
point_source[int(512/2):int(512/2)] = 1
plt.imshow(convolve(point_source, mask_array))
CodePudding user response:
Almost there. Note what the docs state regarding the filters
:
A Tensor. Must have the same type as input. A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels]
Here is a working example:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
def convolve(arr, kernel):
#arr: 512 x 512 2d array
#kernel: 256 x 256 2d array
# make arr 4d
f = tf.cast(tf.reshape(arr, [1, arr.shape[0], arr.shape[1], 1]), tf.float32)
# make kernel 4d
h = tf.cast(tf.reshape(kernel, [kernel.shape[0], kernel.shape[1], 1, 1]), tf.float32)
return tf.nn.conv2d(f, h, strides=[1, 1, 1, 1], padding="VALID")
point_source = np.zeros((512,512))
point_source[int(512/2):int(512/2)] = 1
mask_array = np.ones((256, 256))
plt.imshow(convolve(point_source, mask_array)[0, :, :, 0],cmap='gray')