【问题标题】:Understanding the input_shape parameter of hub.KerasLayer理解 hub.KerasLayer 的 input_shape 参数
【发布时间】:2020-11-01 03:18:03
【问题描述】:

迁移学习完成后,可以使用 tf hub 中的模型。像 MobilNetV2 或 Inception。这些模型需要输入,即一定大小的图像。因此,在应用模型之前,必须将图像调整为这个大小。在这个tutorial 中使用了以下内容:

feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2" 

feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=(224,224,3))

在此示例中,图像之前已调整为 224,224。我想知道input_shape=(224,224,3)。在这个tutorial 中,预训练模型没有加载 hub-KerasLayer,而是使用

base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')

IMG_SHAPE 在哪里

IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

img_size 是 160。所以这里的 input_shape 是 input_shape=(160,160,3)。

现在回到:

feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=(224,224,3))

我想知道 input_shape 参数到底告诉我什么或做了什么?所以我这里不需要输入 224,224 对吧?我可以输入另一个尺寸,比如 160,因为我的图像被调整到这个尺寸?所以 MobilNetV2 确实期望 224,224,但是使用此选项我可以指定其他内容吗?对于tf.keras.applications.MobileNetV2,我找到了documentation,它准确地解释了它:

可选的形状元组,如果您想使用模型,请指定 输入图像分辨率不是 (224, 224, 3)。这应该 正好有 3 个输入通道(224、224、3)。你也可以省略这个 如果您想从 input_tensor 推断 input_shape 选项。如果 您选择同时包含 input_tensor 和 input_shape 如果它们匹配,将使用 input_shape,如果形状不匹配 然后我们会抛出一个错误。例如。 (160, 160, 3) 将是一个有效的 价值。

所以当我将图像大小调整为 300,300 并且我想使用 MobileNetV2 时,是否可以使用以下代码:

 feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2" 
    
    feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                             input_shape=(300,300,3))

或者我必须将大小调整为 224,224 并在此处输入 224,224?

当我检查 implementation 的初始值时,图像被调整为 299,299,然后使用以下代码:

IMAGE_RES = 299

feature_extractor = hub.KerasLayer(URL,
  input_shape=(IMAGE_RES, IMAGE_RES, 3),
  trainable=False)

是否有必要精确到 299?或者我也可以调整到另一个尺寸,比如 250 并将其作为输入:

   IMAGE_RES = 250

feature_extractor = hub.KerasLayer(URL,
  input_shape=(IMAGE_RES, IMAGE_RES, 3),
  trainable=False)

因此,预训练模型确实需要某个固定大小,并且存在这个 input_shape 参数是为了使其灵活,以防用户想要使用其他大小,对吧?但是,为什么所有这些示例都会调整到模型假设的大小呢?我也可以这样做到另一个尺寸,对吧?因此,在所有示例中,它都表示模型期望这一点,我以这样的方式理解它,因此我们必须调整大小以完全符合模型的期望。但是 input_shape 参数是完全存在的,以使其灵活,这样我就不必调整到模型所期望的大小,而只需调整到我想要的任何大小,并使用 input_shape 参数告诉模型?如上面提到的 160 图像大小的示例。或者如果我使用 tf.keras.applications.MobileNetV2 加载预训练模型,但使用 hub.KerasLayer 时我无法做到这一点,这是否可行?

【问题讨论】:

    标签: tensorflow keras tensorflow-hub


    【解决方案1】:

    这是一个很好的观察。

    TLDR,可以为 Modelstf.keras.applicationsinclude_top = False 传递不同的 Input Shapes,但是当我们将 tf.keras.applications 与参数 @ 一起使用时,这是不可能的987654327@ 以及当我们使用ModelsTensorflow Hub 时。

    详细说明

    这个Tensorflow Hub Documentation 状态

    > The height and width dimensions are fixed to the expected size of
    > input images. (Future work may remove that restriction for fully
    > convolutional modules.)
    

    这就是原因,如果我们传递 Image Shape 而不是 Expected Shape,则会引发错误,

     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')
            * True
            * False
            * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
          Keyword arguments: {}
        
        Option 2:
          Positional arguments (4 total):
            * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
            * True
            * True
            * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
          Keyword arguments: {}
        
        Option 3:
          Positional arguments (4 total):
            * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
            * False
            * True
            * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
          Keyword arguments: {}
        
        Option 4:
          Positional arguments (4 total):
            * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
            * False
            * False
            * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
          Keyword arguments: {}
    

    同样,当我们在使用 tf.keras.applicationsPre-Trained Models 和参数 include_top = True(包括顶部的密集层)时传递不同的 Input Shape,它会引发一个错误,

    ValueError: When setting `include_top=True` and loading `imagenet` 
    weights, `input_shape` should be (224, 224, 3).
    

    但是如果我们设置参数的值,include_top = False,同时使用来自tf.keras.applicationsPre-Trained ModelsInput_Shape 可以是灵活的,即MobileNetV2,我们可以传递列表中的任何形状,[96, 128, 160, 192, 224]),对于像 ResNetVGGNet 这样的模型,我们可以传递任何 Input Shape

    【讨论】:

    • 感谢您的回答。不幸的是,这只有 50% 是正确的。 “但如果我们使用来自 tf.keras.applications 的预训练模型,则 Input_Shape 可以灵活,因为它已在这些模型的源代码中处理。”这不是真的。 image_size 只能用于以下值:[96, 128, 160, 192, 224]。当我尝试运行 tf.keras.applications.MobileNetV2(input_shape=(140, 140, 3), include_top=False, weights='imagenet') 我收到一个错误提示:
    • WARNING:tensorflow:input_shape 未定义或非正方形,或者 rows 不在 [96, 128, 160, 192, 224] 中。输入形状 (224, 224) 的权重将作为默认值加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2020-05-29
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多