首页 > 解决方案 > Inception V3 可以使用 150x150x3 的图像尺寸吗?

问题描述

我在官方 keras 文档上看到了这段代码,并且我已经阅读了在输入模型之前需要调整大小/缩放的图像。你能给些建议么?

from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.layers import Input

# input size
input_tensor = Input(shape=(150, 150, 3))

model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)

标签: kerasclassification

解决方案


只要您的图像有 3 个通道,Inception V3就可以处理任何大小的图像。因为 ImageNet 图像由 3 个通道组成。它可以用于任何尺寸的原因是卷积不关心图像尺寸。您还可以将它与灰度图像一起使用,但需要做一些额外的工作,但我不确定它是否会破坏网络性能等。为此,您需要设置include_top = False,否则您的图像大小应与模型定义的大小匹配,(299,299,3).

Lambda您可以使用图层重新调整图像大小。假设您有 1024x1024 图像:

input_images = tf.keras.Input(shape=(1024, 1024, 3))

whatever_this_size = tf.keras.layers.Lambda(lambda x: tf.image.resize(x,(150,150),
                     method=tf.image.ResizeMethod.BILINEAR))(input_images)

model = InceptionV3(input_tensor=whatever_this_size, weights='imagenet', include_top=False)

如果您使用的是 TF-Dataset API,您还可以执行以下操作:

your_train_data = your_train_data.map(lambda x, y: (tf.image.resize(x, (150,150), y))

推荐阅读