kerasTuner.py 4.83 KB
Newer Older
Ard Kastrati's avatar
Ard Kastrati committed
1
from kerastuner.tuners import RandomSearch
Ard Kastrati's avatar
Ard Kastrati committed
2
3
4
5
6
7
8
9
10
11
12
from sklearn.model_selection import train_test_split
from config import config
import logging

from CNN.CNN import Classifier_CNN
from DeepEye.deepeye import Classifier_DEEPEYE
from DeepEyeRNN.deepeyeRNN import Classifier_DEEPEYE_RNN
from Xception.xception import Classifier_XCEPTION
from InceptionTime.inception import Classifier_INCEPTION
from EEGNet.eegNet import Classifier_EEGNet

Ard Kastrati's avatar
Ard Kastrati committed
13
14

def build_model(hp):
Ard Kastrati's avatar
Ard Kastrati committed
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
    classifier = None
    logging.info('Starting tuning ' + config['model'])
    if config['model'] == 'deepeye':
        classifier = Classifier_DEEPEYE(input_shape=config['deepeye']['input_shape'],
                                        epochs=2, verbose=True, batch_size=64, use_residual=True,
                                        kernel_size=hp.Choice('kernel_size', values=[40, 64]),
                                        nb_filters=hp.Choice('nb_filters', values=[32, 64]),
                                        depth=hp.Int('depth',min_value=6,max_value=20, step=4),
                                        bottleneck_size=hp.Choice('bottleneck_size', values=[32, 64]),
                                        use_simple_convolution= hp.Choice('use_simple_convolution', values=[True, False]),
                                        use_separable_convolution=hp.Choice('use_separable_convolution', values=[True, False]),
                                        preprocessing=False)
                                        # preprocessing_F1 = hp.Choice('preprocessing_F1', values=[8, 16, 32, 64]),
                                        # preprocessing_D = hp.Choice('preprocessing_F1', values=[2, 4, 6, 8]),
                                        # preprocessing_kernLength = hp.Choice('preprocessing_kernlength', values=[64, 125, 250]),

    elif config['model'] == 'cnn':
        classifier = Classifier_CNN(input_shape=config['cnn']['input_shape'],
                                    epochs=2, verbose=True, batch_size=64,
                                    use_residual=hp.Choice('use_residual', values=[True, False]),
                                    kernel_size=hp.Choice('kernel_size', values=[40, 32, 64]),
                                    nb_filters=hp.Choice('nb_filters', values=[16, 32, 64]),
                                    depth=hp.Int('depth', min_value=6, max_value=20, step=3),
                                    preprocessing=False
                                    )
    elif config['model'] == 'eegnet':
        classifier = Classifier_EEGNet(dropoutRate = 0.5, kernLength = 250, F1 = 16,
                                       D = 4, F2 = 256, norm_rate = 0.5, dropoutType = 'Dropout',
                                       epochs = 50)

    elif config['model'] == 'inception':
        classifier = Classifier_INCEPTION(input_shape=config['inception']['input_shape'],
                                          epochs=2, verbose=True, batch_size=64,
                                          use_residual=hp.Choice('use_residual', values=[True, False]),
                                          kernel_size=hp.Choice('kernel_size', values=[40, 32, 64]),
                                          nb_filters=hp.Choice('nb_filters', values=[16, 32, 64]),
                                          depth=hp.Int('depth', min_value=6, max_value=20, step=3),
                                          bottleneck_size=hp.Choice('bottleneck_size', values=[16, 32, 64])
                                          )

    elif config['model'] == 'xception':
        classifier = Classifier_XCEPTION(input_shape=config['inception']['input_shape'],
                                         epochs=2, verbose=True, batch_size=64,
                                          use_residual=hp.Choice('use_residual', values=[True, False]),
                                          kernel_size=hp.Choice('kernel_size', values=[40, 32, 64]),
                                          nb_filters=hp.Choice('nb_filters', values=[16, 32, 64]),
                                          depth=hp.Int('depth', min_value=6, max_value=20, step=3)
                                         )

    elif config['model'] == 'deepeye-rnn':
        classifier = Classifier_DEEPEYE_RNN(input_shape=config['deepeye-rnn']['input_shape'])
    else:
        logging.info('Cannot start the program. Please choose one model in the config.py file')

    return classifier.get_model()


def tune(trainX, trainY):
    tuner = RandomSearch(
        build_model,
        objective='val_accuracy',
76
77
        max_trials=200,
        executions_per_trial=2,
Ard Kastrati's avatar
Ard Kastrati committed
78
79
80
81
82
83
84
85
86
        directory='my_dir',
        project_name='testKerasTuner')

    print(trainX.shape)
    tuner.search_space_summary()
    X_train, X_val, y_train, y_val = train_test_split(trainX, trainY, test_size=0.2, random_state=42)
    tuner.search(X_train, y_train, epochs=1, validation_data=(X_val, y_val))
    tuner.results_summary()