-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto_tune.py
286 lines (227 loc) · 9.2 KB
/
auto_tune.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
#
# Automatically create the architecture and train the network. Can then be
# evaluated using "evaluate.py".
#
try:
# If we are running in a multi node multi gpu setup. Otherwise run
# with tensorflow defaults
import cluster_setup
except ImportError:
pass
import gc
import io
import os
import time
import argparse
import json
import csv
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_addons as tfa
from models.factory import create_model
from data.factory import load_dataset
import conflicting_bundle as cb
from config import get_config, save_config
config = get_config()
# Only one run as we want to see how auto-tune creates different architectures.
# Therefore auto_tune should be called multiple times with different log dirs
config.runs = 1
def prune_model(model, train_ds):
""" If the bundle entropy is larger than zero, the first conflicting layer
and all subsequent layers of the same block type (a, b, c or d) are
removed from the architecture.
"""
print("Start pruning of architecture...", flush=True)
conflicts = cb.bundle_entropy(
model, train_ds,
config.batch_size, config.learning_rate,
config.num_classes, config.conflicting_samples_size,
all_layers=True)
layer = -1
pruned = False
for block_type in range(len(config.pruned_layers)):
new_layers = 0
for block_layer in range(config.pruned_layers[block_type]):
layer += 1
bundle_entropy_of_layer = conflicts[layer][1]
if bundle_entropy_of_layer <= 0:
new_layers += 1
else:
pruned = True
config.pruned_layers[block_type] = new_layers
if pruned:
save_config(config)
return
def train(train_ds, test_ds, train_writer, test_writer, log_dir_run):
# Initialize
strategy = tf.distribute.MirroredStrategy()
num_replicas = strategy.num_replicas_in_sync
with strategy.scope():
model = create_model(config)
radam=tfa.optimizers.RectifiedAdam(
learning_rate=config.learning_rate,
epsilon=1e-6, weight_decay=1e-2
)
optimizer = tfa.optimizers.Lookahead(radam)
train_ds = strategy.experimental_distribute_dataset(train_ds)
test_ds = strategy.experimental_distribute_dataset(test_ds)
with strategy.scope():
loss_fun = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True,
reduction=tf.keras.losses.Reduction.NONE)
def compute_loss(y, pred):
per_example_loss = loss_fun(y, pred)
loss = tf.nn.compute_average_loss(
per_example_loss,
global_batch_size=config.batch_size)
return loss
with strategy.scope():
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
def reset_train_metrics():
train_accuracy.reset_states()
train_loss.reset_states()
def reset_test_metrics():
test_accuracy.reset_states()
test_loss.reset_states()
reset_train_metrics()
reset_test_metrics()
with strategy.scope():
# Train and test step functions
def train_step(x, y):
with tf.GradientTape() as tape:
pred = model(x, training=True)
loss = compute_loss(y, pred)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
train_accuracy.update_state(y, pred)
train_loss.update_state(loss)
def test_step(x, y):
pred = model(x, training=False)
loss = compute_loss(y, pred)
test_accuracy.update_state(y, pred)
test_loss.update_state(loss)
with strategy.scope():
@tf.function
def distributed_train_step(x, y):
strategy.run(train_step, args=(x, y,))
@tf.function
def distributed_test_step(x, y):
strategy.run(test_step, args=(x, y,))
#
# TRAINING LOOP
#
epoch = 0
while True:
if epoch > config.epochs:
break
is_last_epoch = epoch >= config.epochs-1
print("", flush=True)
model.save_weights("%s/ckpt-%d" % (log_dir_run, epoch))
#
# Test
#
if epoch % 5 == 0 or is_last_epoch:
for x, y in test_ds:
start = time.time()
distributed_test_step(x, y)
with test_writer.as_default():
log_tensorboard("TEST", start, test_accuracy,
epoch, test_loss, model, x)
reset_test_metrics()
#
# Train
#
if not is_last_epoch:
for x, y in train_ds:
start = time.time()
distributed_train_step(x, y)
with train_writer.as_default():
log_tensorboard("TRAIN", start, train_accuracy, epoch,
train_loss, model, x)
reset_train_metrics()
train_writer.flush()
# In the previous experiments we have seen that
# conflicts occur after the third epoch. If we already
# trained the network for 10 epochs without any conflicts,
# we continue without checking conflicts because we have seen
# in the experiments that after 10 epoch conflicts rarly occur.
if epoch < 10:
# We evaluate the conflicting layer only if we have
# conflicts at a^{(L)} as its computationally cheaper to
# evaluate only the last layer rather than all layers
config.all_conflict_layers = False
print("Checking conflicts...")
conflicts = cb.bundle_entropy(
model, train_ds,
config.batch_size, config.learning_rate,
config.num_classes, config.conflicting_samples_size,
all_layers=False)
print("Entropy: %.5f" % conflicts[-1][1])
if conflicts[-1][1] > 0:
# Our model has conflicts, so lets autotune our model
# "The training is then restarted with the new pruned architecture."
print("Found conflicting layers.", flush=True)
prune_model(model, train_ds)
return False
epoch += 1
return True
def log_tensorboard(name, start, accuracy, epoch, loss, model, x):
accuracy_val = accuracy.result().numpy()
loss_val = loss.result().numpy()
print("[%s] Epoch %d (%d): Loss %.7f ; Accuracy %.4f; Time/step %.3f" % (
name, epoch, epoch, loss_val,
accuracy_val, time.time() - start), flush=True)
tf.summary.scalar("Accuracy", accuracy_val, step=epoch)
tf.summary.scalar("Loss", loss_val, step=epoch)
# Log some images (use only gpu 0)
x = x.values[0] if config.num_gpus > 1 else x
tf.summary.image("Input data", x, step=epoch, max_outputs=3,)
#
# M A I N
#
def main():
global config
print("\n\n####################", flush=True)
print("# AUTO TUNE %s" % config.log_dir, flush=True)
print("####################\n", flush=True)
train_csv = []
test_csv = []
# "First, the largest network from fig. 1 (120 layer) is trained without
# residual connections."
config.model = "vgg"
config.pruned_layers = [3,12,41,3]
config.use_residual = False
config.conflicting_samples_size = 64
log_dir_run = "%s/0" % (config.log_dir)
# Log some things
if not os.path.exists(config.log_dir):
os.makedirs(config.log_dir)
train_writer = tf.summary.create_file_writer("%s/train" % log_dir_run)
test_writer = tf.summary.create_file_writer("%s/test" % log_dir_run)
# Load dataset
train_ds, test_ds = load_dataset(config, augment=True)
# "[...] This process is repeated until no conflicting layer can be found and the
# network is successfully trained without conflicting bundles for 120 epochs."
trained = False
while not trained:
print("###################################################")
print("# Train model with blocks %s" % config.pruned_layers)
print("###################################################")
# Write hyperparameters, therefore in TensorBoard all
# network updates are logged.
with train_writer.as_default():
params = vars(config)
text = "|Parameter|Value| \n"
text += "|---------|-----| \n"
for val in params:
text += "|%s|%s| \n" % (val, params[val])
tf.summary.text("Hyperparameters", text, step=0)
train_writer.flush()
# Try to train the model
trained = train(train_ds, test_ds, train_writer, test_writer, log_dir_run)
if __name__ == '__main__':
main()