使用占位符但出现错误“NoneType”对象没有属性“type”

2024-05-15 21:53:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在梳理两个不同的图像时获得新的图像,以进行风格转换。我有两个图像(一个是目标,另一个是样式)。我想从他们两个那里得到一个新的

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))

当我想在此功能中使用组合图像时:

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)

我正在处理一个错误,“非类型”对象没有属性“类型”

我尝试更改组合图像(组合图像=预处理图像(新图像路径)),但出现新错误:

---->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'

Tags: path图像imageselfnonetargetimgvalue