Example #1
0
    mean = params['mean']
    std = params['std']
    original_signal = (original_signal-mean)/std

epoch_length = int(Fs * window_time_length / 1000.)
perturb_length = math.floor(Fs * perturb_time / 1000.)

# ============================ build model ==============================
processers = [
    Blocks.Xdawn(n_filters=4, with_xdawn_templates=True, apply_filters=True),
    Blocks.CovarianceFeature(with_mean_templates=False),
    Blocks.TangentSpaceFeature(mean_metric='riemann', name='TangentSpace({})'.format('riemann'))
]
classifier = Blocks.LogisticRegression()

model = Pipeline(processers, classifier)
model.load(load_path)
keras_model = model.get_keras_model(input_shape=(n_channel, epoch_length))

# =========================== target attack ==============================
acc_dict = {}
clean_acc_dict = {}
for target_char in target_char_list:

    # get stimuli of the target char
    x, y = np.argwhere(_CHAR_MATRIX == target_char)[0]
    x += 7
    y += 1
    target_locations = (y, x)

    # ============================ add adv_noise ============================
Example #2
0
    y1_rate = np.mean(np.where(y == 1, 1, 0))
    class_weight = {0: y1_rate, 1: y0_rate}
else:
    class_weight = None

# ============================== build model ==================================
processers = [
    Blocks.Xdawn(n_filters=8, with_xdawn_templates=True, apply_filters=True),
    Blocks.CovarianceFeature(with_mean_templates=False),
    Blocks.TangentSpaceFeature(mean_metric='riemann',
                               name='TangentSpace({})'.format('riemann'))
]

classifier = Blocks.LogisticRegression(class_weight=class_weight,
                                       max_iter=2000)

model = Pipeline(processers, classifier)
print('subject: {}'.format(subject))
model.pipeline_information()  # show model information

# ============================== train model ===================================
print('fitting.......')
model.fit(epochs, y)
model.save(save_path)

y_pred = np.argmax(model.predict(epochs), axis=1)
bca = np.round(utils.bca(y, y_pred), decimals=3)
acc = np.round(np.sum(y_pred == y).astype(np.float64) / len(y_pred),
               decimals=3)
print('train (target and nontarget): acc={}, bca={}'.format(acc, bca))