-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
357 lines (296 loc) · 14.3 KB
/
classes.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
346
347
348
349
350
351
352
353
354
355
356
357
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import functools
import json
import os
import shutil
# import re
import tensorflow.compat.v1 as tf
import adanet
from saep.subnetwork_generator import Subnetwork as PrunedSubSubnetwork
from saep.subnetwork_generator import Generator as PrunedSubGenerator
from saep.subnetwork_generator import Builder as PrunedSubBuilder
# --------------------------------------
_NUM_LAYERS_KEY = "num_layers"
class SimpleCNNBuilder(adanet.subnetwork.Builder):
"""Builds a CNN subnetwork for AdaNet."""
def __init__(self, learning_rate, max_iteration_steps, seed):
"""Initializes a `SimpleCNNBuilder`.
Args:
learning_rate: The float learning rate to use.
max_iteration_steps: The number of steps per iteration.
seed: The random seed.
Returns:
An instance of `SimpleCNNBuilder`.
"""
self._learning_rate = learning_rate
self._max_iteration_steps = max_iteration_steps
self._seed = seed
def build_subnetwork(self, features, logits_dimension, training,
iteration_step, summary, previous_ensemble=None):
"""See `adanet.subnetwork.Builder`."""
images = list(features.values())[0]
# Visualize some of the input images in TensorBoard.
summary.image("images", images)
# input : [?, hw,hw, 3] # 32 or 28
# output: [?, hw,hw,16] -> [?, hw/2,hw/2,16] -> [?, hw**2/4 *16=4*hw**2]
kernel_initializer = tf.keras.initializers.he_normal(seed=self._seed)
x = tf.keras.layers.Conv2D(filters=16, kernel_size=3,
padding="same", activation="relu",
kernel_initializer=kernel_initializer)(images)
x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(units=64, activation="relu",
kernel_initializer=kernel_initializer)(x)
# output: [?, 64] -> [?, 10]
# The `Head` passed to adanet.Estimator will apply the softmax activation.
logits = tf.keras.layers.Dense(units=10, activation=None,
kernel_initializer=kernel_initializer)(x)
# Use a constant complexity measure, since all subnetworks have the same
# architecture and hyperparameters.
complexity = tf.constant(1)
return adanet.Subnetwork(last_layer=x, logits=logits,
complexity=complexity, persisted_tensors={})
def build_subnetwork_train_op(self, subnetwork, loss, var_list, labels,
iteration_step, summary, previous_ensemble=None):
"""See `adanet.subnetwork.Builder`."""
# Momentum optimizer with cosine learning rate decay works well with CNNs.
learning_rate = tf.train.cosine_decay(learning_rate=self._learning_rate,
global_step=iteration_step,
decay_steps=self._max_iteration_steps)
optimizer = tf.train.MomentumOptimizer(learning_rate, .9)
# NOTE: The `adanet.Estimator` increments the global step.
return optimizer.minimize(loss=loss, var_list=var_list)
def build_mixture_weights_train_op(self, loss, var_list, logits, labels,
iteration_step, summary):
"""See `adanet.subnetwork.Builder`."""
return tf.no_op("mixture_weights_train_op")
@property
def name(self):
"""See `adanet.subnetwork.Builder`."""
return "simple_cnn" # self._name
class SimpleCNNGenerator(adanet.subnetwork.Generator):
"""Generates a `SimpleCNN` at each iteration."""
def __init__(self, learning_rate, max_iteration_steps, seed=None):
"""Initializes a `Generator` that builds `SimpleCNNs`.
Args:
learning_rate: The float learning rate to use.
max_iteration_steps: The number of steps per iteration.
seed: The random seed.
Returns:
An instance of `Generator`.
"""
self._seed = seed
self._dnn_builder_fn = functools.partial(
SimpleCNNBuilder,
learning_rate=learning_rate,
max_iteration_steps=max_iteration_steps)
def generate_candidates(self, previous_ensemble, iteration_number,
previous_ensemble_reports, all_reports):
"""See `adanet.subnetwork.Generator`."""
seed = self._seed
# Change the seed according to the iteration so that each subnetwork
# learns something different.
if seed is not None:
seed += iteration_number
return [self._dnn_builder_fn(seed=seed)]
# ===============================
# Self-defined
class PyFile(object):
def find_architecture(self, filename, srcpath, logger=None):
srcfile = os.path.join(srcpath, filename)
if not os.path.exists(srcfile):
if logger:
logger.info("")
logger.info(" srcpath: {}".format(srcpath))
logger.info("No such file: {}".format(filename))
else:
print("\nNo such file: {}".format(srcfile))
# ls | grep architecture
archs = os.listdir(srcpath)
archs = list(filter(lambda x: 'architecture' in x, archs))
# archs = list(filter(lambda x: re.search(r'architecture', x), archs))
archs = sorted(archs)
nums = [i.split('-')[1] for i in archs]
nums = [i.split('.')[0] for i in nums]
nums = sorted(int(i) for i in nums)
if logger:
logger.info("Last arch is: architecture-{}.json".format(nums[-1]))
else:
print("Last arch is: architecture-{}.json".format(nums[-1]))
return 'architecture-{}.json'.format(nums[-1])
return filename
def copy_architecture(self, filename, srcpath, dstpath='./',
dstname='', logger=None):
srcfile = os.path.join(srcpath, filename)
dstfile = os.path.join(dstpath, dstname + filename)
if not os.path.exists(srcfile) and logger:
logger.info("No such file: {}".format(srcfile))
elif not os.path.exists(srcfile):
print("No such file: {}".format(srcfile))
shutil.copyfile(srcfile, dstfile)
if logger:
logger.info("Copy architecture-?.json")
logger.info("\tSrc path: {}".format(srcfile))
logger.info("\tDst path: {}".format(dstfile))
else:
print("Copy architecture-?.json")
print("\tSrc path: {}".format(srcfile))
print("\tDst path: {}".format(dstfile))
return
def read_architecture(self, filename, dstpath='./'):
dstfile = os.path.join(dstpath, filename)
with open(dstfile, "r") as dstload:
dstdict = json.load(dstload)
return dstdict
# --------------------------------------
class PrunedCNNBuilder(PrunedSubBuilder):
def __init__(self, learning_rate, max_iteration_steps, seed,
learn_mixture_weights=False):
self._learning_rate = learning_rate
self._max_iteration_steps = max_iteration_steps
self._seed = seed
self._learn_mixture_weights = learn_mixture_weights
def build_subnetwork(self, features, logits_dimension, training,
iteration_step, summary, previous_ensemble=None):
"""See `adanet.subnetwork.Builder`."""
images = list(features.values())[0]
summary.image("images", images)
kernel_initializer = tf.keras.initializers.he_normal(seed=self._seed)
x = tf.keras.layers.Conv2D(filters=16, kernel_size=3,
padding="same", activation="relu",
kernel_initializer=kernel_initializer)(images)
x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(units=64, activation="relu",
kernel_initializer=kernel_initializer)(x)
logits = tf.keras.layers.Dense(units=10, activation=None,
kernel_initializer=kernel_initializer)(x)
complexity = tf.constant(1)
return PrunedSubSubnetwork(last_layer=x, logits=logits,
complexity=complexity, persisted_tensors={})
def build_subnetwork_train_op(self, subnetwork, loss, var_list, labels,
iteration_step, summary, previous_ensemble=None):
"""See `adanet.subnetwork.Builder`."""
learning_rate = tf.train.cosine_decay(learning_rate=self._learning_rate,
global_step=iteration_step,
decay_steps=self._max_iteration_steps)
self._optimizer = tf.train.MomentumOptimizer(learning_rate, .9)
return self._optimizer.minimize(loss=loss, var_list=var_list)
def build_mixture_weights_train_op(self, loss, var_list, logits, labels,
iteration_step, summary):
"""See `adanet.subnetwork.Builder`."""
if not self._learn_mixture_weights:
return tf.no_op("mixture_weights_train_op")
return self._optimizer.minimize(loss=loss, var_list=var_list)
@property
def name(self):
"""See `adanet.subnetwork.Builder`."""
return "pruned_cnn" # self._name
class PrunedCNNGenerator(PrunedSubGenerator):
def __init__(self, learning_rate, max_iteration_steps, seed=None, learn_mixture_weights=False):
self._seed = seed
self._dnn_builder_fn = functools.partial(
PrunedCNNBuilder, learning_rate=learning_rate,
max_iteration_steps=max_iteration_steps,
learn_mixture_weights=learn_mixture_weights)
def generate_candidates(self, previous_ensemble, iteration_number,
previous_ensemble_reports, all_reports):
seed = self._seed
if seed is not None:
seed += iteration_number
return [self._dnn_builder_fn(seed=seed)]
# --------------------------------------
"""
class ComplexCNNBuilder(PrunedSubBuilder):
def __init__(self, learning_rate, max_iteration_steps, seed, learn_mixture_weights=False):
self._learning_rate = learning_rate
self._max_iteration_steps = max_iteration_steps
self._seed = seed
self._learn_mixture_weights = learn_mixture_weights
def build_subnetwork(self, features, logits_dimension, training,
iteration_step, summary, previous_ensemble=None):
images = list(features.values())[0]
summary.image("images", images)
kernel_initializer = tf.keras.initializers.he_normal(seed=self._seed)
x = tf.keras.layers.Conv2D(filters=16, kernel_size=3,
padding="same", activation="relu",
kernel_initializer=kernel_initializer)(images)
x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2)(x)
# input : [?, hw, hw, 3] # 32 or 28
# output: [?, hw, hw, 16] # [?, hw/2, hw/2, 16]
x = tf.keras.layers.Conv2D(
filters=128, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/2, .., 64]
x = tf.keras.layers.Conv2D(
filters=256, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/2, .., 128]
x = tf.keras.layers.Conv2D(
filters=512, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/2, .., 256]
x = tf.keras.layers.MaxPool2D(
pool_size=2, strides=2)(x) # [?, hw/4, .., 256] i.e., 16*hw**2
x = tf.keras.layers.Conv2D(
filters=512, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/4, .., 128]
x = tf.keras.layers.Conv2D(
filters=1024, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/4, .., 256]
x = tf.keras.layers.Conv2D(
filters=2048, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/4, .., 512]
x = tf.keras.layers.MaxPool2D(
pool_size=2, strides=2)(x) # [?, hw/4, .., 512] i.e., 32
x = tf.keras.layers.Conv2D(
filters=2048, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/4, .., 512]
x = tf.keras.layers.Conv2D(
filters=4096, kernel_size=3, padding="same", activation="relu",
kernel_initializer=kernel_initializer)(x) # [?, hw/4, .., 512]
x = tf.keras.layers.MaxPool2D(
pool_size=2, strides=2)(x) # [?, hw/8, .., 1024]
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(units=1024, activation="relu",
kernel_initializer=kernel_initializer)(x)
x = tf.keras.layers.Dense(units=512, activation="relu",
kernel_initializer=kernel_initializer)(x)
# output: [?, hw**2/16 *64 = 4*hw**2] -> [?, 64]
logits = tf.keras.layers.Dense(units=logits_dimension, activation=None,
kernel_initializer=kernel_initializer)(x)
complexity = tf.constant(1)
return PrunedSubSubnetwork(last_layer=x, logits=logits,
complexity=complexity,
persisted_tensors={})
def build_subnetwork_train_op(self, subnetwork, loss, var_list, labels,
iteration_step, summary,
previous_ensemble=None):
learning_rate = tf.train.cosine_decay(
learning_rate=self._learning_rate,
global_step=iteration_step,
decay_steps=self._max_iteration_steps)
self._optimizer = tf.train.MomentumOptimizer(learning_rate, .9)
return self._optimizer.minimize(loss=loss, var_list=var_list)
def build_mixture_weights_train_op(self, loss, var_list, logits, labels,
iteration_step, summary):
if not self._learn_mixture_weights:
return tf.no_op("mixture_weights_train_op")
return self._optimizer.minimize(loss=loss, var_list=var_list)
@property
def name(self):
return "pruned_cpx" # complex cnn
class ComplexCNNGenerator(PrunedSubGenerator):
def __init__(self, learning_rate, max_iteration_steps, seed=None, learn_mixture_weights=False):
self._seed = seed
self._dnn_builder_fn = functools.partial(
ComplexCNNBuilder, learning_rate=learning_rate,
max_iteration_steps=max_iteration_steps,
learn_mixture_weights=learn_mixture_weights)
def generate_candidates(self, previous_ensemble, iteration_number,
previous_ensemble_reports, all_reports):
seed = self._seed
if seed is not None:
seed += iteration_number
return [self._dnn_builder_fn(seed=seed)]
"""