-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhyperas_coarse.py
118 lines (95 loc) · 4.13 KB
/
hyperas_coarse.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
from __future__ import print_function
gpu = 'gpu2'
import os
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=%s,floatX=float32" % gpu
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform, conditional, quniform
from keras.datasets import cifar100
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
import cPickle as pickle
# Open an IPython session if an exception is found
import sys
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose', color_scheme='Linux', call_pdb=1)
nb_epoch = 30#10 #NOTE: need to modify this elsewhere as well
nb_evals = 100#50
def data():
nb_classes_fine = 100
nb_classes_coarse = 20
(X_train, y_train_fine), (X_test, y_test_fine) = cifar100.load_data(label_mode='fine')
(_, y_train_coarse), (_, y_test_coarse) = cifar100.load_data(label_mode='coarse')
Y_train = np_utils.to_categorical(y_train_coarse, nb_classes_coarse)
Y_test = np_utils.to_categorical(y_test_coarse, nb_classes_coarse)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
return X_train, Y_train, X_test, Y_test
"""
Y_train_fine = np_utils.to_categorical(y_train_fine, nb_classes_fine)
Y_train_coarse = np_utils.to_categorical(y_train_coarse, nb_classes_coarse)
Y_test_fine = np_utils.to_categorical(y_test_fine, nb_classes_fine)
Y_test_coarse = np_utils.to_categorical(y_test_coarse, nb_classes_coarse)
return X_train, Y_train_fine, X_test, Y_test_fine
"""
def model(X_train, Y_train, X_test, Y_test):
nb_dim = 20
img_rows, img_cols = 32, 32
img_channels = 3
dense_layer_size = {{choice([256, 512, 1024])}}
optimizer = {{choice(['rmsprop', 'adam', 'sgd'])}}
batch_size = {{choice([32, 64, 128])}}
num_conv1 = int({{quniform(24, 64, 1)}})
num_conv2 = int({{quniform(32, 96, 1)}})
params = {'dense_layer_size':dense_layer_size,
'optimizer':optimizer,
'batch_size':batch_size,
'num_conv1':num_conv1,
'num_conv2':num_conv2,
}
model = Sequential()
model.add(Convolution2D(num_conv1, 3, 3, border_mode='same',
input_shape=(img_channels, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(num_conv1, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(num_conv2, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(num_conv2, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(dense_layer_size))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_dim))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=optimizer)
model.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=30,
show_accuracy=True,
verbose=2,
validation_data=(X_test, Y_test))
score, acc = model.evaluate(X_test, Y_test, verbose=0)
print('Test accuracy:', acc)
#return {'loss': -acc, 'status': STATUS_OK, 'model':model}
return {'loss': -acc, 'status': STATUS_OK, 'params':params}
if __name__ == '__main__':
trials = Trials()
best_run, best_model = optim.minimize(model=model,
data=data,
algo=tpe.suggest,
max_evals=nb_evals,
trials=trials)
X_train, Y_train, X_test, Y_test = data()
#print("Evaluation of best performing model:")
#print(best_model.evaluate(X_test, Y_test))
pickle.dump(trials, open('net_output/trials_coarse_epoch%s_evals%s.p'%(nb_epoch, nb_evals),'w'))