[英]Tensorflow how do I reshape array of images properly for model prediction input
[英]How to properly get the shape in Tensorflow so that I can reshape again?
我正在尝试执行以下代码
def f(x):
(_, H, W, C) = tf.shape(x)
x_reshaped = tf.reshape(x, (-1,C))
res = x_reshaped/(H*W*C)
return res
但是,这里的问题显然是我不了解H,W,所以它们是?,?。 因此,重塑和乘法不起作用。 现在我的问题是,如何正确地进行上述计算,以便res
是可以在会话中稍后计算的正确的张量流节点?
以下应该工作:
X = tf.placeholder(tf.float32, shape=[None, None, None, 40])
def f(x):
s = tf.shape(x)
x_reshaped = tf.reshape(x, [-1,s[3]])
res = tf.div(x_reshaped, tf.cast((s[0]*s[1]*s[2]), tf.float32))
return res
out = f(X)
sess = tf.Session()
sess.run(out, {X:np.random.normal(size=(10,20,30,40))})
我假设您希望x
的形状为(batch_size, H*W*C)
,这意味着x
中的每个项目都是“展平”的图像数据。 在这种情况下,正确的代码将是:
x_reshaped = tf.reshape(x, (-1, H*W*C))
但是,如果我看不到更多代码,则无法确定。 例如,如果您的神经网络被设计为卷积,那么完全重塑是错误的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.