TensorflowHub 预训练 MobileNetV2 改变输入形状和迁移学习

TensorflowHub pretrained MobileNetV2 change input shape and transfer learning

我正在使用来自 Tensorflow Hub 的 MobileNetV2 140 224 作为预训练卷积网络创建神经网络。现在我想更改输入层大小,我想输入 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) 或必须添加输入层或其他方式来实现?

按照您的建议更改 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。