Ejemplo n.º 1
0
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,
Ejemplo n.º 2
0
## 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
  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, 
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
                     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,
Ejemplo n.º 5
0
pilot_avg = {'acc': 0, 'bal': 0, 'kap': 0}
for i in range(REPEATS):
    # stratify and split pilot data for transfer learning
    skf = StratifiedKFold(FOLDS, shuffle=True)

    pilot_fold_avg = {'acc': 0, 'bal': 0, 'kap': 0}
    for i, (train_index, test_index) in enumerate(skf.split(_pilotX, _pilotY)):
        # stratified train-val-test split for pilot data
        pilot_trainX, pilot_testX = _pilotX[train_index], _pilotX[test_index]
        pilot_trainY, pilot_testY = _pilotY[train_index], _pilotY[test_index]
        pilot_trainX, pilot_valX, pilot_trainY, pilot_valY = train_test_split(
            pilot_trainX,
            pilot_trainY,
            test_size=1 / (FOLDS - 1),
            stratify=pilot_trainY)
        pilot_trainX, pilot_valX, pilot_testX = add_kernel_dim(
            (pilot_trainX, pilot_valX, pilot_testX), kernels=KERNELS)
        pilot_trainY, pilot_valY, targets = onehot(
            (pilot_trainY, pilot_valY, pilot_testY))

        # 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
Ejemplo n.º 6
0
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,
                   Samples=samples,
                   dropoutRate=0.5,
                   kernLength=128,
                   F1=8,
                   D=2,
                   F2=16,