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

查看:893
本文介绍了在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天全站免登陆