Tensorflow 2 Hub:如何获得中间层的输出? [英] Tensorflow 2 Hub: How can I obtain the output of an intermediate layer?

查看:116
本文介绍了Tensorflow 2 Hub:如何获得中间层的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement following network Fots for Text detection using the new tensorflow 2. The authors use the resnet as the backbone of their network, so my first thought was to use the tensoflow hub resnet for loading a pretrained network. But the problem is that i can not find a way to print the summary of the module, which is loaded from tfhub?

有没有办法从tf-hub看到加载模块的层数?谢谢

Is there any way to see the layers of the loaded modules from tf-hub? Thanks

更新

不幸的是,tf2-hub 无法使用 resnet,所以我决定使用 resent 的内置 keras 实现,至少在有集线器实现之前.

Unfortunately is the resnet not available for tf2-hub, so i deceided to use the builtin keras implementation of resent, at least till there is a hub impl. of it.

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

Here is how i get the intermediate layers of resnet using tf2.keras.applications:

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)

推荐答案

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

You can do something like this to examine the intermediate outputs:

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)

然后,如果您想将集线器模块的中间层链接到图形的其余部分,您可以执行以下操作:

Then, if you want to link an intermediate layer of the hub module to the rest of your graph, you can do:

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 的最后一个块中提取特征.

Assuming that we want to extract the features from the last block of the ResNet.

这篇关于Tensorflow 2 Hub:如何获得中间层的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆