This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
read_model.py
executable file
·73 lines (61 loc) · 2.51 KB
/
read_model.py
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
#!/usr/bin/python
import sys
sys.dont_write_bytecode = True
import tensorflow as tf
import papl
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument("-m", "--model", required=True, help="Specify serialized input model")
argparser.add_argument("-r", "--ratio", help="Specify ratio")
args = argparser.parse_args()
def read_model_obj_with_sorted_ratio(fname, ratio):
saver = tf.train.Saver()
saver.restore(sess, fname)
print str(ratio*100)+" %"
target_obj_list = [weights[elem] for elem in papl.config.target_all_layer]
for elem in target_obj_list:
arr = elem.eval()
arr = list(arr.reshape(arr.size))
arr.sort(cmp=lambda x,y:cmp(abs(x), abs(y)))
print "\""+elem.name[:-2]+"\": ", abs(arr[int(len(arr)*ratio)-1]), ","
def print_raw_matrix(fname):
saver = tf.train.Saver()
saver.restore(sess, fname)
import numpy as np
np.save("w_fc1.raw", weights["w_fc1"].eval())
np.save("w_fc2.raw", weights["w_fc2"].eval())
def read_model_obj(fname):
saver = tf.train.Saver()
import os.path
try:
assert os.path.isfile(fname)
saver.restore(sess, fname)
switcher = {
"model_ckpt_dense": papl.config.target_dat,
"model_ckpt_dense_pruned": papl.config.target_p_dat,
"model_ckpt_dense_retrained": papl.config.target_tp_dat
}
papl.print_weight_vars(weights, papl.config.target_all_layer, switcher.get(args.model))
except AssertionError:
print "Warning: No such files or directory\n"
pass
except:
import sys
print "Unexpected error:", sys.exc_info()[0]
weights = {
"w_conv1": tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name="w_conv1"),
"b_conv1": tf.Variable(tf.constant(0.1, shape=[32]), name="b_conv1"),
"w_conv2": tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1), name="w_conv2"),
"b_conv2": tf.Variable(tf.constant(0.1, shape=[64]), name="b_conv2"),
"w_fc1": tf.Variable(tf.truncated_normal([7*7*64, 1024], stddev=0.1), name="w_fc1"),
"b_fc1": tf.Variable(tf.constant(0.1, shape=[1024]), name="b_fc1"),
"w_fc2": tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name="w_fc2"),
"b_fc2": tf.Variable(tf.constant(0.1, shape=[10]), name="b_fc2")
}
sess = tf.InteractiveSession()
if __name__ == "__main__":
if bool(args.ratio) == False:
read_model_obj(args.model)
else:
read_model_obj_with_sorted_ratio(args.model, float(args.ratio))
# print_raw_matrix(args.model)