0

这是我尝试过的代码,但它给了我 Input 0 is incompatible with layer model_21: expected shape=(None, 224, 224, 3), found shape=(1, 256, 256, 3)

 def loss_object(style_outputs, content_outputs, style_target, content_target):
          style_weight = 1e-2
          content_weight = 1e-1
          content_loss = tf.reduce_mean((content_outputs - content_target)**2)
          style_loss = tf.add_n([tf.reduce_mean((output_ - target_)**2) for output_, target_ in zip(style_outputs, style_target)])
          total_loss = content_weight*content_loss + style_weight*style_loss
          return total_loss
        vgg_model = load_vgg()
        content_target = vgg_model(np.array([content_image*255]))[0]
        style_target = vgg_model(np.array([style_image*255]))[1]
4

1 回答 1

0

根据错误,您使用的预训练模型期望输入为形状(无、224、224、3),但您输入的是形状(1、256、256、3)。您必须将输入调整为 (224,224) 并将其重新调整为 (None,224,224,3)。要重塑图像,请使用以下类似的代码:

image=image.reshape(None,224,224,3)

让我们知道问题是否仍然存在。谢谢!

于 2022-02-24T14:53:10.803 回答