Skip to content

Instantly share code, notes, and snippets.

@jzstark
Last active January 22, 2019 13:59
Show Gist options
  • Save jzstark/9eeff87dee7c5253a085f3cb2eccf80a to your computer and use it in GitHub Desktop.
Save jzstark/9eeff87dee7c5253a085f3cb2eccf80a to your computer and use it in GitHub Desktop.
## Simple CGraph
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import os
import sys
import numpy as np
import tensorflow as tf
log_dir = os.path.join(os.getenv('HOME'),
'Tmp/tf_converter/log')
checkpoint_file = os.path.join(os.getenv('HOME'),
'Tmp/tf_converter/model_simple.ckpt')
meta_file = checkpoint_file + '.meta'
## 1. Build Graph and save
with tf.Graph().as_default():
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=(3, 3), name='x')
weights = tf.Variable(tf.ones([3, 3]), name='weights')
y = tf.placeholder(tf.float32, shape=(), name='y')
z = 2 * (tf.matmul(x, weights) + y) - 1
tf.add_to_collection("result", z)
saver = tf.train.Saver()
init = tf.global_variables_initializer()
sess.run(init)
saver.save(sess, checkpoint_file)
## 2. Load CGraph in do a computation
def eval(meta_file, verbose=True, yvalue=3.):
with tf.Graph().as_default():
sess = tf.Session()
saver = tf.train.import_meta_graph(meta_file)
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('x:0')
y = graph.get_tensor_by_name('y:0')
result = tf.get_collection("result")[0]
init = tf.global_variables_initializer()
sess.run(init)
arr = np.ones((3, 3))
foo = sess.run(result, feed_dict={x:arr, y:yvalue})
summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
if verbose == True:
g = saver.export_meta_graph()
print(g)
print(foo)
eval(meta_file)
## 3. Save the previous cgraph into 'cgraph_simple.pbtxt' by redirecting output to that file
## 4. Generated pb file from pbtxt file, and do the evaluation again. It works!
import os
import sys
import tensorflow as tf
from google.protobuf import text_format
from tensorflow.python.framework import graph_io
filename = 'cgraph_simple'
with open(filename + '.pbtxt', 'r') as f:
metagraph_def = tf.MetaGraphDef()
file_content = f.read()
text_format.Merge(file_content,metagraph_def)
graph_io.write_graph(metagraph_def,
os.path.dirname(filename),
os.path.basename(filename) + '.pb',
as_text=False)
eval('cgraph_simple.pb', verbose=True, yvalue=1.)
meta_info_def {
stripped_op_list {
op {
name: "Add"
input_arg {
name: "x"
type_attr: "T"
}
input_arg {
name: "y"
type_attr: "T"
}
output_arg {
name: "z"
type_attr: "T"
}
attr {
name: "T"
type: "type"
allowed_values {
list {
type: DT_BFLOAT16
type: DT_HALF
type: DT_FLOAT
type: DT_DOUBLE
type: DT_UINT8
type: DT_INT8
type: DT_INT16
type: DT_INT32
type: DT_INT64
type: DT_COMPLEX64
type: DT_COMPLEX128
type: DT_STRING
}
}
}
}
op {
name: "Assign"
input_arg {
name: "ref"
type_attr: "T"
is_ref: true
}
input_arg {
name: "value"
type_attr: "T"
}
output_arg {
name: "output_ref"
type_attr: "T"
is_ref: true
}
attr {
name: "T"
type: "type"
}
attr {
name: "validate_shape"
type: "bool"
default_value {
b: true
}
}
attr {
name: "use_locking"
type: "bool"
default_value {
b: true
}
}
allows_uninitialized_input: true
}
op {
name: "Const"
output_arg {
name: "output"
type_attr: "dtype"
}
attr {
name: "value"
type: "tensor"
}
attr {
name: "dtype"
type: "type"
}
}
op {
name: "Identity"
input_arg {
name: "input"
type_attr: "T"
}
output_arg {
name: "output"
type_attr: "T"
}
attr {
name: "T"
type: "type"
}
}
op {
name: "MatMul"
input_arg {
name: "a"
type_attr: "T"
}
input_arg {
name: "b"
type_attr: "T"
}
output_arg {
name: "product"
type_attr: "T"
}
attr {
name: "transpose_a"
type: "bool"
default_value {
b: false
}
}
attr {
name: "transpose_b"
type: "bool"
default_value {
b: false
}
}
attr {
name: "T"
type: "type"
allowed_values {
list {
type: DT_BFLOAT16
type: DT_HALF
type: DT_FLOAT
type: DT_DOUBLE
type: DT_INT32
type: DT_COMPLEX64
type: DT_COMPLEX128
}
}
}
}
op {
name: "Mul"
input_arg {
name: "x"
type_attr: "T"
}
input_arg {
name: "y"
type_attr: "T"
}
output_arg {
name: "z"
type_attr: "T"
}
attr {
name: "T"
type: "type"
allowed_values {
list {
type: DT_BFLOAT16
type: DT_HALF
type: DT_FLOAT
type: DT_DOUBLE
type: DT_UINT8
type: DT_INT8
type: DT_UINT16
type: DT_INT16
type: DT_INT32
type: DT_INT64
type: DT_COMPLEX64
type: DT_COMPLEX128
}
}
}
is_commutative: true
}
op {
name: "NoOp"
}
op {
name: "Placeholder"
output_arg {
name: "output"
type_attr: "dtype"
}
attr {
name: "dtype"
type: "type"
}
attr {
name: "shape"
type: "shape"
default_value {
shape {
unknown_rank: true
}
}
}
}
op {
name: "RestoreV2"
input_arg {
name: "prefix"
type: DT_STRING
}
input_arg {
name: "tensor_names"
type: DT_STRING
}
input_arg {
name: "shape_and_slices"
type: DT_STRING
}
output_arg {
name: "tensors"
type_list_attr: "dtypes"
}
attr {
name: "dtypes"
type: "list(type)"
has_minimum: true
minimum: 1
}
is_stateful: true
}
op {
name: "SaveV2"
input_arg {
name: "prefix"
type: DT_STRING
}
input_arg {
name: "tensor_names"
type: DT_STRING
}
input_arg {
name: "shape_and_slices"
type: DT_STRING
}
input_arg {
name: "tensors"
type_list_attr: "dtypes"
}
attr {
name: "dtypes"
type: "list(type)"
has_minimum: true
minimum: 1
}
is_stateful: true
}
op {
name: "Sub"
input_arg {
name: "x"
type_attr: "T"
}
input_arg {
name: "y"
type_attr: "T"
}
output_arg {
name: "z"
type_attr: "T"
}
attr {
name: "T"
type: "type"
allowed_values {
list {
type: DT_BFLOAT16
type: DT_HALF
type: DT_FLOAT
type: DT_DOUBLE
type: DT_UINT8
type: DT_INT8
type: DT_UINT16
type: DT_INT16
type: DT_INT32
type: DT_INT64
type: DT_COMPLEX64
type: DT_COMPLEX128
}
}
}
}
op {
name: "VariableV2"
output_arg {
name: "ref"
type_attr: "dtype"
is_ref: true
}
attr {
name: "shape"
type: "shape"
}
attr {
name: "dtype"
type: "type"
}
attr {
name: "container"
type: "string"
default_value {
s: ""
}
}
attr {
name: "shared_name"
type: "string"
default_value {
s: ""
}
}
is_stateful: true
}
}
tensorflow_version: "1.12.0"
tensorflow_git_version: "v1.12.0-rc2-3-ga6d8ffae09"
}
graph_def {
node {
name: "x"
op: "Placeholder"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
node {
name: "ones"
op: "Const"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 3
}
dim {
size: 3
}
}
float_val: 1.0
}
}
}
}
node {
name: "weights"
op: "VariableV2"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
attr {
key: "container"
value {
s: ""
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
attr {
key: "shared_name"
value {
s: ""
}
}
}
node {
name: "weights/Assign"
op: "Assign"
input: "weights"
input: "ones"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_class"
value {
list {
s: "loc:@weights"
}
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
attr {
key: "use_locking"
value {
b: true
}
}
attr {
key: "validate_shape"
value {
b: true
}
}
}
node {
name: "weights/read"
op: "Identity"
input: "weights"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_class"
value {
list {
s: "loc:@weights"
}
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
}
node {
name: "y"
op: "Placeholder"
attr {
key: "_output_shapes"
value {
list {
shape {
}
}
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
}
}
}
}
node {
name: "MatMul"
op: "MatMul"
input: "x"
input: "weights/read"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
attr {
key: "transpose_a"
value {
b: false
}
}
attr {
key: "transpose_b"
value {
b: false
}
}
}
node {
name: "add"
op: "Add"
input: "MatMul"
input: "y"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
}
node {
name: "mul/x"
op: "Const"
attr {
key: "_output_shapes"
value {
list {
shape {
}
}
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 2.0
}
}
}
}
node {
name: "mul"
op: "Mul"
input: "mul/x"
input: "add"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
}
node {
name: "sub/y"
op: "Const"
attr {
key: "_output_shapes"
value {
list {
shape {
}
}
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 1.0
}
}
}
}
node {
name: "sub"
op: "Sub"
input: "mul"
input: "sub/y"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
}
node {
name: "save/Const"
op: "Const"
attr {
key: "_output_shapes"
value {
list {
shape {
}
}
}
}
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "model"
}
}
}
}
node {
name: "save/SaveV2/tensor_names"
op: "Const"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 1
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
dim {
size: 1
}
}
string_val: "weights"
}
}
}
}
node {
name: "save/SaveV2/shape_and_slices"
op: "Const"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 1
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
dim {
size: 1
}
}
string_val: ""
}
}
}
}
node {
name: "save/SaveV2"
op: "SaveV2"
input: "save/Const"
input: "save/SaveV2/tensor_names"
input: "save/SaveV2/shape_and_slices"
input: "weights"
attr {
key: "dtypes"
value {
list {
type: DT_FLOAT
}
}
}
}
node {
name: "save/control_dependency"
op: "Identity"
input: "save/Const"
input: "^save/SaveV2"
attr {
key: "T"
value {
type: DT_STRING
}
}
attr {
key: "_class"
value {
list {
s: "loc:@save/Const"
}
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
}
}
}
}
}
node {
name: "save/RestoreV2/tensor_names"
op: "Const"
device: "/device:CPU:0"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 1
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
dim {
size: 1
}
}
string_val: "weights"
}
}
}
}
node {
name: "save/RestoreV2/shape_and_slices"
op: "Const"
device: "/device:CPU:0"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 1
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
dim {
size: 1
}
}
string_val: ""
}
}
}
}
node {
name: "save/RestoreV2"
op: "RestoreV2"
input: "save/Const"
input: "save/RestoreV2/tensor_names"
input: "save/RestoreV2/shape_and_slices"
device: "/device:CPU:0"
attr {
key: "_output_shapes"
value {
list {
shape {
unknown_rank: true
}
}
}
}
attr {
key: "dtypes"
value {
list {
type: DT_FLOAT
}
}
}
}
node {
name: "save/Assign"
op: "Assign"
input: "weights"
input: "save/RestoreV2"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_class"
value {
list {
s: "loc:@weights"
}
}
}
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: 3
}
dim {
size: 3
}
}
}
}
}
attr {
key: "use_locking"
value {
b: true
}
}
attr {
key: "validate_shape"
value {
b: true
}
}
}
node {
name: "save/restore_all"
op: "NoOp"
input: "^save/Assign"
}
node {
name: "init"
op: "NoOp"
input: "^weights/Assign"
}
versions {
producer: 27
}
}
saver_def {
filename_tensor_name: "save/Const:0"
save_tensor_name: "save/control_dependency:0"
restore_op_name: "save/restore_all"
max_to_keep: 5
keep_checkpoint_every_n_hours: 10000.0
version: V2
}
collection_def {
key: "result"
value {
node_list {
value: "sub:0"
}
}
}
collection_def {
key: "trainable_variables"
value {
bytes_list {
value: "\n\tweights:0\022\016weights/Assign\032\016weights/read:02\006ones:08\001"
}
}
}
collection_def {
key: "variables"
value {
bytes_list {
value: "\n\tweights:0\022\016weights/Assign\032\016weights/read:02\006ones:08\001"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment