首页 > 解决方案 > 使用 tensorflow-hub.KerasLayer 和 tensorflow 2.x 使用 Faster-RCNN 进行迁移学习

问题描述

系统信息

我正在尝试使用 fast_rcnn 进行迁移学习。目前这个模型不能通过 tensorflow-hub 获得,因此我必须从上面给出的地址加载一个遗留模块。我能够加载模型,检索 tensorflow-hub.KerasLayer 对象并通过它传递数据。

但是现在我想将此网络调整为我自己的仅包含 2 个类的数据集,所以我想知道如何修改 KerasLayer 对象,以便分类层不输出 90 个类,而只输出 2 个?

如果我没有使用正确的方法,你有什么建议来解决我的问题?

我想避免使用 tensorflow 对象检测 API。此外,正如文档中提到的那样,遗留模型是不可训练的,但我认为如果我添加自己的层不会有问题,对吗?

这是我正在使用的代码

import tensorflow_hub as hub
import numpy as np
import cv2 

# Run this once, then provide the local path where the model was downloaded, 
# instead of the below http address
model = hub.KerasLayer(
    "http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz",
    signature="serving_default",
    signature_outputs_as_dict=True,
)

image = np.expand_dims(cv2.imread("/your/image.jpg"), 0).astype(np.uint8)
# I would like this to output only 2 different classes in 'outputs["detection_classes"]'
outputs = model(image)

标签: pythontensorflowtensorflow-hub

解决方案


推荐阅读