且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Keras Lambda 层中调整输入图像的大小

更新时间:2023-12-01 21:44:40

如果您使用的是 tensorflow 后端,那么您可以使用 tf.image.resize_images() 函数来调整 中的图像大小Lambda 层.

If you are using tensorflow backend then you can use tf.image.resize_images() function to resize the images in Lambda layer.

这是一个小例子来演示相同的:

Here is a small example to demonstrate the same:

import numpy as np
import scipy.ndimage
import matplotlib.pyplot as plt

from keras.layers import Lambda, Input
from keras.models import Model
from keras.backend import tf as ktf


# 3 channel images of arbitrary shape
inp = Input(shape=(None, None, 3))
try:
    out = Lambda(lambda image: ktf.image.resize_images(image, (128, 128)))(inp)
except :
    # if you have older version of tensorflow
    out = Lambda(lambda image: ktf.image.resize_images(image, 128, 128))(inp)

model = Model(input=inp, output=out)
model.summary()

X = scipy.ndimage.imread('test.jpg')

out = model.predict(X[np.newaxis, ...])

fig, Axes = plt.subplots(nrows=1, ncols=2)
Axes[0].imshow(X)
Axes[1].imshow(np.int8(out[0,...]))

plt.show()