0
我写了一个计算形状(1,H,W,C)图像特征的克矩阵的函数。方法我写的是下面:在Tensorflow中处理未知尺寸
def calc_gram_matrix(features, normalize=True):
#input: features is a tensor of shape (1, Height, Width, Channels)
_, H, W, C = features.shape
matrix = tf.reshape(features, shape=[-1, int(C)])
gram = tf.matmul(tf.transpose(matrix), matrix)
if normalize:
tot_neurons = H * W * C
gram = tf.divide(gram,tot_neurons)
return gram
来测试我的落实克矩阵的,有一种方法:
def gram_matrix_test(correct):
gram = calc_gram_matrix(model.extract_features()[5]) #
student_output = sess.run(gram, {model.image: style_img_test})
print(style_img_test.shape)
error = rel_error(correct, student_output)
print('Maximum error is {:.3f}'.format(error))
gram_matrix_test(answers['gm_out'])
当我运行gram_matrix_test()我得到一个错误 - > ValueError异常:无法转换张量的未知维度:?
(该错误是在这条线 - > “克= tf.divide(克,tot_neurons)”)
在调试我发现的model.extract_features形状()[5]是(?,?,?,128),因此划分是不可能的。的style_img_test
尺寸为((1,192,242,3)),所以当我们运行会话H,W,C将得到填充。
你能指导我如何解决这个问题吗?
使用'tf.shape'获得一个整形张量的形状(你还需要删除你的'int()'cast)。即使图形构造过程中形状未知(这是'tensor.shape'给出的信息),这也是有效的。 –
谢谢@AllenLavoie,它工作! :) – BimalGrewal