无法将图片送入feed_dict

时间:2019-06-22 13:58:11

标签: python tensorflow deep-learning

我正在尝试使用feed_dict喂神经网络,但这会产生此错误“ 不可散列的类型:'numpy.ndarray'

提要字典的输入是图像,图像是形状(宽度,高度,通道)和方向的图像列表,方向是2d数组

def batch_gen(data_dir, image_paths, steering_angles, batch_size, 
    is_training):
    index = np.random.permutation(image_paths.shape[0])
    for center, left, right in image_paths[index]:
       center = center
       left = left
       right = right
       break
    steering_angle = steering_angles[index]
    # argumentation
    if is_training and np.random.rand() < 0.6:
        image, steering_angle = augument(data_dir, center, left, right, 
         steering_angle)
    else:
        image = load_image(data_dir, center) 
            # add the image and steering angle to the batch
    images = preprocess(image)
    steers = steering_angle
    return images,steers

#

with tf.Session() as sess:

# Run the initializer sess.run(tf.global_variables_initializer()) for step in range(1, num_steps+1): # Run optimization op (backprop) images, steer = batch_gen(data_dir, X_train, y_train, 5, True) print(images.shape) sess.run(optimizer, feed_dict={images, steer})

那么不可散列的含义是什么,我该如何解决这个问题

1 个答案:

答案 0 :(得分:1)

feed_dict是一个字典(键值对)。

e.g. feed_dict={  x: images, y: steer }

x&y是必须为可哈希类型的键。在您的情况下,您直接将图像作为字典键传递,这会导致产生无法哈希的类型错误。

x&y(对于您的网络,名称可能有所不同)通常是网络中的tf.placeholder。

例如

import tensorflow as tf

x = tf.placeholder("float", None)
y = tf.placeholder("float", None)
z = x * y

with tf.Session() as session:
    result = session.run(z, feed_dict={x: [1, 2, 3], y: [2,4,6]})
    print(result)