如何使用优化器对图像施加损失
How do you apply a loss using an optimizer on a image
我正在尝试实现神经艺术迁移。所以我的问题是现在我不知道如何将它应用到图像上。下面我创建了一个优化器、一个损失函数和一个随机生成的图像。如何对此应用自定义优化器?每当我 运行 这是 'tensorflow.python.framework.ops.EagerTensor' object is not callable
并且我对这条消息有点困惑。任何帮助将不胜感激。
optimiser=tf.keras.optimizers.Adam(learning_rate=0.05)
#All [content_image,style_image,generated_image] the same Shape 1, H ,W ,C uint8
epochs=20
# pass content image through model
content_image_value=model(content_image.reshape(size_of_images))[-1]
# pass styles image through model
style_image_values=model(style_image.reshape(size_of_images))[:-1]
# Randomly Generated Image
generated_image=tf.Variable(generated_image)
for epoch in range(epochs):
# pass model generated image
generate_image_values=model(generated_image)
# Get the output we compare with the content
content_generator_value=generate_image_values[-1]
# Get the output we compare with the style
style_generator_value=generate_image_values[:-1]
loss=some_custom_loss() # returns a float64
print(loss)
# Minimise the error
optimiser.minimize(loss,var_list=[generated_image])
imshow(generated_image[0])
问题出在 minimize
函数上。此函数旨在在 tensorflow session
.
内运行
您需要按如下方式修改您的代码,以最大限度地减少急切模式下的损失:
with tf.GradientTape() as tape:
generate_image_values = model(generated_image)
# Get the output we compare with the content
content_generator_value = generate_image_values[-1]
# Get the output we compare with the style
style_generator_value = generate_image_values[:-1]
loss = some_custom_loss() # returns a float64
print(loss)
# Get the gradients
grads = tape.gradient(loss, generated_image)
# minimize
opt.apply_gradients(zip(grads, generated_image)
imshow(generated_image[0])
我正在尝试实现神经艺术迁移。所以我的问题是现在我不知道如何将它应用到图像上。下面我创建了一个优化器、一个损失函数和一个随机生成的图像。如何对此应用自定义优化器?每当我 运行 这是 'tensorflow.python.framework.ops.EagerTensor' object is not callable
并且我对这条消息有点困惑。任何帮助将不胜感激。
optimiser=tf.keras.optimizers.Adam(learning_rate=0.05)
#All [content_image,style_image,generated_image] the same Shape 1, H ,W ,C uint8
epochs=20
# pass content image through model
content_image_value=model(content_image.reshape(size_of_images))[-1]
# pass styles image through model
style_image_values=model(style_image.reshape(size_of_images))[:-1]
# Randomly Generated Image
generated_image=tf.Variable(generated_image)
for epoch in range(epochs):
# pass model generated image
generate_image_values=model(generated_image)
# Get the output we compare with the content
content_generator_value=generate_image_values[-1]
# Get the output we compare with the style
style_generator_value=generate_image_values[:-1]
loss=some_custom_loss() # returns a float64
print(loss)
# Minimise the error
optimiser.minimize(loss,var_list=[generated_image])
imshow(generated_image[0])
问题出在 minimize
函数上。此函数旨在在 tensorflow session
.
您需要按如下方式修改您的代码,以最大限度地减少急切模式下的损失:
with tf.GradientTape() as tape:
generate_image_values = model(generated_image)
# Get the output we compare with the content
content_generator_value = generate_image_values[-1]
# Get the output we compare with the style
style_generator_value = generate_image_values[:-1]
loss = some_custom_loss() # returns a float64
print(loss)
# Get the gradients
grads = tape.gradient(loss, generated_image)
# minimize
opt.apply_gradients(zip(grads, generated_image)
imshow(generated_image[0])