我试图在组合两个不同的图像时获得新图像,以进行风格转移。我有两张图片(一张是目标,另一张是样式)。我想从他们两个那里得到一个新的。
target_image = K.constant(preprocess_image(target_image_path))
style_reference_image = K.constant(preprocess_image(style_image_path))
combination_image = K.placeholder((1, img_height, img_width, 3))
当我想在这个函数中使用combinatin_image时:
grads = K.gradients(loss, combination_image)[0]
fetch_loss_and_grads = K.function([combination_image], [loss, grads])
class Evaluator(object):
def __init__(self):
self.loss_value = None
self.grads_values = None
def loss(self, x):
assert self.loss_value is None
x = x.reshape((1, img_height, img_width, 3))
outs = fetch_loss_and_grads([x])
loss_value = outs[0]
grad_values = outs[1].flatten().astype('float64')
self.loss_value = loss_value
self.grad_values = grad_values
return self.loss_value
def grads(self, x):
assert self.loss_value is not None
grad_values = np.copy(self.grad_values)
self.loss_value = None
self.grad_values = None
return grad_values
evaluator = Evaluator()
x = preprocess_image(target_image_path)
x = x.flatten()
for i in range(iterations):
x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x,
fprime=evaluator.grads, maxfun=20)
我正在处理一个错误'NoneType' object has no attribute 'type'。
我尝试更改combinatin_image (combination_image = preprocess_image(new_image_path)) 但我有一个新错误:
---->5 fetch_loss_and_grads = K.function([combination_image], [loss, grads])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __init__(self, inputs, outputs, updates, name)
3681 'of elements from multiple graphs.')
3682
-> 3683 source_graph = graphs.pop()
3684 global_graph = get_graph()
3685
KeyError: 'pop from an empty set'