我正在编写Python代码,以可视化ConvNet层的输出。一切似乎都还不错,但是在sess.run函数调用中出现如下所示的尺寸转换错误。
ValueError:无法为形状为((,, 128,128,3)'的张量'image-in:0'输入形状(128,128,3)的值
loaded_graph = tf.Graph()
image_to_use = train_images[0]
print(image_to_use.shape) # (128, 128, 3)
with tf.Session(graph=loaded_graph) as sess:
# Load model
loader = tf.train.import_meta_graph(save_model_path + ".meta")
loader.restore(sess, save_model_path)
# Get Tensors from loaded model
hidden_layer_1 = loaded_graph.get_tensor_by_name("hidden-layer-1:0")
keep_prob_tf = tf.placeholder(tf.float32, name="keep-prob-in")
image_in_tf = tf.placeholder(tf.float32, [None, image_to_use.shape[0], image_to_use.shape[1], image_to_use.shape[2]], name="image-in")
units = sess.run(hidden_layer_1, feed_dict={image_in_tf:image_to_use, keep_prob_tf:1.0})
ValueError:无法为形状为((,, 128,128,3)'的张量'image-in:0'输入形状(128,128,3)的值
答案 0 :(得分:0)
此行有问题
image_to_use = train_images[0]
其中image_to_use
的维度为(128, 128, 3)
。
将其更改为此:
image_to_use = np.asarray([train_images[0]])
现在image_to_use
的形状为(1, 128, 128, 3)
,它将与预期的形状(?, 128, 128, 3)
兼容。
还可以这样定义image_in_tf
占位符的形状:
image_in_tf = tf.placeholder(tf.float32, (None, ) + image_to_use[0].shape)
否则,它将接受当前代码格式的错误尺寸(我们更改了第一个尺寸)。