<jupyter_start><jupyter_text>Load Data - OPENBMI<jupyter_code>subject_ids = list(np.arange(config['train_subject_ids']['low'], config['train_subject_ids']['high']+1, dtype=int))

openbmi_data = MultipleSubjects(
    dataset=OPENBMI, 
    root=config['root'], 
    subject_ids=subject_ids, 
    sessions=config['sessions'],
    selected_channels=config['selected_channels'],
    segment_config=config['segment_config'],
    bandpass_config=config['bandpass_config'],
    one_hot_labels=True,
)<jupyter_output>Load subject: 1
Load subject: 2
Load subject: 3
Load subject: 4
Load subject: 5
Load subject: 6
Load subject: 7
Load subject: 8
Load subject: 9
Load subject: 10
Load subject: 11
Load subject: 12
Load subject: 13
Load subject: 14
Load subject: 15
Load subject: 16
Load subject: 17
Load subject: 18
Load subject: 19
Load subject: 20
Load subject: 21
Load subject: 22
Load subject: 23
Load subject: 24
Load subject: 25
Load subject: 26
Load subject: 27
Load subject: 28
Load subject: 29
Load subject: 30
Load subject: 31
Load subject: 32
Load subject: 33
Load subject: 34
Load subject: 35
Load subject: 36
Load subject: 37
Load subject: 38
Load subject: 39
Load subject: 40
Load subject: 41
Load subject: 42
Load subject: 43
Load subject: 44
Load subject: 45
Load subject: 46
Load subject: 47
Load subject: 48
Load subject: 49
Load subject: 50
Load subject: 51
Load subject: 52
Load subject: 53
Load subject: 54<jupyter_text>Train-Test model - leave one subject out<jupyter_code>train_loader, val_loader, test_loader = openbmi_data.leave_one_subject_out(selected_subject_id=1)
dataloaders_dict = {
    'train': train_loader,
    'val': val_loader
}
check_model = config['check_model'] if 'check_model' in config else False
if check_model:
    x = torch.ones((20, 10, 1000)).to(device)
    
    if config['tsdata'] == True:
        x = torch.ones((40, config['num_channel'], config['segment_config']['window_len'] * config['bandpass_config']['sample_rate'])).to(device)

    model = MultitaskSSVEP(num_channel=config['num_channel'],
        num_classes=config['num_classes'],
        signal_length=config['segment_config']['window_len'] * config['bandpass_config']['sample_rate'],
        filters_n1= config['model']['n1'],
        kernel_window_ssvep= config['model']['kernel_window_ssvep'],
        kernel_window= config['model']['kernel_window'],
        conv_3_dilation= config['model']['conv_3_dilation'],
        conv_4_dilation= config['model']['conv_4_dilation'],
    ).to(device)

    out = model(x)
    print('output',out.shape)

    def count_params(model):
        return sum(p.numel() for p in model.parameters() if p.requires_grad)
    print('model size',count_params(model))

    del model
    del out
model = MultitaskSSVEP(num_channel=config['num_channel'],
    num_classes=config['num_classes'],
    signal_length=config['segment_config']['window_len'] * config['bandpass_config']['sample_rate'],
    filters_n1= config['model']['n1'],
    kernel_window_ssvep= config['model']['kernel_window_ssvep'],
    kernel_window= config['model']['kernel_window'],
    conv_3_dilation= config['model']['conv_3_dilation'],
    conv_4_dilation= config['model']['conv_4_dilation'],
).to(device)

epochs=config['epochs'] if 'epochs' in config else 50
patience=config['patience'] if 'patience' in config else 20
early_stopping=config['early_stopping'] if 'early_stopping' in config else 40

trainer = Multitask_Trainer(model, model_name="multitask", device=device, num_classes=config['num_classes'], multitask_learning=True, patience=patience, verbose=True)

trainer.fit(dataloaders_dict, num_epochs=epochs, early_stopping=early_stopping, topk_accuracy=1, save_model=False)
test_loss, test_acc, test_metric = trainer.validate(test_loader, 1)
print('test: {:.5f}, {:.5f}, {:.5f}'.format(test_loss, test_acc, test_metric))<jupyter_output>test: 21.58886, 0.46500, 0.63500<jupyter_text>Train-Test model - k-fold and leave one subject out<jupyter_code>subject_kfold_acc = {}
subject_kfold_f1 = {}

test_subject_ids = list(np.arange(config['test_subject_ids']['low'], config['test_subject_ids']['high']+1, dtype=int))

for subject_id in test_subject_ids:
    print('Subject', subject_id)
    kfold_acc = []
    kfold_f1 = []
        
    for k in range(config['runkfold']):
        openbmi_data.split_by_kfold(kfold_k=k, kfold_split=config['runkfold'])
        train_loader, val_loader, test_loader = openbmi_data.leave_one_subject_out(selected_subject_id=subject_id, dataloader_batchsize=config['batchsize'])
        dataloaders_dict = {
            'train': train_loader,
            'val': val_loader
        }
        
        model = MultitaskSSVEP(num_channel=config['num_channel'],
            num_classes=config['num_classes'],
            signal_length=config['segment_config']['window_len'] * config['bandpass_config']['sample_rate'],
            filters_n1= config['model']['n1'],
            kernel_window_ssvep= config['model']['kernel_window_ssvep'],
            kernel_window= config['model']['kernel_window'],
            conv_3_dilation= config['model']['conv_3_dilation'],
            conv_4_dilation= config['model']['conv_4_dilation'],
        ).to(device)

        epochs=config['epochs'] if 'epochs' in config else 50
        patience=config['patience'] if 'patience' in config else 20
        early_stopping=config['early_stopping'] if 'early_stopping' in config else 40

        trainer = Multitask_Trainer(model, model_name="Network064b_1-8sub", device=device, num_classes=config['num_classes'], multitask_learning=True, patience=patience, verbose=False)

        trainer.fit(dataloaders_dict, num_epochs=epochs, early_stopping=early_stopping, topk_accuracy=1, save_model=True)
        
        test_loss, test_acc, test_metric = trainer.validate(test_loader, 1)
        # print('test: {:.5f}, {:.5f}, {:.5f}'.format(test_loss, test_acc, test_metric))
        kfold_acc.append(test_acc)
        kfold_f1.append(test_metric)
    
    subject_kfold_acc[subject_id] = kfold_acc
    subject_kfold_f1[subject_id] = kfold_f1

print('results')
print('subject_kfold_acc', subject_kfold_acc)
print('subject_kfold_f1', subject_kfold_f1)
# acc
subjects = []
acc = []
acc_min = 1.0
acc_max = 0.0

for subject_id in subject_kfold_acc:
    subjects.append(subject_id)
    avg_acc = np.mean(subject_kfold_acc[subject_id])
    if avg_acc < acc_min:
        acc_min = avg_acc
    if avg_acc > acc_max:
        acc_max = avg_acc
    acc.append(avg_acc)


x_pos = [i for i, _ in enumerate(subjects)]
figure(num=None, figsize=(15, 3), dpi=80, facecolor='w', edgecolor='k')
plt.bar(x_pos, acc, color='skyblue')
plt.xlabel("Subject")
plt.ylabel("Accuracies")
plt.title("Average k-fold Accuracies by subjects")
plt.xticks(x_pos, subjects)
plt.ylim([acc_min-0.02, acc_max+0.02])
plt.show()

# f1
subjects = []
f1 = []
f1_min = 1.0
f1_max = 0.0

for subject_id in subject_kfold_f1:
    subjects.append(subject_id)
    avg_f1 = np.mean(subject_kfold_f1[subject_id])
    if avg_f1 < f1_min:
        f1_min = avg_f1
    if avg_f1 > f1_max:
        f1_max = avg_f1
    f1.append(avg_f1)


x_pos = [i for i, _ in enumerate(subjects)]
figure(num=None, figsize=(15, 3), dpi=80, facecolor='w', edgecolor='k')
plt.bar(x_pos, f1, color='skyblue')
plt.xlabel("Subject")
plt.ylabel("Accuracies")
plt.title("Average k-fold F1 by subjects")
plt.xticks(x_pos, subjects)
plt.ylim([f1_min-0.02, f1_max+0.02])
plt.show()
print('Average acc:', np.mean(acc))
print('Average f1:', np.mean(f1))<jupyter_output>Average acc: 0.9287592592592593
Average f1: 0.9598666666666666
StarcoderdataJupyterStructuredCleanDedup