Home > Net >  Error: All inputs to the layer should be tensors- when trying to use VGG19 model
Error: All inputs to the layer should be tensors- when trying to use VGG19 model

Time:12-03

While trying to import VGG19 model, the code below generates an error of non tensor inputs. Although I am following another this code snippet here.

Code:

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

Error:

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

unable to figure out why.

CodePudding user response:

Maybe try converting your images to tensors:

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

Instead of tf.expand_dims, you could also just do this:

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

There is also an option to load your images with 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)

CodePudding user response:

the code from this page works fine. i changed the code little bit.

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