print(_pilotX.shape) if (_compX.shape[2] > _pilotX.shape[2]): _compX = _compX[:, :, :_pilotX.shape[2]] elif (_compX.shape[2] < _pilotX.shape[2]): _pilotX = _pilotX[:, :, :_compX.shape[2]] chans, samples = _pilotX.shape[1], _pilotX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) # stratify and split pilot data for transfer learning skf = StratifiedKFold(TRANSFER_FOLDS, shuffle=True) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0} pilot_avg = {'acc': 0, 'bal': 0, 'kap': 0} 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,
### script start _compX, _compY = epoch_comp(prep_comp(comp_channel_map2, GOODS), CLASSES) _pilotX, _pilotY = epoch_pilot(prep_pilot('data/rivet/VIPA_BCIpilot_imaginedmovement.vhdr', GOODS), CLASSES) chans, samples = _pilotX.shape[1], _pilotX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) pilotX = add_kernel_dim((_pilotX,), kernels=KERNELS) pilotY = onehot((_pilotY,)) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0} pilot_avg = {'acc': 0, 'bal': 0, 'kap': 0} for i in range(0, FOLDS): trainX, valX, compX = add_kernel_dim(get_fold(_compX, FOLDS, i, test_val_rest_split), kernels=KERNELS) trainY, valY, compY = 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=125, F1=16, D=4, F2=64, dropoutType='Dropout' ) # compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
batch_size=16, epochs=50, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=[checkpointer]) ### script start compX, compY = epoch_comp(prep_comp({}), CLASSES) chans, samples = compX.shape[1], compX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0} for i in range(0, FOLDS): trainX, valX, testX = add_kernel_dim(get_fold(compX, FOLDS, i, test_val_rest_split), kernels=KERNELS) trainY, valY, 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=125, F1=16, D=4,
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)
_pilotX, _pilotY = epoch_pilot(prepall_pilot(GOODS, h_freq=LOW_PASS), CLASSES, resample=RESAMPLE, trange=T_RANGE) # _pilotX, _pilotY = epoch_pilot(prep_pilot('data/rivet/VIPA_BCIpilot_realmovement.vhdr', GOODS, l_freq=0.5, h_freq=30.), CLASSES) chans, samples = _pilotX.shape[1], _pilotX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) # stratify and split pilot data for transfer learning skf = StratifiedKFold(TRANSFER_FOLDS, shuffle=True) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0} pilot_avg = {'acc': 0, 'bal': 0, 'kap': 0} for i in range(0, FOLDS): comp_trainX, comp_testX = get_fold(_compX, FOLDS, i, test_rest_split) comp_trainY, comp_testY = get_fold(_compY, FOLDS, i, test_rest_split) comp_trainX, comp_trainY = stratify(comp_trainX, comp_trainY, FOLDS - 1) comp_trainX, comp_valX = get_fold(_compX, FOLDS - 1, 0, test_rest_split) comp_trainY, comp_valY = get_fold(_compY, FOLDS - 1, 0, test_rest_split) comp_trainX, comp_valX, comp_testX = add_kernel_dim( (comp_trainX, comp_valX, comp_testX), kernels=KERNELS) comp_trainY, comp_valY, comp_testY = onehot( (comp_trainY, comp_valY, comp_testY)) # weight file path weight_file = f"{WEIGHT_PATH}/{i+1}.h5" # initialise model model = EEGNet(nb_classes=CLASSES, Chans=chans,