-
Notifications
You must be signed in to change notification settings - Fork 11
/
channels_pruning.py
188 lines (149 loc) · 7.97 KB
/
channels_pruning.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#coding:utf-8
'''
author:Wang Haibo
at: Pingan Tec.
email: haibo.david@qq.com
!!!
代码中会有少量中文注释,无需在意
'''
import numpy as np
import os
import tensorflow as tf
import time
from tensorflow.python.framework import graph_util
from modelsets import CifarModelZoo
from datasets import CifarData
from utils.drawCurve import drawLib
from modelBuilder import test_full_model
from utils import configs as cfg
# 文件存放目录
CIFAR_DIR = "./cifar-10-python"
if __name__ == "__main__":
drawer = drawLib()
train_filename = [os.path.join(CIFAR_DIR, 'data_batch_%d' % i) for i in range(1, 6)]
test_filename = [os.path.join(CIFAR_DIR, 'test_batch')]
train_data = CifarData(train_filename, True)
test_data = CifarData(test_filename, True)
train_batch_size = cfg.TRAIN_BATCHSIZE
train_batches = train_data._num_examples // train_batch_size
test_batch_size = cfg.TEST_BATCHSIZE
test_batches = test_data._num_examples // test_batch_size
retrain_epoches = cfg.RETRAIN_EPOCH
test_prune_batches = cfg.TEST_PRUNE_BATCHES #剪枝前的测试组数
dropout_time_threshold = cfg.DROPOUT_TIME_THRESHOLD #舍弃前多少组的时间
l2_loss_decay = cfg.L2_LOSS_DECAY
prune_rate = cfg.PRUNING_RATE
full_train = cfg.FULL_TRAIN
model_name = cfg.MODEL_NAME
# ----------------test full model--------------- #
test_full_model()
# -----------------------------------------------#
# ----------------get pruned wieghts and shapes.--------------- #
print("[INFO]: start prune channels.")
input_x = tf.placeholder(tf.float32, [None, 32, 32, 3], name="input0")
input_y = tf.placeholder(tf.int64, [None], name="input1")
is_train = tf.placeholder(tf.bool, [], name="is_training")
params = {"inputs":input_x,"is_train":is_train,"reload_w":None,"num_classes":cfg.NUM_CLASSES}
logits, model = CifarModelZoo.getModel(model_name,params)
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"./ckpt_model/full_model.ckpt")
model._prune_channels(sess,"rate",prune_rate)
print("[INFO]: channels have been pruned.")
# -------------------------------------------------------------- #
tf.reset_default_graph()
# ---------------------reconstruction network---------------------#
print("[INFO]: start reconstruction network.")
input_x = tf.placeholder(tf.float32, [None, 32, 32, 3], name="input0")
input_y = tf.placeholder(tf.int64, [None], name="input1")
is_train = tf.placeholder(tf.bool, [], name="is_training")
params = {"inputs":input_x,"is_train":is_train,"reload_w":"./weights_data/shapes_"+str(prune_rate)+".pkl","num_classes":cfg.NUM_CLASSES}
logits, model = CifarModelZoo.getModel(model_name,params)
softmax_out = tf.nn.softmax(logits, name="softmax") # sparse_softmax_cross_entropy_with_logits
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
loss = tf.reduce_mean(tf.losses.sparse_softmax_cross_entropy(logits=logits, labels=input_y))
l2_loss = l2_loss_decay*tf.add_n([tf.nn.l2_loss(tf.cast(v,tf.float32))for v in tf.trainable_variables()])
loss += l2_loss
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
optimizer = tf.train.AdamOptimizer(cfg.INIT_LR)
capped_gvs = optimizer.compute_gradients(loss)
# capped_gvs = [(tf.clip_by_value(grad, -5e+3, 5e+3), var) for grad, var in gvs]
train_step = optimizer.apply_gradients(capped_gvs)
correct_prediction = tf.equal(tf.argmax(softmax_out, 1), input_y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
if not full_train:
model.restore_w(sess,"./weights_data/weights_"+str(prune_rate)+".pkl")
else:
print("fullly train the pruned model.")
# test before pruning
all_test_acc_val = []
t_before = []
all_test_loss_val = []
for j in range(test_prune_batches):
test_batch_data, test_batch_labels = test_data.next_batch(test_batch_size)
t_start = time.time()
test_loss_val,test_acc_val = sess.run([loss,accuracy],
feed_dict={input_x: test_batch_data, input_y: test_batch_labels,
is_train: False})
t_before.append(float(time.time() - t_start) * 1000.)
all_test_loss_val.append(test_loss_val)
all_test_acc_val.append(test_acc_val)
print("[before retrain] , acc:", round(np.mean(all_test_acc_val[dropout_time_threshold:]),4),
" avg time/ms:", round(np.mean(t_before),4),
"loss:",round(np.mean(all_test_loss_val),4))
# start retrain
for epoch in range(retrain_epoches):
for i in range(train_batches):
batch_data, batch_labels = train_data.next_batch(train_batch_size)
start_time = time.time()
sess.run(train_step,feed_dict={input_x: batch_data, input_y: batch_labels,is_train:True})
step_time = time.time()-start_time
if (i + 1) % cfg.PRINT_TRAIN_INFO_PER_STEP == 0:
loss_val,acc_val = sess.run([loss,accuracy],feed_dict={input_x: batch_data, input_y: batch_labels,is_train:False})
print('[Retrain] Epoch: %d, Step: %d, loss: %4.5f, acc: %4.5f, time:%4.3fms/sample'
% (epoch+1,i + 1, loss_val, acc_val, (step_time*1000.)/(cfg.PRINT_TRAIN_INFO_PER_STEP*cfg.TRAIN_BATCHSIZE)))
drawer.drawPts(acc_val,0)
drawer.drawPts(loss_val,1,5.0)
drawer.update()
if (i+1)% cfg.TEST_PER_STEP == 0:
all_test_acc_val = []
all_test_loss_val = []
for j in range(test_batches):
test_batch_data, test_batch_labels = test_data.next_batch(test_batch_size)
test_loss_val,test_acc_val = sess.run([loss,accuracy],
feed_dict={input_x: test_batch_data, input_y: test_batch_labels,is_train:False})
all_test_acc_val.append(test_acc_val)
all_test_loss_val.append(test_loss_val)
test_acc = np.mean(all_test_acc_val)
test_loss = np.mean(all_test_loss_val)
print('[Test] loss: %4.5f, acc: %4.5f' % (test_loss, test_acc))
drawer.drawPts(test_acc,2,5.0)
drawer.drawPts(test_loss,3,5.0)
drawer.update()
drawer.save("./logs/channels_pruned.png")
# test before pruning
all_test_acc_val = []
t_before = []
all_test_loss_val = []
for j in range(test_prune_batches):
test_batch_data, test_batch_labels = test_data.next_batch(test_batch_size)
t_start = time.time()
test_loss_val,test_acc_val = sess.run([loss,accuracy],
feed_dict={input_x: test_batch_data, input_y: test_batch_labels,
is_train: False})
t_before.append(float(time.time() - t_start) * 1000.)
all_test_loss_val.append(test_loss_val)
all_test_acc_val.append(test_acc_val)
print("[after retrain] , acc:", round(np.mean(all_test_acc_val[dropout_time_threshold:]),4),
" avg time/ms:", round(np.mean(t_before),4),
"loss:",round(np.mean(all_test_loss_val),4))
saver.save(sess, "./channels_pruned_model/channels_pruned_model.ckpt")
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ["softmax"])
with tf.gfile.FastGFile("./channels_pruned_model/channels_pruned_model.pb", 'wb') as f:
f.write(constant_graph.SerializeToString())
print("model has saved...")