【问题标题】:Is it possible to use tf.keras.models.clone_model to change shape of data input?是否可以使用 tf.keras.models.clone_model 来改变数据输入的形状?
【发布时间】:2021-07-15 12:22:50
【问题描述】:

我想使用 tf.keras 的方法 clone_model 并更改由功能 API 创建的 tensorflow/keras 模型的输入形状。因此,我尝试使用参数input_tensor 来改变形状。但是,它似乎没有使用提供的input_tensors,并且名称和形状与原始模型保持一致。参数input_tensors 打算用于什么?

代码:

import tensorflow as tf
from tensorflow.keras import layers

inputs_small = layers.Input((64, 64, 3), name="small")

outputs = layers.Conv2D(32, 1)(inputs_small)
model_small = tf.keras.models.Model(inputs=inputs_small, outputs=outputs)


inputs_large = layers.Input((128, 128, 3), name="large")
model_large = tf.keras.models.clone_model(model_small, input_tensors=inputs_large)

model_large.summary()

结果:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
small (InputLayer)           [(None, 64, 64, 3)]       0         
_________________________________________________________________
conv2d (Conv2D)              (None, 64, 64, 32)        128       
=================================================================

但我喜欢:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
large (InputLayer)           [(None, 128, 128, 3)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 128, 128, 32)      128       
=================================================================

我使用 TensorFlow 2.4.1。我简化了我的问题。在我的代码中,我还使用clone_model 的参数clone_function 来替换图层。

【问题讨论】:

  • 如果您的唯一目的是调整图像大小。请考虑添加Resizing 层。

标签: python tensorflow keras tf.keras


【解决方案1】:

我进一步调查发现了一个 Keras 错误: https://github.com/keras-team/keras/issues/14937

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多