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
尝试导入 VGG19 模型时,以下代码生成非张量输入错误。虽然我正在关注另一个代码片段 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.
无法弄清楚原因。
也许尝试将您的图像转换为张量:
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)
此 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)
尝试导入 VGG19 模型时,以下代码生成非张量输入错误。虽然我正在关注另一个代码片段 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.
无法弄清楚原因。
也许尝试将您的图像转换为张量:
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)
此 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)