【问题标题】:TensorflowHub pretrained MobileNetV2 change input shape and transfer learningTensorflowHub 预训练的 MobileNetV2 改变输入形状和迁移学习
【发布时间】:2020-05-06 06:44:18
【问题描述】:

我正在使用来自 Tensorflow Hub 的 MobileNetV2 140 224 作为预训练的 convnet 创建一个 NN。现在我想改变输入层的大小,我想输入 500x500 的图像。 这可能吗?实现这一目标的最佳方法是什么?

这是我的代码:

IMG_SHAPE = (224, 224, 3)
base_model = hub.KerasLayer('https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/4', input_shape=IMG_SHAPE)
base_model.trainable = False
model = Sequential([
  base_model,
  Dropout(0.25),
  Dense(3, activation='softmax')
])
adam = Adam(learning_rate=0.0001)
model.compile(optimizer=adam,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

我只能通过更改IMG_SHAPE = (500, 500, 3) 来做到这一点,还是必须添加输入层或其他东西?

【问题讨论】:

    标签: tensorflow keras tensorflow2.0 keras-layer tensorflow-hub


    【解决方案1】:

    按照您的建议更改 IMG_SHAPE 会导致信息丰富的错误消息(删节):

      ValueError: Could not find matching function to call loaded from the SavedModel. Got:
          Positional arguments (4 total):
            * Tensor("inputs:0", shape=(None, 500, 500, 3), dtype=float32)
            ...
    
        Expected these arguments to match one of the following 4 option(s):
    
        Option 1:
          Positional arguments (4 total):
            * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
            ...
    

    原来https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/4 的输入大小硬连线为 224x224。这是由于底层 TF-Slim 代码的限制,它需要一个已知高度和宽度的 tf.Placeholder 来构建 SavedModel。

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2018-02-10
      • 1970-01-01
      • 2022-09-27
      • 2021-10-14
      • 2022-11-10
      相关资源
      最近更新 更多