-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_hyperparameters.py
91 lines (69 loc) · 3.07 KB
/
optimize_hyperparameters.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
from keras.layers import Dense, Dropout
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.densenet import DenseNet121, preprocess_input
from keras.callbacks import ModelCheckpoint
from keras.utils import multi_gpu_model
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform
from os.path import join
import numpy as np
def data():
npix = 224
imdir = 'Processed Planes'
batch = 64
datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=.1)
train_gen = datagen.flow_from_directory(imdir, class_mode='categorical', target_size=(npix, npix),
subset='training', batch_size=batch)
val_gen = datagen.flow_from_directory(imdir, class_mode='categorical', target_size=(npix, npix),
subset='validation', batch_size=batch)
return train_gen, val_gen
def create_model(train_gen, val_gen):
save_dir = 'Plane Classifiers'
ncat = 25
dropout = .5
nlayers = {{choice([0, 1, 2, 3, 4])}}
nunits = {{choice([64, 128, 256, 512, 1024])}}
loglr = {{uniform(-5, -1)}}
lr = 10 ** loglr
hyperparms = [nlayers, nunits, loglr]
base_model = DenseNet121(include_top=False, pooling='avg')
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
for lyr in range(nlayers):
x = Dense(nunits, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x)
x = Dense(ncat, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=x)
model = multi_gpu_model(model, gpus=2)
model.compile(optimizer=Adam(lr), loss='categorical_crossentropy', metrics=['acc'])
checkpoint = ModelCheckpoint(join(save_dir, '{}.hdf5'.format(hyperparms)), save_best_only=True)
result = model.fit_generator(train_gen,
epochs=60,
validation_data=val_gen,
callbacks=[checkpoint],
verbose=False)
validation_acc = np.amax(result.history['val_acc'])
with open('plane_classifier_results.txt', 'a') as f:
f.write(str(validation_acc) + ' ' + str(hyperparms) + '\n')
return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}
if __name__ == '__main__':
best_run, best_model = optim.minimize(model=create_model,
data=data,
algo=tpe.suggest,
max_evals=25,
trials=Trials())
save_dir = 'Plane Classifiers'
best_model.save(join(save_dir, 'best_model.hdf5'))