【问题标题】:Is there any way to access layers in tensorflow_hub.KerasLayer object?有什么方法可以访问 tensorflow hub.KerasLayer 对象中的图层吗?
【发布时间】:2020-09-11 17:56:47
【问题描述】:

我正在尝试将来自 tensorflow hub 的预训练模型用于我的对象检测模型。我按照官方说明将集线器中的模型包装为 KerasLayer 对象。然后我意识到我无法访问这个预训练模型中的层。但我需要使用某些特定层的输出来构建我的模型。有什么方法可以访问 tensorflow_hub.KerasLayer 对象中的图层吗?

【问题讨论】:

    标签: tensorflow keras tensorflow-hub


    【解决方案1】:

    为了能够轻松地做到这一点,预训练模型的创建者需要准备好可以访问该输出。例如。通过拥有一个额外的函数或额外的签名来输出你想要使用的激活。

    【讨论】:

    • 那么创作者不提供就不可能了?
    【解决方案2】:

    有一种未记录的方法可以从一些从 TF-Slim 导出的 TF2 SavedModel 中获取中间层,例如 https://tfhub.dev/google/imagenet/inception_v1/feature_vector/4:将 return_endpoints=True 传递给 SavedModel 的 __call__ 函数会将输出更改为 dict

    注意:此界面可能会更改或删除,并且存在已知问题。

    model = tfhub.KerasLayer('https://tfhub.dev/google/imagenet/inception_v1/feature_vector/4', trainable=False, arguments=dict(return_endpoints=True))
    input = tf.keras.layers.Input((224, 224, 3))
    outputs = model(input)
    for k, v in sorted(outputs.items()):
      print(k, v.shape)
    

    此示例的输出:

    InceptionV1/Conv2d_1a_7x7 (None, 112, 112, 64)
    InceptionV1/Conv2d_2b_1x1 (None, 56, 56, 64)
    InceptionV1/Conv2d_2c_3x3 (None, 56, 56, 192)
    InceptionV1/MaxPool_2a_3x3 (None, 56, 56, 64)
    InceptionV1/MaxPool_3a_3x3 (None, 28, 28, 192)
    InceptionV1/MaxPool_4a_3x3 (None, 14, 14, 480)
    InceptionV1/MaxPool_5a_2x2 (None, 7, 7, 832)
    InceptionV1/Mixed_3b (None, 28, 28, 256)
    InceptionV1/Mixed_3c (None, 28, 28, 480)
    InceptionV1/Mixed_4b (None, 14, 14, 512)
    InceptionV1/Mixed_4c (None, 14, 14, 512)
    InceptionV1/Mixed_4d (None, 14, 14, 512)
    InceptionV1/Mixed_4e (None, 14, 14, 528)
    InceptionV1/Mixed_4f (None, 14, 14, 832)
    InceptionV1/Mixed_5b (None, 7, 7, 832)
    InceptionV1/Mixed_5c (None, 7, 7, 1024)
    InceptionV1/global_pool (None, 1, 1, 1024)
    default (None, 1024)
    

    需要注意的问题:

    • 未记录,可能会更改或删除,无法始终如一地提供。
    • __call__ 计算所有输出(并在训练期间应用所有更新操作),无论稍后使用的输出如何。

    来源:https://github.com/tensorflow/hub/issues/453

    【讨论】:

    • 谢谢!一种有趣的实现方式
    【解决方案3】:

    因为return_endpoints=True 似乎不再起作用了。

    你可以这样做:

    efficientnet_lite0_base_layer = hub.KerasLayer(
        "https://tfhub.dev/tensorflow/efficientnet/lite0/feature-vector/2",
        output_shape=[1280],
        trainable=False
    )
    
    print("Thickness of the model:", len(efficientnet_lite0_base_layer.weights))
    print ("{:<80} {:<20} {:<10}".format('Layer','Shape','Type'))
    
    for i in range(len(efficientnet_lite0_base_layer.weights)):
        model_weights_raw_string = str(efficientnet_lite0_base_layer.weights[i])
        model_weights_wo_weights = model_weights_raw_string.split(", numpy", 1)[0]
        dtype = model_weights_wo_weights.split(" dtype=")[1]
        shape = model_weights_wo_weights.split(" shape=")[1].split(" dtype=")[0]
        
        print ("{:<80} {:<20} {:<10}".format(efficientnet_lite0_base_layer.weights[i].name, shape, dtype))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-20
      • 2018-11-30
      • 2011-07-30
      • 2010-10-26
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多