python - 使用 tensorflow-hub.KerasLayer 和 tensorflow 2.x 使用 Faster-RCNN 进行迁移学习
问题描述
系统信息
- 您正在使用的模型的顶级目录是什么:http : //download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz
- 我是否编写了自定义代码(而不是使用 TensorFlow 中提供的股票示例脚本):是
- 操作系统平台和发行版(例如,Linux Ubuntu 16.04):Windows 10
- TensorFlow 安装自(源或二进制):二进制
- TensorFlow 版本(使用下面的命令):2.1.0
- Bazel 版本(如果从源代码编译):
- CUDA/cuDNN 版本:10.0 / 7.6.5
- GPU型号和内存:GTX 1060
- 重现的确切命令:
我正在尝试使用 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)
解决方案
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射