Другие подобные вопросы не работают для меня. Моя настройка намного проще, но я все еще получаю эту ошибку при использовании tensorflow. Я сворачиваю двумерный массив, представляющий точечный источник: массив 512 x 512 со средней точкой, установленной на 1, с массивом 256 x 256, представляющим систему обработки изображений. Свертка должна быть функцией разброса точек системы. При выполнении tf.conv2d я продолжаю получать сообщение об ошибке в заголовке. Я слежу за тем, чтобы размеры массивов соответствовали тензорному потоку. т.е. [1 512 512 1] для изображения и [1 256 256 1] для ядра
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))
Почти готово. Обратите внимание на состояние документы относительно filters
:
A Tensor. Must have the same type as input. A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels]
Вот рабочий пример:
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')