Exemple #1
0
def standard_all(model_,
                 model_name,
                 big_X_train,
                 big_y_train,
                 big_X_test,
                 big_y_test,
                 ch_num,
                 class_names=class_names,
                 addon=''):
    features_train = [None] * len(big_X_train)
    labels_train = [None] * len(big_y_train)

    for i, (X_user, y_user) in enumerate(zip(big_X_train, big_y_train)):
        temp = [item for sublist in X_user for item in sublist]
        temp = np.asanyarray(temp)
        temp = np.swapaxes(temp, 1, 2)
        features_train[i] = temp.reshape(temp.shape[0], 1, ch_num, 1125)
        lab = [item for sublist in y_user for item in sublist]
        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(lab)
        encoded_Y = encoder.transform(lab)
        # convert integers to dummy variables (i.e. one hot encoded)
        labels_train[i] = np_utils.to_categorical(encoded_Y)

    features_test = [None] * len(big_X_test)
    labels_test = [None] * len(big_y_test)
    for i, (X_user, y_user) in enumerate(zip(big_X_test, big_y_test)):
        temp = [item for sublist in X_user for item in sublist]
        temp = np.asanyarray(temp)
        temp = np.swapaxes(temp, 1, 2)
        features_test[i] = temp.reshape(temp.shape[0], 1, ch_num, 1125)
        lab = [item for sublist in y_user for item in sublist]
        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(lab)
        encoded_Y = encoder.transform(lab)
        # convert integers to dummy variables (i.e. one hot encoded)
        labels_test[i] = np_utils.to_categorical(encoded_Y)

    metrics = np.zeros(((len(big_y_train)), 4))

    for i in range(len(big_X_train)):
        model = EEGNet_org(nb_classes=len(class_names),
                           Chans=ch_num,
                           Samples=1125,
                           dropoutRate=0.2)

        model.compile(loss=categorical_crossentropy,
                      optimizer=Adam(),
                      metrics=['accuracy'])

        filename_ = '{0}{1}{2}'.format(model_name,
                                       'standard_user_{}'.format(i + 1), addon)
        metrics[i] = standard_unit(model, features_train[i], labels_train[i],
                                   features_test[i], labels_test[i], filename_,
                                   class_names)

    metrics_to_csv(metrics, '{}_Standard_testing_{}'.format(model_name, addon))
Exemple #2
0
def full_freezing(model_name, big_X_train, big_y_train, big_X_test, big_y_test,
                  class_names=class_names, ch_num=25, dr=0.1, addon=''):
    ###
    ### Take all the data for training
    ###

    ###
    ### Train and save the model
    ###

    my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

    model = EEGNet_org(nb_classes=2, Chans=ch_num, Samples=1125, dropoutRate=dr)

    model.compile(loss=categorical_crossentropy,
                  optimizer=Adam(), metrics=['accuracy'])

    features_train = []
    labels_train = []

    for i in my_list:
        temp = [item for sublist in big_X_train[i] for item in sublist]
        temp = np.asanyarray(temp)
        temp = np.swapaxes(temp, 1, 2)
        features_train.append(temp.reshape(temp.shape[0], 1, ch_num, 1125))
        lab = [item for sublist in big_y_train[i] for item in sublist]
        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(lab)
        encoded_Y = encoder.transform(lab)
        # convert integers to dummy variables (i.e. one hot encoded)
        labels_train.append(np_utils.to_categorical(encoded_Y))

        # Also add the testing data to increase the size of training data
        temp = [item for sublist in big_X_test[i] for item in sublist]
        temp = np.asanyarray(temp)
        temp = np.swapaxes(temp, 1, 2)
        features_train.append(temp.reshape(temp.shape[0], 1, ch_num, 1125))
        lab = [item for sublist in big_y_test[i] for item in sublist]
        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(lab)
        encoded_Y = encoder.transform(lab)
        # convert integers to dummy variables (i.e. one hot encoded)
        labels_train.append(np_utils.to_categorical(encoded_Y))

        # Flatten the data for training
        full_features = np.vstack(features_train)
        full_labels = np.vstack(labels_train)

    filename_ = '{0}{1}{2}'.format(model_name, '_Full_Freezing_2mvt', addon)

    full_freezing_unit(model, full_features, full_labels,
                       filename_, class_names)
Exemple #3
0
def splitted_layers(model_name,
                    big_X_train,
                    big_y_train,
                    big_X_test,
                    big_y_test,
                    class_names=class_names,
                    ch_num=25,
                    dr=0.1,
                    addon=''):
    ###
    ### First create the sequence of training users and single test user
    ###
    list_element = []
    my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    series = []

    for idx in range(90):
        if idx % 10 != 0:
            list_element.append(my_list[idx % len(my_list)])
        elif idx % 10 == 0:
            series.append(list_element)
            list_element = []
    series[0] = list_element

    features_train = []
    labels_train = []

    metrics = np.zeros(((len(my_list)), 4))
    ###
    ### Iterate trough the sequences
    ###
    for user_list in series:
        print('Starting Splitted learning for user {}'.format(user_list[-1]))

        model = EEGNet_org(nb_classes=2,
                           Chans=ch_num,
                           Samples=1125,
                           dropoutRate=dr)

        model.compile(loss=categorical_crossentropy,
                      optimizer=Adam(),
                      metrics=['accuracy'])

        # Create the first training data
        for i in user_list[:-1]:
            temp = [item for sublist in big_X_train[i] for item in sublist]
            temp = np.asanyarray(temp)
            temp = np.swapaxes(temp, 1, 2)
            features_train.append(temp.reshape(temp.shape[0], 1, ch_num, 1125))
            lab = [item for sublist in big_y_train[i] for item in sublist]
            # encode class values as integers
            encoder = LabelEncoder()
            encoder.fit(lab)
            encoded_Y = encoder.transform(lab)
            # convert integers to dummy variables (i.e. one hot encoded)
            labels_train.append(np_utils.to_categorical(encoded_Y))

            # Also add the testing data to increase the size of training data
            temp = [item for sublist in big_X_test[i] for item in sublist]
            temp = np.asanyarray(temp)
            temp = np.swapaxes(temp, 1, 2)
            features_train.append(temp.reshape(temp.shape[0], 1, ch_num, 1125))
            lab = [item for sublist in big_y_test[i] for item in sublist]
            # encode class values as integers
            encoder = LabelEncoder()
            encoder.fit(lab)
            encoded_Y = encoder.transform(lab)
            # convert integers to dummy variables (i.e. one hot encoded)
            labels_train.append(np_utils.to_categorical(encoded_Y))
        ###
        ### Create the second training data (of the specific user)
        ###
        temp = [
            item for sublist in big_X_train[user_list[-1]] for item in sublist
        ]
        temp = np.asanyarray(temp)
        temp = np.swapaxes(temp, 1, 2)
        features_train_2 = temp.reshape(temp.shape[0], 1, ch_num, 1125)
        lab = [
            item for sublist in big_y_train[user_list[-1]] for item in sublist
        ]
        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(lab)
        encoded_Y = encoder.transform(lab)
        # convert integers to dummy variables (i.e. one hot encoded)
        labels_train_2 = np_utils.to_categorical(encoded_Y)

        # Flatten the data for training
        full_features = np.vstack(features_train)
        full_labels = np.vstack(labels_train)
        ###
        ### Create the testing data (of the specific user)
        ###
        temp = [
            item for sublist in big_X_test[user_list[-1]] for item in sublist
        ]
        temp = np.asanyarray(temp)
        temp = np.swapaxes(temp, 1, 2)
        features_test_2 = temp.reshape(temp.shape[0], 1, ch_num, 1125)
        lab = [
            item for sublist in big_y_test[user_list[-1]] for item in sublist
        ]
        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(lab)
        encoded_Y = encoder.transform(lab)
        # convert integers to dummy variables (i.e. one hot encoded)
        labels_test_2 = np_utils.to_categorical(encoded_Y)

        filename_ = '{0}{1}{2}'.format(model_name,
                                       'Splitted_{}'.format(user_list[-1] + 1),
                                       addon)

        metrics[user_list[-1]] = freezing_unit(model, full_features,
                                               full_labels, features_train_2,
                                               features_test_2, labels_train_2,
                                               labels_test_2, filename_,
                                               class_names)
    metrics_to_csv(metrics,
                   '{}_Splitted_Learning_{}'.format(model_name, filename_))