def full_distributed(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=''): 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) # Flatten the data for training full_features = np.vstack(features_train) full_labels = np.vstack(labels_train) 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=dr) model.compile(loss=categorical_crossentropy, optimizer=Adam(), metrics=['accuracy']) filename_ = '{0}{1}{2}'.format(model_name, 'Distributed_{}'.format(i + 1), addon) metrics[i] = distributed_unit(model, full_features, full_labels, features_test[i], labels_test[i], filename_, class_names) metrics_to_csv(metrics, '{}_Distributed_Learning_{}'.format(model_name, filename_))
def frozen_from_4mvt(model_name, model_file, big_X_train, big_y_train, big_X_test, big_y_test, class_names=class_names, ch_num=25, ep=50, dr=0.1, 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)): filename_ = '{0}{1}{2}'.format(model_name, '_transfer_learning_{}_'.format(i + 1), addon) model_ = EEGNet_var(model_file, 4, 2, Chans=ch_num, dropoutRate=dr, Samples=1125) metrics[i] = transfer_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))
def frozen_4mvt_2mvt(model_name, model_file, big_X_train, big_y_train, big_X_test, big_y_test, class_names=class_names, ch_num=25, ep=50, 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, 9, 10, 11, 12] series = [] for idx in range(182): if idx % 14 != 0: list_element.append(my_list[idx % len(my_list)]) elif idx % 14 == 0: series.append(list_element) list_element = [] series[0] = list_element metrics = np.zeros(((len(my_list)), 4)) ### ### Iterate trough the sequences ### for user_list in series: print('Starting Frozen learning for user {}'.format(user_list[-1] + 1)) features_train = [] labels_train = [] # 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.append(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.append(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 = 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 = np_utils.to_categorical(encoded_Y) filename_ = '{0}{1}{2}'.format(model_name, '_transfer_learning_{}_'.format(i + 1), addon) model_ = EEGNet_var(model_file, 4, 2, Chans=ch_num, dropoutRate=dr, Samples=1125) metrics[user_list[-1]] = transfer_unit(model_, full_features, full_labels, features_test, labels_test, filename_, class_names) metrics_to_csv(metrics, '{}_Standard_testing_{}'.format(model_name, addon))
def splited_with_model(model_name, big_X_train, big_y_train, big_X_test, big_y_test, class_names=class_names, ch_num=25, ep=50, addon=''): ### ### First create the sequence of training users and single test user ### list_element = [] my_list = [0, 1, 2, 3, 4, 5, 6, 7] series = [] for idx in range(72): if idx % 9 != 0: list_element.append(my_list[idx % len(my_list)]) elif idx % 9 == 0: series.append(list_element) list_element = [] series[0] = list_element metrics = np.zeros(((len(my_list)), 4)) ### ### Iterate trough the sequences ### r_series = reversed(series) for user_list in r_series: print('Starting Splited learning for user {}'.format(user_list[-1])) ### ### Only 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) ### ### 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}{3}'.format(model_name, '_Split_{}'.format(user_list[-1] + 1), addon, '_{}_Epochs'.format(str(ep))) model_file = 'models/{0}{1}_50_Epochs{2}.h5'.format(model_name, 'Freezing_{}'.format(user_list[-1] + 1), '_-2_Frozen') metrics[user_list[-1]] = split_unit_with_model(model_file, features_train_2, features_test_2, labels_train_2, labels_test_2, filename_, class_names, ep) del features_train_2, features_test_2, \ labels_train_2, labels_test_2 metrics_to_csv(metrics, '{}_Split_Learning_{}'.format(model_name, filename_))
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] series = [] for idx in range(72): if idx % 9 != 0: list_element.append(my_list[idx % len(my_list)]) elif idx % 9 == 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=4, 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_))