Tensorflow Hub:从Resnet50的最高卷积层中提取特征



我正在使用Tensorflow Hub从图像中提取功能。即,我正在使用模块hub.Module("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3")

因为我想从最后一个卷积层中提取功能,所以我有点困惑,我应该从 Resnet50中使用哪种dictonary输出。例如:

image = ...
embedding_module = hub.Module("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3")
output = embedding_module(image, signature="image_feature_vector",
                          as_dict=True)

现在,如果我们从该字典中打印出键,则有3个不同的键,我不知道。

之间的区别。
  • resnet_v2_50/block4/unit_3/bottleneck_v2
  • resnet_v2_50/block4/unit_3/bottleneck_v2/conv3
  • resnet_v2_50/block4

我发现令人困惑的是,它们的输出具有相同的形状(7, 7, 2048),但是resnet_v2_50/block4/unit_3/bottleneck_v2resnet_v2_50/block4的值与resnet_v2_50/block4/unit_3/bottleneck_v2/conv3不同。有人可以指向我应该将哪个键用于从Resnet50的最后一个卷积层进行特征提取,以及我列出的每个键之间有什么区别?

谢谢!

根据官方手册,我们可以直接使用module(xxx)获得结果。

它对我来说很好

module = hub.Module("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3")
features = module(images) 

实际上,我还试图弄清楚如何通过name获得正确的最后一层,而不是缺乏灵活性的预先提供的API。

在Tensorflow2中,我们可以按照以下操作。有关更多文档:链接

from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
# as per updated docs
model = hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5")
imagePath = "......."
image = load_img(imagePath, target_size=(224, 224))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
embeddings = model(image)