【问题标题】:how to create a new tensorflow hub module from an existing frozen pb model file如何从现有的冻结 pb 模型文件创建新的 tensorflow hub 模块
【发布时间】:2019-09-30 03:36:41
【问题描述】:

我正在尝试将现有的冻结 Tensorflow 模型转换为用于图像分类迁移学习的 tensorflow_hub 模块,转换没有错误,但新模块的推理准确度很低,只有 40%~50%,我在这里想念什么?

我正在使用来自 Tensorflow-hub git 存储库的图像分类转移学习示例代码“retrain.py”。 “retrain.py”脚本使用Tensorflow-hub模块作为输入,所以我从“http://download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_1.0_224.tgz”下载了一个冻结的预训练模型,并将其转换为一个hub模块,然后使用这个新模块作为输入重新训练脚本。我的设置是 ubuntu14.04、python2.7、Tensorflow-1.12 和 Tensorflow-hub-0.4.0。

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np

MODEL="mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_frozen.pb"
MODULE_PATH="output_hub"

def module_fn():
    input_name="input:0"
    output_name="MobilenetV1/Predictions/Reshape_1:0"
    with tf.gfile.GFile(MODEL, 'rb') as f:
        graph_def=tf.GraphDef()
        graph_def.ParseFromString(f.read())
        input_tensor = tf.placeholder(tf.float32, [None, 224,224, 3])
        output_tensor, = tf.import_graph_def(graph_def, input_map = {input_name: input_tensor}, return_elements=[output_name])
        hub.add_signature(inputs = {"images": input_tensor}, outputs = output_tensor)

spec = hub.create_module_spec(module_fn)
with tf.Graph().as_default():
    module = hub.Module(spec)
    input = np.random.normal(0, 1, (1, 224, 224, 3))
    output = module(input)
    with tf.Session() as session:
        session.run(output)
        module.export(MODULE_PATH, session=session)

spec = hub.load_module_spec(MODULE_PATH)
height, width = hub.get_expected_image_size(spec)
with tf.Graph().as_default() as graph:
    module = hub.Module(spec)
    input_tensor = tf.placeholder(tf.float32, [None, height, width, 3])
    output_tensor = module(input_tensor)
    with tf.Session() as session:
        for node in graph.as_graph_def().node:
            print(node.name)

【问题讨论】:

  • 面临一个非常相似的问题(Mobilenet V2)。我的准确率下降不是那么严重,但仍然会发生。有消息吗?

标签: tensorflow tensorflow-hub


【解决方案1】:

您可以直接使用retrain.pyhttps://tfhub.dev/google/imagenet/mobilenet_v1_100_224/feature_vector/3 并保存手动转换。 retrain.py 将在退出时为您冻结生成的模型。

也就是说,还有更新的 tf2_image_retraining colab 甚至可以进行微调,但它使用 TensorFlow 2,并且两者都还处于预览阶段。

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多