Resizing an input image in a Keras Lambda layer

风流意气都作罢 提交于 2019-12-28 13:08:06

问题


I would like my keras model to resize the input image using cv2 or similar.

I have seen the use of ImageGenerator, but I would prefer to write my own generator and simply resize the image in the first layer with keras.layers.core.Lambda.

How would I do this?


回答1:


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


来源:https://stackoverflow.com/questions/42260265/resizing-an-input-image-in-a-keras-lambda-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!