-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval_cnn.py
264 lines (180 loc) · 5.7 KB
/
eval_cnn.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
## eval_cnn.py
##
##
import tensorflow as tf
import cPickle as pickle
import numpy as np
import sys
from cnn_NSGAII import CNN_Individual
BATCH_SIZE = 1000
def make_batches(X, y, batch_size=BATCH_SIZE, shuffle=True):
"""
Get some batches
"""
X_batches = []
y_batches = []
# Randomize the indices
if shuffle:
idx = np.random.permutation(len(X))
else:
idx = np.array(range(len(X)))
cur_idx = 0
while cur_idx < len(X):
end_idx = min(cur_idx + batch_size, len(X))
X_batches.append(X[idx[cur_idx:end_idx]])
y_batches.append(y[idx[cur_idx:end_idx]])
cur_idx += batch_size
return X_batches, y_batches
class Models:
"""
"""
def __init__(self, population):
"""
Create the actual models in tensorflow
"""
self.population = population
# Create a tensorflow session
self.sess = tf.InteractiveSession()
# How big is the input and output?
input_shape = population[0].input_shape
output_size = population[0].output_size
# Create a universal input and output
self.input = tf.placeholder(tf.float32, (None,) + input_shape)
self.target = tf.placeholder(tf.float32, (None, output_size))
# Create an optimizer
self.optimizer = tf.train.AdamOptimizer(0.01)
# Collection of tensors
self.outputs = []
self.losses = []
self.accuracies = []
self.train_steps = []
# Build all the models
for i in range(len(self.population)):
individual = self.population[i]
namespace = 'Individual_%d' % i
output_tensor, loss, accuracy, train_step = self.build_model(individual, namespace)
self.outputs.append(output_tensor)
self.losses.append(loss)
self.accuracies.append(accuracy)
self.train_steps.append(train_step)
# Create tensorboard and initialize the variables
self.tensorboard = tf.summary.FileWriter('./tensorboard', self.sess.graph)
self.sess.run(tf.global_variables_initializer())
def build_model(self, individual, namespace=""):
"""
Build the actual model
"""
# Build everything within the provided namespace
with tf.variable_scope(namespace):
input_tensor, output_tensor = individual.generate_model(self.input)
loss = tf.losses.softmax_cross_entropy(self.target, output_tensor)
target_label = tf.argmax(self.target, 1)
pred_label = tf.argmax(output_tensor, 1)
equality = tf.equal(target_label, pred_label)
accuracy = tf.reduce_mean(tf.cast(equality, tf.float32))
train_step = self.optimizer.minimize(loss)
return output_tensor, loss, accuracy, train_step
def train(self, X, y):
"""
Run a train step on all models
"""
x_batch, y_batch = make_batches(X, y)
for _x, _y in zip(x_batch, y_batch):
fd = {self.input: _x, self.target: _y}
self.sess.run(self.train_steps, feed_dict=fd)
def loss(self, X, y):
"""
Calculate the losses
"""
total_loss = [0.0] * len(self.population)
x_batch, y_batch = make_batches(X,y)
for _x, _y in zip(x_batch, y_batch):
fd = {self.input: _x, self.target: _y}
loss = self.sess.run(self.losses, feed_dict=fd)
for i in range(len(total_loss)):
total_loss[i] += float(len(_x)*loss[i]) / len(X)
return total_loss
def accuracy(self, X, y):
"""
Calculate the accuracies
"""
total_accuracy = [0.0] * len(self.population)
x_batch, y_batch = make_batches(X,y)
for _x, _y in zip(x_batch, y_batch):
fd = {self.input: _x, self.target: _y}
correct = self.sess.run(self.accuracies, feed_dict=fd)
for i in range(len(total_accuracy)):
total_accuracy[i] += float(len(_x) * correct[i]) / len(X)
return total_accuracy
def param_count(self):
"""
Calculate the number of parameters in each model
"""
param_counts = []
for i in range(len(self.population)):
# Get all the trainable variables in the namespace
namespace = 'Individual_%d/' % i
model_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=namespace)
# Get the shape of each variable
var_shapes = [v.get_shape().as_list() for v in model_vars]
# Count the total number of variables
var_counts = [reduce(lambda x,y: x*y, v, 1) for v in var_shapes]
total_vars = reduce(lambda x,y: x+y, var_counts, 0)
param_counts.append(total_vars)
return param_counts
if __name__ == '__main__':
"""
Extract population and data filenames from the command line, load both and evaluate
"""
if len(sys.argv) != 4:
print "USAGE: python eval_cnn.py <population_path> <dataset_path> <output_path>"
sys.exit(-1)
population_path = sys.argv[1]
data_path = sys.argv[2]
output_path = sys.argv[3]
# Load the population
pickle_file = open(population_path)
population = pickle.load(pickle_file)
pickle_file.close()
# Load the datafile
pickle_file = open(data_path)
dataset = pickle.load(pickle_file)
pickle_file.close()
X, y = dataset['train']
X_valid, y_valid = dataset['validate']
# Create the tensorflow models
models = Models(population)
print "Step %d" % 0, "\t",
losses = models.loss(X,y)
accs = models.accuracy(X,y)
for l in losses:
print '%f, ' % l,
print '\t',
for a in accs:
print '%f, ' % (100*a),
print
for i in range(50):
models.train(X, y)
print "Step %d" % (i+1), "\t",
losses = models.loss(X,y)
accs = models.accuracy(X,y)
for l in losses:
print '%f, ' % l,
print '\t',
for a in accs:
print '%f, ' % (100*a),
print
# All done training, get the objectives
losses = models.loss(X_valid,y_valid)
accuracies = models.accuracy(X_valid,y_valid)
num_params = models.param_count()
# Release resources
models.sess.close()
# Apply the objectives to each individual in the population
objectives = []
for i in range(len(population)):
objectives.append([1.0 - accuracies[i], num_params[i]])
# Save the population to the original file
pickle_file = open(output_path, 'wb')
pickle.dump(objectives, pickle_file)
pickle_file.close()