1

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.

AloneTogether
  • 25,814
  • 5
  • 20
  • 39
Safi
  • 152
  • 1
  • 11

2 Answers2

1

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

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)
AloneTogether
  • 25,814
  • 5
  • 20
  • 39
  • thank you. I am getting wrong answer, i dont know why. first it was generating error `TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64, ` so i changed the code as `img1 = tf.expand_dims(tf.constant(img1.astype('float32')), axis=0) img2 = tf.expand_dims(tf.constant(img2.astype('float32')), axis=0). ` now the result i get is `Tensor("Mean:0", shape=(), dtype=float32)` – Safi Dec 03 '21 at 02:53
  • although code from this [page](https://keras.io/api/applications/) works fine. i chnaged the code as in my answer below – Safi Dec 03 '21 at 05:33
  • 1
    @Safi Your code is working fine...you just not seeing the value being printed...do you have eager mode on? – AloneTogether Dec 03 '21 at 07:01
  • 1
    you are right. my eager execution was turned off. `tf.compat.v1.enable_eager_execution(config=None, device_policy=None, execution_mode=None)` did the job. thank you – Safi Dec 03 '21 at 15:53
0

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)
Safi
  • 152
  • 1
  • 11