-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support Tensorflow model file read/write #800
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bff76f5
nn refactor
yiheng-wang-intel 269681c
fix code style issue
yiheng-wang-intel 27362e8
change back the layers
yiheng-wang-intel fd0b347
nn refactor
yiheng-wang-intel 1f5edcf
code refactor
yiheng-wang-intel 7df9a62
change tests to automatic test
0ab6677
add more test for model save
yiheng-wang-intel bf56fe3
remove some useless unit test
yiheng-wang-intel db5f385
add more save test
yiheng-wang-intel d481afc
add more writer test
yiheng-wang-intel bc4fd87
rnn test case automation
yangw1234 190ecf6
refine save test
yiheng-wang-intel fab5c47
refine save unit test
adcc53f
remove NHWC
01837b2
meet code review
3f8a858
meet code review
yiheng-wang-intel ca105c1
use MulConst in MulTF
yiheng-wang-intel 50c8d6c
add a flatten node for tf 1.1
yiheng-wang-intel 44b66b8
fix code style and failed unit test
yiheng-wang-intel 886f912
mv tf model layers to another package
yangw1234 11f6ad4
add a python example to show how to define model in tensorflow and ru…
yiheng-wang-intel 82bba8d
move tf example python code to example folder
yiheng-wang-intel ce5905e
Add backward test in LeNet and AlexNet (#19)
frankfzw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # | ||
| # Copyright 2016 The BigDL Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| import tempfile | ||
|
|
||
| import tensorflow as tf | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why import tensorflow? I don't think we want to do that. @yiheng |
||
|
|
||
| from google.protobuf import text_format | ||
|
|
||
| from tensorflow.core.framework import graph_pb2 | ||
| from tensorflow.python.client import session | ||
| from tensorflow.python.framework import graph_util | ||
| from tensorflow.python.framework import importer | ||
| from tensorflow.python.platform import gfile | ||
|
|
||
| from bigdl.nn.layer import Model | ||
|
|
||
| def convert(input_ops, output_ops, sess): | ||
| """ | ||
| Convert tensorflow model to bigdl model | ||
| :param input_ops: operation list used for input, should be placeholders | ||
| :param output_ops: operations list used for output | ||
| :param sess: current tensorflow session | ||
| :return: bigdl model | ||
| """ | ||
| input_names = map(lambda x: x.name.split(":")[0], input_ops) | ||
| output_names = map(lambda x: x.name.split(":")[0], output_ops) | ||
| temp = tempfile.mkdtemp() | ||
|
|
||
| saver = tf.train.Saver() | ||
| saver.save(sess, temp + '/model.chkp') | ||
| tf.train.write_graph(sess.graph, temp, 'model.pbtxt') | ||
|
|
||
| merge_checkpoint(temp + '/model.pbtxt', | ||
| temp + '/model.chkp', | ||
| output_names, | ||
| temp + '/model.pb', sess) | ||
| return Model.load_tensorflow(temp + '/model.pb', input_names, output_names) | ||
|
|
||
| def merge_checkpoint(input_graph, | ||
| checkpoint, | ||
| output_node_names, | ||
| output_graph, | ||
| sess): | ||
| """ | ||
| Get the variable values from the checkpoint file, and merge them to the GraphDef file | ||
| Args: | ||
| input_graph: the GraphDef file, doesn't contain variable values | ||
| checkpoint: the checkpoint file | ||
| output_node_names: A list of string, the output names | ||
| output_graph: String of the location and the name of the | ||
| output graph | ||
| """ | ||
| restore_op_name = "save/restore_all" | ||
| filename_tensor_name = "save/Const:0" | ||
|
|
||
| input_graph_def = graph_pb2.GraphDef() | ||
| with gfile.FastGFile(input_graph, "r") as f: | ||
| text_format.Merge(f.read().decode("utf-8"), input_graph_def) | ||
|
|
||
| for node in input_graph_def.node: | ||
| node.device = "" | ||
|
|
||
| importer.import_graph_def(input_graph_def, name="") | ||
|
|
||
| sess.run([restore_op_name], {filename_tensor_name: checkpoint}) | ||
| output_graph_def = graph_util.convert_variables_to_constants( | ||
| sess, | ||
| input_graph_def, | ||
| output_node_names, | ||
| variable_names_blacklist="" | ||
| ) | ||
|
|
||
| with gfile.GFile(output_graph, "wb") as f: | ||
| f.write(output_graph_def.SerializeToString()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # | ||
| # Copyright 2016 The BigDL Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| import tensorflow as tf | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add such an example here? It should go to unit test if needed. |
||
| import numpy as np | ||
| from bigdl.util.tf_utils import convert | ||
|
|
||
| def main(): | ||
| input = tf.placeholder(tf.float32, [None, 5]) | ||
| weight = tf.Variable(tf.random_uniform([5, 10])) | ||
| bias = tf.Variable(tf.random_uniform([10])) | ||
| middle = tf.nn.bias_add(tf.matmul(input, weight), bias) | ||
| output= tf.nn.tanh(middle) | ||
|
|
||
| tensor = np.random.rand(5, 5) | ||
| with tf.Session() as sess: | ||
| init = tf.global_variables_initializer() | ||
| sess.run(init) | ||
| tensorflow_result = sess.run(output, {input: tensor}) | ||
| bigdl_model = convert([input], [output], sess) | ||
| bigdl_result = bigdl_model.forward(tensor) | ||
|
|
||
| print("Tensorflow forward result is " + str(tensorflow_result)) | ||
| print("BigDL forward result is " + str(bigdl_result)) | ||
|
|
||
| np.testing.assert_almost_equal(tensorflow_result, bigdl_result, 6) | ||
| print("The results are almost equal in 6 decimals") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # | ||
| # Copyright 2016 The BigDL Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| cd "`dirname $0`" | ||
|
|
||
| $PYTHON_EXECUTABLE $BIGDL_HOME/pyspark/example/tf_example.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a test case to cover this function?