0
shape(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)")Iが判明デバッグを
そのmodel.extract_featuresの形状()[5]は(?、?、?、128)なので、除算はできません。
のサイズはです((1,192,242,3))ので、セッションH、Wを実行するとCが生成されます。
この問題を解決する方法を教えてください。
シェイプを整数のテンソルとして取得するには、 'tf.shape'を使用します(' int() 'キャストを削除する必要もあります)。これは、グラフの構築中に形状が不明な場合でも機能します(これは 'tensor.shape'があなたに与える情報です)。 –
ありがとうございました@AllenLavoie、それは働いた! :) – BimalGrewal