用keras和tensorflow调整大小会得到不同的结果

时间:2019-07-17 12:20:44

标签: python tensorflow tf.keras

代码以重现错误:

import tensorflow as tf
from tensorflow import keras

inputs = keras.layers.Input(shape=(7,7,256))

keras_resized = keras.backend.resize_images(inputs,224,224,data_format='channels_last',interpolation='bilinear')

tf_resized = tf.image.resize_bilinear(inputs,(224,224))

我有以下发现:

1 tf_resized.get_shape()给出:

  

TensorShape([Dimension(None),Dimension(224),Dimension(224),Dimension(256)])

预期结果

2 keras_resized.get_shape()给出的位置:

  

TensorShape([Dimension(None),Dimension(1568),Dimension(1568),Dimension(256)])

这令人困惑。

3 。但是:

mean_input = keras.backend.mean(inputs, axis=[1,2], keepdims=True)
keras_resized = keras.backend.resize_images(mean_input,224,224,data_format='channels_last',interpolation='bilinear')

然后keras_resized.get_shape()给出:

  

TensorShape([Dimension(None),Dimension(224),Dimension(224),Dimension(256)])



我需要帮助

  • 调整大小的方法有何不同
  • 应该在哪种情况下使用
  • 案例1和3是否等效

2 个答案:

答案 0 :(得分:1)

据我了解,在TF 2.0中有两个用于调整图像大小的功能, 即:

tf.keras.backend.resize_images
tf.image.resize or tf.image.resize_images

这两个功能各有利弊。

对于tf.keras.backend.resize_images

优点:支持不同的张量通道顺序(请参见data_format参数) 缺点:图片只能按整数倍放大,而不能缩小(因为height_factor和width_factor必须为正整数)

对于tf.image.resize或tf.image.resize_images, 优点是: 1)它支持

等不同的插值方法
ResizeMethod.BILINEAR: Bilinear interpolation
ResizeMethod.NEAREST_NEIGHBOR: Nearest neighbor interpolation
ResizeMethod.BICUBIC: Bicubic interpolation
ResizeMethod.AREA: Area interpolation

2)此外,它还支持图像缩小。

根据要求可以使用这些方法

谢谢

答案 1 :(得分:0)

实际上,您不必将相同的参数传递给两个函数。例如,如果你想从 7 到 224,那么你需要输入 tensorflow 方法:

tf.image.resize(inputs,(224,224))

但是使用 keras 方法,您必须指定 height_factor 和 width_factor :所以在这种情况下 224/7 = 32

tf.keras.backend.resize_images( image, 32, 32, data_format)