我正在使用具有原始权重的StyleGANv2(https://github.com/moono/stylegan2-tf-2.x)。 我的TF版本是2.2.0。默认的训练过程和推理工作正常。
但是我正在尝试进行有关输入的学习步骤:
rnd = np.random.RandomState(seed)
latents = rnd.randn(1, generator.z_dim).astype(np.float32)
labels = rnd.randn(1, generator.labels_dim).astype(np.float32)
initial_input = [tf.convert_to_tensor(latents), tf.convert_to_tensor(labels)]
input_tensor = tf.Variable([initial_input[0]])
for step in range(10):
with tf.GradientTape() as tape:
tape.watch(initial_input[0])
if step == 0:
generated_image = generator(initial_input)
else:
generated_image = generator([input_tensor[0,:,:], initial_input[1]])
# target_image - the image I'm trying to learn latents for
loss_value = mae_loss(generated_image, target_image)
plt.imshow(postprocess_images(generated_image).numpy()[0])
plt.show()
print("Loss: ", loss_value)
print("Tensor: ", input_tensor)
print("Initial Input: ", [initial_input[0]])
gradients = tape.gradient(loss_value, [initial_input[0]])
print("Gradients: ", gradients)
optimizer.apply_gradients(zip([gradients], [input_tensor]))
第一步成功完成,但是在第二步,我得到一个错误:
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
第二步,gradients = tape.gradient(loss_value, [initial_input[0]])
返回None
。
那么你知道可能是什么问题吗?
答案 0 :(得分:1)
现在可以使用。感谢@klapeyron。我用input_tensor中的值更新了tf.Variable值:
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
initial_input = [tf.convert_to_tensor(latents), tf.convert_to_tensor(labels)]
input_tensor = tf.Variable([initial_input[0]])
for step in range(100):
with tf.GradientTape() as tape:
tape.watch(initial_input[0])
initial_input[0] = input_tensor[0,:,:]
generated_image = generator(initial_input)initial_input[1]])
loss_value = mse_loss(generated_image, target_image)
plt.imshow(postprocess_images(generated_image).numpy()[0])
plt.show()
gradients = tape.gradient(loss_value, [initial_input[0]])
optimizer.apply_gradients(zip([gradients], [input_tensor]))