在 Keras Lambda 层中调整输入图像的大小 [英] Resizing an input image in a Keras Lambda layer

查看:34
本文介绍了在 Keras Lambda 层中调整输入图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 keras 模型使用 cv2 或类似方法调整输入图像的大小.

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

我已经看到了 ImageGenerator 的使用,但我更愿意编写自己的生成器,并使用 keras.layers.core.Lambda.

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.

我该怎么做?

推荐答案

如果您使用的是 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()

这篇关于在 Keras Lambda 层中调整输入图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆