-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensorflow_graph_loader.py
More file actions
56 lines (51 loc) · 2.59 KB
/
tensorflow_graph_loader.py
File metadata and controls
56 lines (51 loc) · 2.59 KB
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
'''
This program exists solely to provide an example of how to load a generic tensorflow graph into your program. This accounts for the bare-minimum variables stored within the graph:
+ data placeholder
+ class placeholder
+ prediction label placeholder
+ final graph layer placeholder
+ [Optional] loss placeholder
+ [Optional] optimizer placeholder
+ [Optional] actual label placeholder
+ [Optional] probibility of correct prediction placeholder
+ [Optional] accuracy placeholder
'''
import tensorflow as tf
# Create a loader for the graph
def graph_loader(logdir=None, path_to_checkpoint=None, x_placeholder=None, y_placeholder=None, pred_label_placeholder=None, final_graph_layer_placeholder=None, loss_placeholder=None, adam_op_placeholder=None, actual_label_placeholder=None,
correct_prediction_placeholder=None, accuracy_placeholder=None):
with tf.Session() as sess:
#load the graph
restore_saver = tf.train.import_meta_graph(path_to_checkpoint)
#reload all the params to the graph
restore_saver.restore(sess, tf.train.latest_checkpoint(logdir))
global model
model = tf.get_default_graph()
#store the variables
global x
x = model.get_tensor_by_name(x_placeholder)
global y_
y_ = model.get_tensor_by_name(y_placeholder)
global pred_label
pred_label = model.get_tensor_by_name(pred_label_placeholder)
global final_graph_layer
final_graph_layer = model.get_tensor_by_name(final_graph_layer_placeholder)
global loss
if loss_placeholder != None:
loss = model.get_tensor_by_name(loss_placeholder)
global adam_op
if adam_op_placeholder != None:
adam_op = model.get_tensor_by_name(adam_op_placeholder)
global actual_label
if actual_label_placeholder != None:
actual_label = model.get_tensor_by_name(actual_label_placeholder)
global correct_prediction
if correct_prediction_placeholder != None:
correct_prediction = model.get_tensor_by_name(correct_prediction_placeholder)
global accuracy
if accuracy_placeholder != None:
accuracy = model.get_tensor_by_name(accuracy_placeholder)
#EXAMPLE ON HOW TO LOAD THIS GRAPH w/ (minimal) user changes necessary
graph_loader(logdir='<MAIN_LOGGING_DIR>', path_to_checkpoint='<YOUR_PATH_TO_MODEL>.ckpt.meta', x_placeholder='x:0', y_placeholder='y_:0', pred_label_placeholder='pred_label:0', final_graph_layer_placeholder='ann:0',
keep_prob_placeholder='keep_prob:0', loss_placeholder='loss:0', adam_op_placeholder='adam_op:0', actual_label_placeholder='actual_label:0',
correct_prediction_placeholder='correct_prediction:0', accuracy_placeholder='accuracy:0')