-
Notifications
You must be signed in to change notification settings - Fork 1
/
compress_model.py
345 lines (260 loc) · 12.2 KB
/
compress_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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
IMAGE_SIZE = (64, 64)
TRAIN_SIZE = 50000
VALIDATION_SIZE = 10000
BATCH_SIZE_PER_GPU = 512
global_batch_size = (BATCH_SIZE_PER_GPU * 1)
NUM_CLASSES = 10
TEST = 1
EPOCHS = 20 if TEST == 1 else 2
NUM_PROC = 2
EARLY_STOPPING = False
SUMMARY_PATH = ""
OG = None
ARCH = 'resnet'
MODEL_PATH = ""
AUG = True
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import math
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
for i in range(len(physical_devices)):
tf.config.experimental.set_memory_growth(physical_devices[i], True)
import tensorflow_datasets as tfds
import numpy as np
import math
import time
def train_layer(target, rank=0):
"""Trains a replacement layer given a target
Args:
target: A dictonary containing {'name': layer.name, 'layer': i}
Returns:
target: Updated dictonary
"""
layer_start = time.time()
dataset, info = tfds.load('cifar10', with_info=True)
options = tf.data.Options()
options.experimental_threading.max_intra_op_parallelism = 1
train = dataset['train'].with_options(options)
test = dataset['test'].with_options(options)
if AUG:
train = train.map(lambda x: load_image_train(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=4)
else:
train = train.map(lambda x: load_image_test(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=4)
train = train.cache()
train_dataset = train.shuffle(buffer_size=4000).batch(global_batch_size).repeat()
train_dataset = train_dataset.prefetch(buffer_size=2)
test_dataset = test.map(lambda x: load_image_test(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=4)
#test_dataset = test_dataset.cache()
test_dataset = test_dataset.batch(global_batch_size).repeat()
test_dataset = test_dataset.prefetch(buffer_size=2)
writer = tf.summary.create_file_writer(SUMMARY_PATH + f"{target['name']}")
with writer.as_default():
print(f"training layer {target['name']}")
tf.keras.backend.clear_session()
print("cleared backend")
model = tf.keras.models.load_model(MODEL_PATH)
print("model loaded")
in_layer = target['layer']
get_output = tf.keras.Model(inputs=model.input, outputs=[model.layers[in_layer - 1].output,
model.layers[in_layer].output])
replacement_layers = build_replacement(get_output, layers=2)
replacement_len = len(replacement_layers.layers)
layer_train_gen = LayerBatch(get_output, train_dataset, TRAIN_SIZE, global_batch_size)
layer_test_gen = LayerBatch(get_output, test_dataset, VALIDATION_SIZE, global_batch_size)
MSE = tf.losses.MeanSquaredError()
starting_lr = 2e-2
initial_learning_rate = 0.1
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
starting_lr,
decay_steps=3000,
decay_rate=0.96,
staircase=True)
optimizer=tf.keras.optimizers.RMSprop(lr_schedule)
replacement_layers.compile(loss=MSE, optimizer=optimizer)
target['score'] = (0, 0)
replacement_layers.save(f'/tmp/layer_{rank}.h5')
print('epochs started')
for epoch in range(EPOCHS):
if epoch % 2 == 0:
tf.keras.backend.clear_session()
model = tf.keras.models.load_model(MODEL_PATH)
in_layer = target['layer']
get_output = tf.keras.Model(inputs=model.input, outputs=[model.layers[in_layer - 1].output,
model.layers[in_layer].output])
layer_train_gen = LayerBatch(get_output, train_dataset, TRAIN_SIZE, global_batch_size)
layer_test_gen = LayerBatch(get_output, test_dataset, VALIDATION_SIZE, global_batch_size)
replacement_layers = tf.keras.models.load_model(f'/tmp/layer_{rank}.h5')
print('training started')
history = replacement_layers.fit(x=layer_train_gen,
epochs=1,
steps_per_epoch=math.ceil(TRAIN_SIZE / global_batch_size / TEST),
validation_data=layer_test_gen,
shuffle=False,
validation_steps=math.ceil(VALIDATION_SIZE / global_batch_size / TEST),
verbose=2)
tf.summary.scalar(name='rep_loss', data=history.history['loss'][0], step=epoch)
tf.summary.scalar(name='val_loss', data=history.history['val_loss'][0], step=epoch)
if epoch % 2 == 0:
replacement_layers.save(f'/tmp/layer_{rank}.h5')
weights = [replacement_layers.layers[1].get_weights(), replacement_layers.layers[3].get_weights()]
tf.keras.backend.clear_session()
model = tf.keras.models.load_model(MODEL_PATH)
layer_name = target['name']
layer_pos = target['layer']
filters = model.layers[layer_pos].output.shape[-1]
new_model = replace_layer(model, layer_name, lambda x: replac(x, filters))
new_model.layers[layer_pos].set_weights(weights[0])
new_model.layers[layer_pos + 2].set_weights(weights[1])
new_model.compile(optimizer=tf.keras.optimizers.SGD(.1), loss="categorical_crossentropy", metrics=['accuracy'])
score = new_model.evaluate(test_dataset, steps=math.ceil(VALIDATION_SIZE / global_batch_size / TEST))
if score[1] > target['score'][1]:
target['score'] = score
target['weights'] = weights
tf.summary.scalar(name='model_acc', data=score[1], step=epoch)
tf.summary.scalar(name='model_loss', data=score[0], step=epoch)
print(f"epoch: {epoch}, rep loss {history.history['loss']}, val loss {history.history['val_loss']}, model acc {score[1]}")
if EARLY_STOPPING:
if np.abs(OG[1] - target['score'][1] < 0.002):
print('stoping early')
break
writer.flush()
layer_end = time.time()
layer_time = layer_end - layer_start
target['run_time'] = layer_time
target['rank'] = rank
return target
if __name__ == '__main__':
import json
import functools
import operator
import tensorflow_datasets as tfds
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-im", "--image_size", type=int,
help="dataset image size", default=32)
parser.add_argument("-ts", "--train_size", type=int,
help="dataset training split size", default=50000)
parser.add_argument("-vs", "--val_size", type=int,
help="dataset validation split size", default=10000)
parser.add_argument("-bs", "--batch_size", type=int,
help="batch size", default=256)
parser.add_argument("-nc", "--num_classes", type=int, default=10)
parser.add_argument("-ep", "--epochs", type=int, default=40)
parser.add_argument("-es", "--early_stopping", type=bool, default=False)
parser.add_argument("-tm", "--test_multiplier", type=int, default=1,
help="multipler to speed up training when testing")
parser.add_argument("-sp", "--summary_path", type=str, default="./summarys/vgg/")
parser.add_argument("-tp", "--timing_path", type=str, help="file name and path for saving timing data")
parser.add_argument("-ar", "--arch", type=str,
help="model architecture being compressed ex. vgg, resnet",
choices=['vgg', 'resnet'], default='resnet')
parser.add_argument("-mp", "--model_path", type=str, help="file path to saved model file", default='cifar10.h5')
parser.add_argument('-aug', "--augment_data", type=bool, default=True, help="Whether or not to augement images or cache them")
args = parser.parse_args()
IMAGE_SIZE = (args.image_size, args.image_size)
TRAIN_SIZE = args.train_size
VALIDATION_SIZE = args.val_size
global_batch_size = args.batch_size
NUM_CLASSES = args.num_classes
EPOCHS = args.epochs
EARLY_STOPPING = args.early_stopping
TEST = args.test_multiplier
SUMMARY_PATH = args.summary_path
timing_path = args.timing_path
ARCH = args.arch
MODEL_PATH = args.model_path
AUG = args.augment_data
if ARCH == 'resnet':
from utils_resnet import *
elif ARCH == 'vgg':
from utils import *
# with open('targets.json', 'r') as f:
# targets = json.load(f)
#need the dataset file to be loaded before training
dataset, info = tfds.load('cifar10', with_info=True)
test_dataset = dataset['test'].map(lambda x: load_image_test(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=tf.data.experimental.AUTOTUNE)
test_dataset = test_dataset.batch(global_batch_size).repeat()
test_dataset = test_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
model = tf.keras.models.load_model(MODEL_PATH)
import pprint
targets = []
for i, layer in enumerate(model.layers):
if layer.__class__.__name__ == "Conv2D":
if layer.kernel_size[0] == 3:
#print(f'{i} layer {layer.name} , kernel size {layer.kernel_size}')
targets.append({'name': layer.name, 'layer': i})
pprint.pprint(targets)
model.compile(optimizer=tf.optimizers.SGD(learning_rate=.01, momentum=.9, nesterov=True), loss='mse', metrics=['acc'])
OG = model.evaluate(test_dataset, steps=math.ceil(VALIDATION_SIZE/global_batch_size/TEST))
del model
tf.keras.backend.clear_session()
tik = time.time()
targets = [train_layer(targets[i]) for i in range(len(targets))]
tok = time.time()
total_time = tok - tik
list.sort(targets, key=lambda target: target['layer'])
tf.keras.backend.clear_session()
model = tf.keras.models.load_model(MODEL_PATH)
writer = tf.summary.create_file_writer(SUMMARY_PATH + "final_model")
with writer.as_default():
for target in targets[::-1]:
if OG[1] - target['score'][1] < 0.02:
print(f'replacing layer {target["name"]}')
layer_name = target['name']
layer_pos = target['layer']
filters = model.layers[layer_pos].output.shape[-1]
new_model = replace_layer(model, layer_name, lambda x: replac(x, filters))
new_model.layers[layer_pos].set_weights(target['weights'][0])
new_model.layers[layer_pos + 2].set_weights(target['weights'][1])
new_model.save('cifar10_resnet_modified.h5')
tf.keras.backend.clear_session()
model = tf.keras.models.load_model('cifar10_resnet_modified.h5')
tf.keras.backend.clear_session()
test_dataset = dataset['test'].map(lambda x: load_image_test(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=tf.data.experimental.AUTOTUNE)
test_dataset = test_dataset.batch(global_batch_size).repeat()
test_dataset = test_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
train = dataset['train']
if AUG:
train = train.map(lambda x: load_image_train(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=4)
else:
train = train.map(lambda x: load_image_test(x, IMAGE_SIZE, NUM_CLASSES), num_parallel_calls=4)
train = train.cache()
train_dataset = train.shuffle(buffer_size=4000).batch(global_batch_size).repeat()
train_dataset = train_dataset.prefetch(buffer_size=2)
fine_tune_epochs = 40
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
.01,
decay_steps= math.ceil(TRAIN_SIZE / global_batch_size / TEST ) * fine_tune_epochs // 3,
decay_rate=0.96,
staircase=False)
from tensorflow.keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint('cifar10_resnet_modified_fine_tune.h5', monitor='val_accuracy', verbose=1, save_best_only=True)
model = tf.keras.models.load_model('cifar10_resnet_modified.h5')
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=lr_schedule, momentum=.9, nesterov=True), loss="categorical_crossentropy", metrics=['accuracy'])
final = model.evaluate(test_dataset, steps=math.ceil(VALIDATION_SIZE / global_batch_size / TEST))
fine_tune = model.fit(
x=train_dataset,
epochs=fine_tune_epochs,
steps_per_epoch=math.ceil(TRAIN_SIZE / global_batch_size / TEST),
validation_data=test_dataset,
shuffle=False,
validation_steps=math.ceil(VALIDATION_SIZE / global_batch_size / TEST),
verbose=1,
callbacks=[checkpoint])
model = tf.keras.models.load_model('cifar10_resnet_modified_fine_tune.h5')
final_fine_tune = model.evaluate(test_dataset, steps=math.ceil(VALIDATION_SIZE / global_batch_size / TEST))
new_model.save('cifar10_resnet_modified_fine_tune.h5')
tf.summary.scalar(name='model_acc', data=final[1], step=0)
tf.summary.scalar(name='model_loss', data=final[0], step=0)
tf.summary.scalar(name='model_acc_fine_tune', data=final_fine_tune[1], step=0)
tf.summary.scalar(name='model_loss_fine_tune', data=final_fine_tune[0], step=0)
if timing_path is not None:
timing_dump = [{'name': target['name'], 'layer': target['layer'], 'run_time': target['run_time'], 'rank': target['rank']} for target in targets]
timing_dump.append({'total_time': total_time})
timing_dump.append({'final_acc': final[1]})
timing_dump.append({'fine_tune_acc': final_fine_tune[1]})
with open(timing_path, 'w') as f:
json.dump(timing_dump, f, indent='\t')
writer.flush()