【问题标题】:Tensorflow 2 Hub: How can I obtain the output of an intermediate layer?Tensorflow 2 Hub:如何获得中间层的输出?
【发布时间】:2019-12-16 00:40:46
【问题描述】:

我正在尝试使用新的 tensorflow 2 实现以下网络 Fots 进行文本检测。作者使用 resnet 作为其网络的主干,所以我的第一个想法是使用 tensoflow hub resnet 来加载预训练网络.但问题是我找不到打印从 tfhub 加载的模块摘要的方法?

有什么方法可以查看从 tf-hub 加载的模块的层? 谢谢


更新

不幸的是,tf2-hub 无法使用 resnet,所以我决定使用 resent 的内置 keras 实现,至少在有 hub impl 之前是这样。 。

这是我使用 tf2.keras.applications 获得 resnet 中间层的方法:

import numpy as np
import tensorflow as tf
from tensorflow import keras

layers_out = ["activation_9", "activation_21", "activation_39", "activation_48"]

imgs = np.random.randn(2, 640, 640, 3).astype(np.float32)
model = keras.applications.resnet50.ResNet50(input_shape=(640, 640, 3), include_top=False)
intermid_outputs= [model.get_layer(layer_name).output for layer_name in layers_out]
shared_conds = keras.Model(inputs=model.input, outputs=intermid_outputs)
Y = conv_shared(imgs)
shapes = [y.shape for y in Y]
print(shapes)

【问题讨论】:

    标签: tensorflow tensorflow-hub


    【解决方案1】:

    您可以执行以下操作来检查中间输出:

    resnet = hub.Module("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3")
    outputs = resnet(np.random.rand(1,224,224,3), signature="image_feature_vector", as_dict=True)
    for intermediate_output in outputs.keys():
        print(intermediate_output)
    

    然后,如果您想将集线器模块的中间层链接到图的其余部分,您可以这样做:

    resnet = hub.Module("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3")
    features = resnet(images, signature="image_feature_vector", as_dict=True)["resnet_v2_50/block4"]
    flatten = tf.reshape(features, (-1, features.shape[3]))
    

    假设我们要从 ResNet 的最后一个块中提取特征。

    【讨论】:

    • 好的,那么问题是什么?
    • 我必须在 tf2 中禁用 Eager Execution 以获取层名称才能使其正常工作。此外,newer 版本不支持使用hub.Module 加载。我没有从文档中找到如何在加载 hub.KerasLayer 后从中间层中选择的示例
    【解决方案2】:

    假设您想获得中间输出,并在 tfhub 中为较新版本的 resnet 更新 @gorjan 答案,您可以尝试这样的事情。


    首先使用 hub.KerasLayer 和参数 return_endpoints=True 加载模型(这不适用于所有模型,并且未记录在 afaik 中,结果可能会有所不同):

    hub_model_layer = hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5",
                                         trainable=True,  
                                         arguments=dict(return_endpoints=True))
    

    然后你可以像这样编译一个模型:

    input = tf.keras.layers.Input((244, 244, 3))
    dict_output = hub_model_layer(input)
    
    C2 = dict_output['resnet_v2_50/block1/unit_1/bottleneck_v2/shortcut']
    C3 = dict_output['resnet_v2_50/block2/unit_1/bottleneck_v2/shortcut']
    C4 = dict_output['resnet_v2_50/block3/unit_1/bottleneck_v2/shortcut']
    C5 = dict_output['resnet_v2_50/block4/unit_1/bottleneck_v2/shortcut']
    
    model = tf.keras.Model(inputs=input, outputs=[C2, C3, C4, C5])
    

    变量dict_output 是一个包含所有可用端点的字典,您可以打印它来搜索您想要使用的输出。它们不按顺序排列,我也没有找到恢复图形的方法,但您可以通过图层名称猜测它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 2019-12-28
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多