1

我正在尝试将现有的冻结 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 ”下载了一个冻结的预训练模型,并且将其转换为集线器模块,然后将此新模块用作重新训练脚本的输入。我的设置是 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)

4

1 回答 1

0

You can use retrain.py directly with https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/feature_vector/3 and save the manual conversion. retrain.py will freeze the resulting model for you on the way out.

That said, there is also the newer tf2_image_retraining colab which can even do fine-tuning, but it uses TensorFlow 2, and both are still in preview.

于 2019-05-24T14:34:57.433 回答