【问题标题】:Tensorflow hub: Extracting features from the topmost convolutional layer of a Resnet50Tensorflow hub:从 Resnet50 的最顶层卷积层中提取特征
【发布时间】:2021-06-15 15:55:44
【问题描述】:

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

因为我想从最后一个卷积层中提取特征,所以我有点困惑应该使用Resnet50 中的哪个字典输出。例如:

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 不同。有人可以指出应该使用哪个键从@9​​87654331@ 的最后一个卷积层提取特征,以及我列出的每个键之间的区别是什么?

谢谢!

【问题讨论】:

    标签: python tensorflow deep-learning resnet tensorflow-hub


    【解决方案1】:

    在 Tensorflow2 中,我们可以这样做。 更多文档:link

    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)
    

    【讨论】:

      【解决方案2】:

      根据the official manual,我们可以直接使用module(xxx)得到结果。

      对我来说很好用

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

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

      【讨论】:

        最近更新 更多