def main(): model = EEGNet(nb_classes=2, Chans=25, Samples=176, dropoutRate=0.5, kernLength=32, F1=8, D=2, F2=16, dropoutType='Dropout') model.load_weights(f'BCICIV2a/weights/all.h5') model._make_predict_function() stream(model)
for i in range(0, FOLDS): comp_trainX, comp_valX, comp_testX = add_kernel_dim(get_fold( _compX, FOLDS, i, test_val_rest_split), kernels=KERNELS) comp_trainY, comp_valY, comp_testY = onehot( get_fold(_compY, FOLDS, i, test_val_rest_split)) # weight file path weight_file = f"{WEIGHT_PATH}/{i+1}.h5" # initialise model model = EEGNet(nb_classes=CLASSES, Chans=chans, Samples=samples, dropoutRate=0.5, kernLength=64, F1=8, D=2, F2=16, dropoutType='Dropout') # compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # train model and save weights # train(model, {"x": comp_trainX, "y": comp_trainY}, {"x": comp_valX, "y": comp_valY}, weight_file, epochs=EPOCHS) # load weights from file model.load_weights(weight_file)
def get_model(transfer_paths=None): K.clear_session() model = EEGNet(nb_classes=3, Chans=9, Samples=250, dropoutRate=0.5, kernLength=64, F1=8, D=2, F2=16, dropoutType='Dropout') # compile model loss function and optimizer for transfer learning model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # load base model model.load_weights(BASE_WEIGHTS) # K.clear_session() print(transfer_paths) if not transfer_paths or len(transfer_paths) is 0: return (model, True) print(len(transfer_paths)) try: transfer_raw = read_raw_brainvision(transfer_paths[0], preload=True) if len(transfer_paths) > 1: for i in range(1, len(transfer_paths)): print(i) transfer_raw = concatenate_raws([ transfer_raw, read_raw_brainvision(transfer_paths[i], preload=True) ]) except Exception as e: print('failed', e) return (model, None) transX, transY = epoch_pilot(transfer_raw, n_classes=3, good_channels=GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) # separate 4:1 train:validation transX, transY = stratify(transX, transY, 5) trans_trainX, trans_valX = add_kernel_dim(get_fold(transX, 5, 0, test_rest_split), kernels=1) trans_trainY, trans_valY = onehot(get_fold(transY, 5, 0, test_rest_split)) trans_valY, _ = onehot((trans_valY, [])) # perform transfer learning on the base model and selected transfer file train(model, { "x": trans_trainX, "y": trans_trainY }, { "x": trans_valX, "y": trans_valY }, epochs=EPOCHS) return (model, False)
# # compile model # model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # # train model and save weights # train(model, {"x": comp_trainX, "y": comp_trainY}, {"x": comp_valX, "y": comp_valY}, weight_file, epochs=EPOCHS) models = [] for i in range(0, MODELS): weight_file = f"{WEIGHT_PATH}/{i+1}.h5" models.append( EEGNet(nb_classes=CLASSES, Chans=chans, Samples=samples, dropoutRate=0.5, kernLength=64, F1=8, D=2, F2=16, dropoutType='Dropout')) models[i].compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) models[i].load_weights(weight_file) # stratify and split pilot data for transfer learning skf = StratifiedKFold(PILOT_FOLDS, shuffle=True) pilot_avg = {'acc': 0, 'bal': 0, 'kap': 0} for i, (train_index, test_index) in enumerate(skf.split(_pilotX, _pilotY)): # reset weights