FOLDS = 9 GOODS = ['Fz','FC5','FC1','FC2','FC6','T7','C3','C1','Cz','C2','C4','T8','CP5','CP1','CPz','CP2','CP6','P3','Pz','P4','POz'] KERNELS = 1 WEIGHT_PATH = f"weights/competition/subject-separated/{CLASSES}class/{FOLDS}fold/channel_map2" ## local functions def train(model, train, validation, weight_file): checkpointer = ModelCheckpoint(filepath=weight_file, verbose=1, save_best_only=True) return model.fit(train['x'], train['y'], batch_size=16, epochs=50, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=[checkpointer]) ### 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
### script start # _compX, _compY = separateXY(readall_comp_epochs('data/competition/epoched/ica')) _compX, _compY = epoch_comp(prep_comp(load_comp(True), comp_channel_map3, GOODS, l_freq=LO_FREQ, h_freq=HI_FREQ), CLASSES, resample=RESAMPLE, trange=T_RANGE) # _pilotX, _pilotY = epoch_pilot(loadall_pilot(True), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) _pilotX, _pilotY = epoch_pilot( load_pilot('data/rivet/raw/pilot2/BCI_imaginedmoves_3class_7-4-21.vhdr'), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) _compX, _compY = stratify(_compX, _compY, FOLDS) print(_compX.shape) 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)
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)
from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split, StratifiedKFold from sklearn.ensemble import RandomForestClassifier # constants CLASSES = 2 FOLDS = 10 GOODS = [ 'Fz', 'FC3', 'FC1', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz' ] # script start _compX, _compY = epoch_comp(prep_comp(comp_channel_map3, GOODS, h_freq=30.), CLASSES) _pilotX, _pilotY = epoch_pilot(prepall_pilot(GOODS, h_freq=30.), CLASSES) csp = CSP(n_components=4) csp.fit(_compX, _compY) csp_compX = csp.transform(_compX) csp_pilotX = csp.transform(_pilotX) print(csp_compX) print(csp_compX.shape) # clf = SVC(kernel='linear', C=0.05, probability=True) # clf = MLPClassifier([10, 3], batch_size=16) clf = RandomForestClassifier() skf = StratifiedKFold(FOLDS, shuffle=True, random_state=1) for i, (train_index, test_index) in enumerate(skf.split(csp_compX, _compY)):
### script start # _compX, _compY = separateXY(readall_comp_epochs('data/competition/epoched/ica')) _compX, _compY = epoch_comp(prep_comp(load_comp(True), comp_channel_map3, GOODS, l_freq=LO_FREQ, h_freq=HI_FREQ), CLASSES, resample=RESAMPLE, trange=T_RANGE) _pilotX, _pilotY = epoch_pilot(loadall_pilot(True), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) print(_compX.shape) 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
sys.path.insert(0, os.path.join(sys.path[0], '../modules')) # imports from preparation import prep_comp, epoch_comp, prep_pilot, prepall_pilot, epoch_pilot, comp_channel_map3 from evaluation import EEGNet, get_fold, add_kernel_dim, onehot, test_val_rest_split, shuffle, stratify # constants CLASSES = 2 FOLDS = 9 GOODS = [] # functions def train(): pass def test(): pass # script start pilotX, pilotY = epoch_pilot(prepall_pilot(), CLASSES) print(pilotX, pilotY) # pilotX, pilotY = shuffle(pilotX, pilotY) # print(pilotX, pilotY) pilotX, pilotY = stratify(pilotX, pilotY, 5) print(pilotY)
batch_size=32, epochs=epochs, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=([checkpointer] if checkpointer is not None else [])) ### script start _compX, _compY = epoch_comp(prep_comp(comp_channel_map3, GOODS, h_freq=LOW_PASS), CLASSES, resample=RESAMPLE, trange=T_RANGE) _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)
## local functions def train(model, train, validation, weight_file=None, epochs=300): checkpointer = ModelCheckpoint(filepath=weight_file, verbose=1, save_best_only=True) if weight_file is not None else None return model.fit(train['x'], train['y'], batch_size=32, epochs=epochs, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=([checkpointer] if checkpointer is not None else [])) def prepare_and_test(model, data, targets): pass ### script start # _compX, _compY = epoch_comp(prep_comp(load_comp(True), comp_channel_map3, l_freq=LO_FREQ, h_freq=HI_FREQ), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE) _pilotX1, _pilotY1 = epoch_pilot(load_pilot('data/rivet/raw/rory_pilot/RIVET_BCI_PILOT1.vhdr'), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) _pilotX2, _pilotY2 = epoch_pilot(load_pilot('data/rivet/raw/rory_pilot/RIVET_BCI_PILOT1.vhdr'), CLASSES, GOODS, resample=None, trange=[-1.5, 2.5], l_freq=None, h_freq=None) # """ # rory data - dicard first 50 epochs because noisy # """ # _pilotX = _pilotX[200:] # _pilotY = _pilotY[200:] # _compX, _compY = stratify(_compX, _compY, FOLDS) # print(_compX.shape) # 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]]
return model.fit( train['x'], train['y'], batch_size=32, epochs=epochs, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=([checkpointer] if checkpointer is not None else [])) ### script start # _compX, _compY = epoch_comp(prep_comp(load_comp(True), comp_channel_map3, l_freq=LO_FREQ, h_freq=HI_FREQ), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE) _pilotX, _pilotY = epoch_pilot(load_pilot('data/rivet/raw/jordan/pilot1.vhdr'), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) # """ # rory data - dicard first 50 epochs because noisy # """ # _pilotX = _pilotX[200:] # _pilotY = _pilotY[200:] # _compX, _compY = stratify(_compX, _compY, FOLDS) # print(_compX.shape) # print(_pilotX.shape) # if (_compX.shape[2] > _pilotX.shape[2]): # _compX = _compX[:,:,:_pilotX.shape[2]]