forked from 0xTechSavvy/autoaugment
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmycontroller.py
135 lines (121 loc) · 5.28 KB
/
mycontroller.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
from keras import models, layers, backend, initializers
import tensorflow as tf
from transformations import get_transformations
import PIL.Image
import numpy as np
LSTM_UNITS = 100
SUBPOLICIES = 5
SUBPOLICY_OPS = 2
OP_TYPES = 16
OP_PROBS = 11
OP_MAGNITUDES = 10
class Operation:
def __init__(self, X, types_softmax, probs_softmax, magnitudes_softmax, argmax=False):
# Ekin Dogus says he sampled the softmaxes, and has not used argmax
# We might still want to use argmax=True for the last predictions, to ensure
# the best solutions are chosen and make it deterministic.
transformations = get_transformations(X)
if argmax:
self.type = types_softmax.argmax()
t = transformations[self.type]
self.prob = probs_softmax.argmax() / (OP_PROBS-1)
m = magnitudes_softmax.argmax() / (OP_MAGNITUDES-1)
self.magnitude = m*(t[2]-t[1]) + t[1]
else:
self.type = np.random.choice(OP_TYPES, p=types_softmax)
t = transformations[self.type]
self.prob = np.random.choice(np.linspace(0, 1, OP_PROBS), p=probs_softmax)
self.magnitude = np.random.choice(np.linspace(t[1], t[2], OP_MAGNITUDES), p=magnitudes_softmax)
self.transformation = t[0]
def __call__(self, X):
_X = []
for x in X:
if np.random.rand() < self.prob:
x = PIL.Image.fromarray(x)
x = self.transformation(x, self.magnitude)
_X.append(np.array(x))
return np.array(_X)
def __str__(self):
return 'Operation %2d (P=%.3f, M=%.3f)' % (self.type, self.prob, self.magnitude)
class Subpolicy:
def __init__(self, *operations):
self.operations = operations
def __call__(self, X):
for op in self.operations:
X = op(X)
return X
def __str__(self):
ret = ''
for i, op in enumerate(self.operations):
ret += str(op)
if i < len(self.operations)-1:
ret += '\n'
return ret
class Controller:
def __init__(self):
self.model = self.create_model()
self.scale = tf.placeholder(tf.float32, ())
self.grads = tf.gradients(self.model.outputs, self.model.trainable_weights)
# negative for gradient ascent
self.grads = [g * (-self.scale) for g in self.grads]
self.grads = zip(self.grads, self.model.trainable_weights)
self.optimizer = tf.train.GradientDescentOptimizer(0.00035).apply_gradients(self.grads)
def create_model(self):
# Implementation note: Keras requires an input. I create an input and then feed
# zeros to the network. Ugly, but it's the same as disabling those weights.
# Furthermore, Keras LSTM input=output, so we cannot produce more than SUBPOLICIES
# outputs. This is not desirable, since the paper produces 25 subpolicies in the
# end.
input_layer = layers.Input(shape=(SUBPOLICIES, 1))
init = initializers.RandomUniform(-0.1, 0.1)
lstm_layer = layers.LSTM(
LSTM_UNITS, recurrent_initializer=init, return_sequences=True,
name='controller')(input_layer)
outputs = []
for i in range(SUBPOLICY_OPS):
name = 'op%d-' % (i+1)
outputs += [
layers.Dense(OP_TYPES, activation='softmax', name=name + 't')(lstm_layer),
layers.Dense(OP_PROBS, activation='softmax', name=name + 'p')(lstm_layer),
layers.Dense(OP_MAGNITUDES, activation='softmax', name=name + 'm')(lstm_layer),
]
return models.Model(input_layer, outputs)
def fit(self, mem_softmaxes, mem_accuracies):
session = backend.get_session()
min_acc = np.min(mem_accuracies)
max_acc = np.max(mem_accuracies)
dummy_input = np.zeros((1, SUBPOLICIES, 1))
dict_input = {self.model.input: dummy_input}
# FIXME: the paper does mini-batches (10)
for softmaxes, acc in zip(mem_softmaxes, mem_accuracies):
scale = (acc-min_acc) / (max_acc-min_acc)
dict_outputs = {_output: s for _output, s in zip(self.model.outputs, softmaxes)}
dict_scales = {self.scale: scale}
session.run(self.optimizer, feed_dict={**dict_outputs, **dict_scales, **dict_input})
return self
def predict(self, size, X):
dummy_input = np.zeros((1, size, 1), np.float32)
softmaxes = self.model.predict(dummy_input)
# convert softmaxes into subpolicies
subpolicies = []
for i in range(SUBPOLICIES):
operations = []
for j in range(SUBPOLICY_OPS):
op = softmaxes[j*3:(j+1)*3]
op = [o[0, i, :] for o in op]
operations.append(Operation(X, *op))
subpolicies.append(Subpolicy(*operations))
return softmaxes, subpolicies
# generator
def autoaugment(subpolicies, X, y, child_batch_size):
while True:
ix = np.arange(len(X))
np.random.shuffle(ix)
for i in range(len(X) // child_batch_size):
_ix = ix[i*child_batch_size:(i+1)*child_batch_size]
_X = X[_ix]
_y = y[_ix]
subpolicy = np.random.choice(subpolicies)
_X = subpolicy(_X)
_X = _X.astype(np.float32) / 255
yield _X, _y