Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FRONTEND][TENSORFLOW] Helper function to add shapes into the graph. Use temp folder for models and clean it. #1697

Merged
merged 1 commit into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions nnvm/python/nnvm/testing/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os.path
import collections
import numpy as np
from tvm.contrib import util

# Tensorflow imports
import tensorflow as tf
Expand Down Expand Up @@ -43,6 +44,31 @@ def ProcessGraphDefParam(graph_def):
raise TypeError('graph_def must be a GraphDef proto.')
return graph_def


def AddShapesToGraphDef(out_node):
""" Add shapes attribute to nodes of the graph.
Input graph here is the default graph in context.

Parameters
----------
out_node: String
Final output node of the graph.

Returns
-------
graph_def : Obj
tensorflow graph definition with shapes attribute added to nodes.

"""

with tf.Session() as sess:
graph_def = tf.graph_util.convert_variables_to_constants(
sess,
sess.graph.as_graph_def(add_shapes=True),
[out_node],
)
return graph_def

class NodeLookup(object):
"""Converts integer node ID's to human readable labels."""

Expand Down Expand Up @@ -128,13 +154,18 @@ def get_workload(model_path):
model_url = os.path.join(repo_base, model_path)

from mxnet.gluon.utils import download
download(model_url, model_name)

temp = util.tempdir()
path_model = temp.relpath(model_name)

download(model_url, path_model)

# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(os.path.join("./", model_name), 'rb') as f:
with tf.gfile.FastGFile(path_model, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
graph = tf.import_graph_def(graph_def, name='')
temp.remove()
return graph_def

#######################################################################
Expand Down
4 changes: 2 additions & 2 deletions tutorials/nnvm/from_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
download(map_proto_url, map_proto)
download(lable_map_url, lable_map)


######################################################################
# Import model
# ------------
Expand All @@ -76,7 +75,8 @@
graph = tf.import_graph_def(graph_def, name='')
# Call the utility to import the graph definition into default graph.
graph_def = nnvm.testing.tf.ProcessGraphDefParam(graph_def)

# Add shapes to the graph.
graph_def = nnvm.testing.tf.AddShapesToGraphDef('softmax')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question. It seems that AddShapesToGraphDef override the graph_def returned from ProcessGraphDefParam. Is it no problem? AddShapesToGraphDef is only used for type checking?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats not a problem as AddShapesToGraphDef take the default graph which is in tf session and is same as graph_def earlier. This step just re freezes the same graph with shapes added .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I understand it. Thanks.


######################################################################
# Decode image
Expand Down