1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| import tensorflow as tf import numpy as np import os from tensorflow.python.framework.graph_util import convert_variables_to_constants
class TestCase(object):
def __init__(self, batch_size, feature_size, hidden_size, output_size): np.random.seed(123)
self.batch_size = batch_size self.feature_size = feature_size self.hidden_size = hidden_size self.output_size = output_size
self.input_file = "./tf_test_input.txt" self.output_file = "./tf_test_output.txt" self.ckpt_prefix = "./ckpt/model" self.pb_file = "./pb/tf_test_model.pb" if not os.path.exists(os.path.dirname(self.ckpt_prefix)): os.makedirs(os.path.dirname(self.ckpt_prefix)) if not os.path.exists(os.path.dirname(self.pb_file)): os.makedirs(os.path.dirname(self.pb_file))
self.input_name = 'input' self.output_name = 'output'
self.prepare_data()
def prepare_data(self): self.x_data = np.random.random((self.batch_size, self.feature_size)) * 2.0 self.y_data = np.random.random((self.batch_size, self.output_size)) * 2.0 with open(self.input_file, 'w') as f: f.write('\n'.join([str(i) for i in self.x_data.flatten()])) print("save input data to file: " + self.input_file) with open(self.output_file, 'w') as f: f.write('\n'.join([str(i) for i in self.y_data.flatten()])) print("save output data to file: " + self.input_file)
def add_fc_layer(self, inputs, in_size, out_size, activation_function=None): w = tf.Variable(tf.random_normal([in_size, out_size])) b = tf.Variable(tf.zeros([1, out_size]) + 0.1) y = tf.matmul(inputs, w) + b if activation_function is None: outputs = y else: outputs = activation_function(y) return outputs
def train_network(self): with tf.Session(graph=tf.Graph()) as sess: x_train_data = tf.placeholder(tf.float32, shape=(self.batch_size, self.feature_size), name=self.input_name) y_train_data = tf.placeholder(tf.float32, shape=(self.batch_size, self.output_size), name='label') l1 = self.add_fc_layer(x_train_data, self.feature_size, self.hidden_size, tf.nn.relu) prediction = self.add_fc_layer(l1, self.hidden_size, self.output_size, None) output = tf.identity(prediction, self.output_name)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(y_train_data - output), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
saver = tf.train.Saver() sess.run(tf.global_variables_initializer())
for i in range(1001): sess.run(train_step, feed_dict={x_train_data: self.x_data, y_train_data: self.y_data}) if i % 50 == 0: print(sess.run(loss, feed_dict={x_train_data: self.x_data, y_train_data: self.y_data})) saver.save(sess, self.ckpt_prefix)
tf.train.write_graph(tf.get_default_graph(), ".", self.pb_file + "txt", as_text=True)
def restore_from_ckpt(self): with tf.Session() as sess: saver = tf.train.import_meta_graph(self.ckpt_prefix + '.meta') saver.restore(sess, tf.train.latest_checkpoint(os.path.dirname(self.ckpt_prefix)))
input_tensor = tf.get_default_graph().get_tensor_by_name('{}:0'.format(self.input_name)) output_tensor = tf.get_default_graph().get_tensor_by_name('{}:0'.format(self.output_name))
print(sess.run(output_tensor, feed_dict={input_tensor: self.x_data}))
def convert_to_pb(self): with tf.Session() as sess: saver = tf.train.import_meta_graph(self.ckpt_prefix + '.meta') saver.restore(sess, tf.train.latest_checkpoint(os.path.dirname(self.ckpt_prefix))) graph = tf.get_default_graph()
output_graph_def = convert_variables_to_constants( sess, sess.graph_def, output_node_names=[self.output_name]) with tf.gfile.FastGFile(self.pb_file, mode='wb') as f: f.write(output_graph_def.SerializeToString())
def restore_from_pb(self): with tf.gfile.GFile(self.pb_file, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read())
with tf.Session(graph=tf.get_default_graph()) as sess: tf.import_graph_def(graph_def, name='')
input_tensor = tf.get_default_graph().get_tensor_by_name('{}:0'.format(self.input_name)) output_tensor = tf.get_default_graph().get_tensor_by_name('{}:0'.format(self.output_name)) print(sess.run(output_tensor, feed_dict={input_tensor: self.x_data}))
if __name__ == "__main__": tc = TestCase(batch_size=128, feature_size=1024, hidden_size=2048, output_size=64) tc.train_network()
|