【问题标题】:Error: All inputs to the layer should be tensors- when trying to use VGG19 model错误:当尝试使用 VGG19 模型时,该层的所有输入都应该是张量
【发布时间】:2022-01-09 03:11:02
【问题描述】:

在尝试导入 VGG19 模型时,以下代码会生成非张量输入的错误。虽然我正在关注另一个这个代码 sn-p here.

代码:

from keras.applications.vgg19 import VGG19
import keras.backend as K
from keras.models import Model
import imageio as iio

image_shape = (384,384,3)
vgg19 = VGG19(include_top=False, weights='imagenet', input_shape=image_shape)
vgg19.trainable = False
# Make trainable as False
for l in vgg19.layers:
    l.trainable = False
model = Model(inputs=vgg19.input, outputs=vgg19.get_layer('block5_conv4').output)
model.trainable = False

img1 = iio.imread('img1.jpg')
img2 = iio.imread('img2.jpg')

mean = K.mean(K.square(model(img1) - model(img2)))

错误:

...,
         [164,  90,   0, 255],
         [164,  90,   0, 255],
         [164,  90,   0, 255]]]], dtype=uint8)]. All inputs to the layer should be tensors.

不知道为什么。

【问题讨论】:

    标签: python tensorflow keras deep-learning vgg-net


    【解决方案1】:

    也许尝试将您的图像转换为张量:

    import numpy
    from PIL import Image
    from keras.applications.vgg19 import VGG19
    import keras.backend as K
    from keras.models import Model
    import imageio as iio
    
    # Create random images
    for n in range(2):
        a = numpy.random.rand(384,384,3) * 255
        im = Image.fromarray(a.astype('uint8')).convert('RGB')
        im.save('test%0d.jpg' % n)
    
    image_shape = (384,384,3)
    vgg19 = VGG19(include_top=False, weights='imagenet', input_shape=image_shape)
    vgg19.trainable = False
    # Make trainable as False
    for l in vgg19.layers:
        l.trainable = False
    model = Model(inputs=vgg19.input, outputs=vgg19.get_layer('block5_conv4').output)
    model.trainable = False
    
    img1 = iio.imread('test0.jpg')
    img2 = iio.imread('test1.jpg')
    img1 = tf.expand_dims(tf.constant(img1), axis=0)
    img2 = tf.expand_dims(tf.constant(img2), axis=0)
    mean = K.mean(K.square(model(img1) - model(img2)))
    print(mean)
    
    tf.Tensor(5.283036, shape=(), dtype=float32)
    

    除了tf.expand_dims,您也可以这样做:

    img1 = tf.constant([img1])
    img2 = tf.constant([img2])
    

    还有一个选项可以使用tf.keras.preprocessing.image.load_img 加载图像:

    img1 = tf.keras.preprocessing.image.load_img('test0.jpg')
    img2 = tf.keras.preprocessing.image.load_img('test1.jpg')
    img1 = tf.constant([tf.keras.preprocessing.image.img_to_array(img1)])
    img2 = tf.constant([tf.keras.preprocessing.image.img_to_array(img2)])
    mean = K.mean(K.square(model(img1) - model(img2)))
    print(mean)
    

    【讨论】:

    • 谢谢。我得到错误的答案,我不知道为什么。首先它产生错误TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64, 所以我将代码更改为img1 = tf.expand_dims(tf.constant(img1.astype('float32')), axis=0) img2 = tf.expand_dims(tf.constant(img2.astype('float32')), axis=0). 现在我得到的结果是Tensor("Mean:0", shape=(), dtype=float32)
    • 虽然来自 page 的代码工作正常。我在下面的答案中更改了代码
    • @Safi 您的代码运行良好...您只是没有看到正在打印的值...您是否开启了 Eager 模式?
    • 你是对的。我急切的执行被关闭了。 tf.compat.v1.enable_eager_execution(config=None, device_policy=None, execution_mode=None) 完成了这项工作。谢谢
    【解决方案2】:

    来自page 的代码工作正常。 我稍微改了一下代码。

    image_shape = (384,384,3)
    base_model = VGG19(include_top=False, weights='imagenet', input_shape=image_shape)
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('block5_conv4').output)
    
    img01 = iio.imread('test0.jpg').astype('float32')
    img11 = iio.imread('test1.jpg').astype('float32')
    
    imgx1 = normalize(img01)
    imgx2 = normalize(img11)
    img1 = np.expand_dims(imgx1, axis=0)
    img2 = np.expand_dims(imgx2, axis=0)
    mean = np.mean((model.predict(img1) - model.predict(img2))**2)
    print(mean)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2020-03-08
      相关资源
      最近更新 更多