Skip to content

How to create a new module from an existing frozen model #299

@yangyn2018

Description

@yangyn2018

I am trying to convert an existing frozen Tensorflow model to a Tensorflow_hub module for image-classification transfer learning use, the converting passed with no error but the inference accuracy of the new module is quite low, only 40%~50%, what I am missing here?

I am using an image-classification transfer leaning example code "retrain.py" from Tensorflow-hub git repository. The "retrain.py" script uses a Tensorflow-hub module as the input, so I downloaded a frozen pre-trained model from "http://download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_1.0_224.tgz", and convert it to a hub module and then use this new module as the input of the re-train script.
(I know I can download the pre-trained module from https://tfhub.dev/, but I also need convert my trained new model to a tensorflow hub module, so I still want to know how to do this "converting" correctly )
My set up is ubuntu14.04, python 2.7, Tensorflow-1.12 and Tensorflow-hub-0.4.0.

following is the code I use:

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)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions