print('epoch: {} / {}'.format(epoch + 1, epoch_num))
    print('-' * 20)

    train_D_loss, train_F_loss, train_src_acc = training(src_data_train,
                                                         target_data_training,
                                                         feature_extractor,
                                                         domain_classifier,
                                                         label_predictor,
                                                         optimizer_F,
                                                         optimizer_C,
                                                         optimizer_D,
                                                         lamb=0.1)

    val_src_D_loss, val_src_C_loss, val_src_acc = validation(src_data_valid,
                                                             feature_extractor,
                                                             domain_classifier,
                                                             label_predictor,
                                                             domain='source')

    val_tag_D_loss, val_tag_C_loss, val_tag_acc = validation(tag_data_valid,
                                                             feature_extractor,
                                                             domain_classifier,
                                                             label_predictor,
                                                             domain='target')

    test0_D_loss, test0_C_loss, test0_acc = testing(tag_data_test0,
                                                    feature_extractor,
                                                    domain_classifier,
                                                    label_predictor)

    test1_D_loss, test1_C_loss, test1_acc = testing(tag_data_test1,
Exemplo n.º 2
0
        # print(tr_acc/tr_pix, loss.item())
        
        #print training information
        print('\rTraining:Epoch[{}]-Iter[{}/{}] = Loss:{:.3f}' 
            '= Acc:{:.3f} = LR:{:.5f} = time:{:.1f}'.format(
            epoch, i, len(loader_train), train_loss.average(), train_acc.average(), lr,
            time.time() - tic), end= '')

    history['tr_loss'].append(train_loss.average())
    history['tr_acc'].append(train_acc.average())
    
    # validation of model performance in the validation set
    if cfg.VAL.validate:
        if (epoch+1) % cfg.VAL.val_step ==0 or epoch+1 == cfg.TRAIN.num_epoch:
            print('Evaluating model!')
            val_miou, val_ciou, val_acc = validation(model, loader_val, cfg)
            history['val_miou'].append(val_miou)
            history['val_ciou'].append(val_ciou)
            history['val_acc'].append(val_acc)

            if val_miou > best_pred[0]:
                best_pred[0] = val_miou
                best_model= True
            else:
                best_model=False
            if val_acc > best_pred[1]:
                best_pred[1] = val_acc
            print('Best validation IOU and Acc:', best_pred)

            checkpoint(state={'epoch': epoch,
                        'encoder': net_encoder.state_dict(),
Exemplo n.º 3
0
tag_test0_C_loss_cur = []
tag_test0_acc_cur = []
tag_test1_D_loss_cur = []
tag_test1_C_loss_cur = []
tag_test1_acc_cur = []

for epoch in range(epoch_num):
    epoch_start_time = time.time()

    print('epoch: {} / {}'.format(epoch + 1, epoch_num))
    print('-' * 20)

    # TL process
    train_loss, train_acc, alpha, net = training(src_data_train, tgt_data_train, net, optimizer, epoch_num, epoch=epoch)

    src_valid_D_loss, src_valid_C_loss, src_valid_acc = validation(src_data_valid, net=net, alpha=alpha)

    tag_valid_D_loss, tag_valid_C_loss, tag_valid_acc = validation(tgt_data_valid, net=net, alpha=alpha)

    tag_test0_D_loss, tag_test0_C_loss, tag_test0_acc = testing(tgt_data_test0, net=net, alpha=alpha)

    tag_test1_D_loss, tag_test1_C_loss, tag_test1_acc = testing(tgt_data_test1, net=net, alpha=alpha)

    print('Training: Loss:{:.4f}  src_Acc:{:.4f}'.format(train_loss, train_acc))
    print('Valid Source:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'
          .format(src_valid_D_loss, src_valid_C_loss, src_valid_acc))
    print('Valid Target:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'
          .format(tag_valid_D_loss, tag_valid_C_loss, tag_valid_acc))
    print('Test0:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'
          .format(tag_test0_D_loss, tag_test0_C_loss, tag_test0_acc))
    print('Test1:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'
best_loss = float('inf')

for epoch in range(epoch_num):
    epoch_start_time = time.time()

    print('epoch: {} / {}'.format(epoch + 1, epoch_num))
    print('-' * 20)

    train_loss, train_acc, alpha, net = training(source_train_dataloader,
                                                 target_train_dataloader,
                                                 net,
                                                 optimizer,
                                                 epoch_num,
                                                 epoch=epoch)

    src_valid_D_loss, src_valid_C_loss, src_valid_acc = validation(
        source_valid_dataloader, net=net, alpha=alpha)
    tag_valid_D_loss, tag_valid_C_loss, tag_valid_acc = validation(
        target_valid_dataloader, net=net, alpha=alpha)
    tag_test_D_loss, tag_test_C_loss, tag_test_acc = testing(
        target_test_dataloader, net=net, alpha=alpha)

    print('Training: Loss:{:.4f}  src_Acc:{:.4f}'.format(
        train_loss, train_acc))
    print('Valid Source:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'.format(
        src_valid_D_loss, src_valid_C_loss, src_valid_acc))
    print('Valid Target:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'.format(
        tag_valid_D_loss, tag_valid_C_loss, tag_valid_acc))
    print('Test_t:  D_loss:{:.4f}  C_loss:{:.4f}  Acc:{:.4f}'.format(
        tag_test_D_loss, tag_test_C_loss, tag_test_acc))

    print('epoch time usage: {:.2f}s'.format(time.time() - epoch_start_time))
                        target_train_dataloader,
                        feature_extractor,
                        domain_classifier,
                        label_predictor,
                        lamb=lamb)
                    print('Train: subject:{}  D_loss:{:.6f}  F_loss:{:.6f}'.
                          format(dataset_index, set_D_loss, set_F_loss,
                                 set_accuracy))
                else:
                    source_valid_dataloader = source_train_dataloaders[phase][
                        dataset_index]
                    target_valid_dataloader = target_subject_valid_loader

                    src_set_D_loss, src_set_C_loss, src_set_accuracy = validation(
                        source_valid_dataloader,
                        feature_extractor,
                        domain_classifier,
                        label_predictor,
                        domain='source')

                    tag_set_D_loss, tag_set_C_loss, tag_set_accuracy = validation(
                        target_valid_dataloader,
                        feature_extractor,
                        domain_classifier,
                        label_predictor,
                        domain='target')
                    D_loss = src_set_D_loss + tag_set_D_loss
                    F_loss = src_set_C_loss + lamb * D_loss
                    print(
                        'Valid: subject:{}  D_loss:{:.6f}  F_loss:{:.6f}  src_Acc:{:.6f}  tag_Acc:{:.6}'
                        .format(dataset_index, D_loss, F_loss,
                                src_set_accuracy, tag_set_accuracy))
Exemplo n.º 6
0
)

# record training process
epoch_train_losses = []
epoch_train_scores = []
epoch_test_losses = []
epoch_test_scores = []

# start training
for epoch in range(epochs):
    # train, test model
    train_losses, train_scores = train(
        log_interval, cnn_model, device, train_loader, optimizer, epoch
    )
    epoch_test_loss, epoch_test_score = validation(
        cnn_model, device, valid_loader, optimizer, save_model_path, epoch
    )

    # save results
    epoch_train_losses.append(train_losses)
    epoch_train_scores.append(train_scores)
    epoch_test_losses.append(epoch_test_loss)
    epoch_test_scores.append(epoch_test_score)

    # save all train test results
    A = np.array(epoch_train_losses)
    B = np.array(epoch_train_scores)
    C = np.array(epoch_test_losses)
    D = np.array(epoch_test_scores)
    np.save("./CNN_epoch_training_losses.npy", A)
    np.save("./CNN_epoch_training_scores.npy", B)