Example #1
0
def train(exp_dict):
    history = ms.load_history(exp_dict)

    if 'only_supervised' in exp_dict:
        tgt_trainloader_supervised, tgt_testloader_supervised = ms.get_tgt_loader_supervised(
            exp_dict)
        # load models
        tgt_model, tgt_opt, tgt_scheduler, _, _, _ = ms.load_model_tgt(
            exp_dict)
        fit_source_supervised(tgt_model, tgt_opt, tgt_scheduler,
                              tgt_trainloader_supervised, exp_dict)

        tgt_acc = test.validate(tgt_model, tgt_model,
                                tgt_trainloader_supervised,
                                tgt_testloader_supervised)

        print("{} TEST Accuracy Supervised =========== {:2%}\n".format(
            exp_dict["tgt_dataset"], tgt_acc))
        ms.save_model_tgt(exp_dict, history, tgt_model, tgt_opt)
    else:
        # Source
        src_trainloader, src_valloader = ms.load_src_loaders(exp_dict)

        ####################### 1. Train source model
        src_model, src_opt, src_scheduler = ms.load_model_src(exp_dict)

        # Train Source
        if exp_dict["reset_src"]:
            history = fit_source(src_model, src_opt, src_scheduler,
                                 src_trainloader, history, exp_dict)

        # Test Source
        src_acc = test.validate(src_model, src_model, src_trainloader,
                                src_valloader)

        print("{} TEST Accuracy = {:2%}\n".format(exp_dict["src_dataset"],
                                                  src_acc))
        history["src_acc"] = src_acc

        ms.save_model_src(exp_dict, history, src_model, src_opt)

        ####################### 2. Train target model
        tgt_trainloader, tgt_valloader = ms.load_tgt_loaders(exp_dict)
        #load models
        tgt_model, tgt_opt, tgt_scheduler, disc_model, disc_opt, disc_scheduler = ms.load_model_tgt(
            exp_dict)
        tgt_model.load_state_dict(src_model.state_dict())
        if exp_dict["reset_tgt"]:
            history = fit_target(src_model, tgt_model, tgt_opt, tgt_scheduler,
                                 disc_model, disc_opt, disc_scheduler,
                                 src_trainloader, tgt_trainloader,
                                 tgt_valloader, history, exp_dict)
Example #2
0
def test_latest_model_bing(exp_dict, verbose=1):

    history = load_history(exp_dict)

    if ('only_supervised' in exp_dict) or ('tgt_supervised' in exp_dict):
        tgt_trainloader_supervised, tgt_testloader_supervised = ms.get_tgt_loader_supervised(
            exp_dict)
        # load models
        tgt_model, tgt_opt, tgt_scheduler, _, _, _ = ms.load_model_tgt(
            exp_dict)

        matrixErr, oa, aa, kappa = test.validate_bing(
            tgt_model, tgt_model, tgt_trainloader_supervised,
            tgt_testloader_supervised)
    else:
        src_trainloader, _ = ms.load_src_loaders(exp_dict)
        _, tgt_valloader = ms.load_tgt_loaders(exp_dict)

        src_model, src_opt, _ = ms.load_model_src(exp_dict)
        tgt_model, tgt_opt, _, _, _, _ = ms.load_model_tgt(exp_dict)

        matrixErr, oa, aa, kappa = test.validate_bing(src_model, tgt_model,
                                                      src_trainloader,
                                                      tgt_valloader)
    if verbose:
        print("====================="
              "\nOvearll Accuracy of model at epoch {}: {}\n"
              "=====================".format(history["tgt_train"][-1]["epoch"],
                                             oa))
        print("====================="
              "\nAverage Accuracy of model at epoch {}: {}\n"
              "=====================".format(history["tgt_train"][-1]["epoch"],
                                             aa))
        print("====================="
              "\nKappa of model at epoch {}: {}\n"
              "=====================".format(history["tgt_train"][-1]["epoch"],
                                             kappa))
    results = {}
    results['confusion_matrix'] = matrixErr
    results['OA'] = oa
    results['AA'] = aa
    results['Kappa'] = kappa
    return results
Example #3
0
def test_latest_model(exp_dict, verbose=1):

    history = load_history(exp_dict)

    src_trainloader, _ = ms.load_src_loaders(exp_dict)
    _, tgt_valloader = ms.load_tgt_loaders(exp_dict)

    src_model, src_opt = ms.load_model_src(exp_dict)
    tgt_model, tgt_opt, _, _ = ms.load_model_tgt(exp_dict)

    acc_tgt = test.validate(src_model, tgt_model, src_trainloader,
                            tgt_valloader)
    if verbose:
        print("====================="
              "\nAcc of model at epoch {}: {}\n"
              "=====================".format(history["tgt_train"][-1]["epoch"],
                                             acc_tgt))
    return acc_tgt
Example #4
0
def train(exp_dict):
    history = ms.load_history(exp_dict)

    # Source
    src_trainloader, src_valloader = ms.load_src_loaders(exp_dict)

    ####################### 1. Train source model
    src_model, src_opt = ms.load_model_src(exp_dict)

    # Train Source
    history = fit_source(src_model, src_opt, src_trainloader, history,
                         exp_dict)
    # Test Source
    src_acc = test.validate(src_model, src_model, src_trainloader,
                            src_valloader)

    print("{} TEST Accuracy = {:2%}\n".format(exp_dict["src_dataset"],
                                              src_acc))
    history["src_acc"] = src_acc

    ms.save_model_src(exp_dict, history, src_model, src_opt)

    ####################### 2. Train target model
    tgt_trainloader, tgt_valloader = ms.load_tgt_loaders(exp_dict)

    # load models
    tgt_model, tgt_opt, disc_model, disc_opt = ms.load_model_tgt(exp_dict)
    tgt_model.load_state_dict(src_model.state_dict())

    history = fit_target(src_model, tgt_model, tgt_opt, disc_model, disc_opt,
                         src_trainloader, tgt_trainloader, tgt_valloader,
                         history, exp_dict)

    ms.save_model_tgt(exp_dict, history, tgt_model, tgt_opt, disc_model,
                      disc_opt)

    exp_dict["reset_src"] = 0
    exp_dict["reset_tgt"] = 0
    ms.test_latest_model(exp_dict)
Example #5
0
def train(exp_dict):
    history = ms.load_history(exp_dict)

    #Simone:
    data_transform = transforms.Compose([
        transforms.Resize((exp_dict['image_size'], exp_dict['image_size']),
                          interpolation=1),
        transforms.ToTensor()
    ])

    # CUDA for PyTorch
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda:0" if use_cuda else "cpu")

    history = ms.load_history(exp_dict)

    src_trainloader = get_coxs2v_trainset(
        exp_dict["still_dir"],
        exp_dict["video1_dir"],
        exp_dict["video1_pairs"],
        train_folds,
        exp_dict["cross_validation_num_fold"],
        data_transform,
        people_per_batch,
        images_per_person,
        video_only=True,
        samples_division_list=[0.6, 0.4],  # [0.6, 0.4]
        div_idx=0)

    src_valloader = get_coxs2v_trainset(
        exp_dict["still_dir"],
        exp_dict["video1_dir"],
        exp_dict["video1_pairs"],
        train_folds,
        exp_dict["cross_validation_num_fold"],
        data_transform,
        people_per_batch,
        images_per_person,
        video_only=True,
        samples_division_list=[0.6, 0.4],  # [0.6, 0.4]
        div_idx=1)

    # Source
    #src_trainloader, src_valloader = ms.load_src_loaders(exp_dict)

    ####################### 1. Train source model
    src_model, src_opt = ms.load_model_src(exp_dict)

    # Train Source
    history = fit_source(src_model, src_opt, src_trainloader, history,
                         exp_dict)
    # Test Source
    src_acc = test.validate(src_model, src_model, src_trainloader,
                            src_valloader)

    print("{} TEST Accuracy = {:2%}\n".format(exp_dict["src_dataset"],
                                              src_acc))
    history["src_acc"] = src_acc

    ms.save_model_src(exp_dict, history, src_model, src_opt)

    ####################### 2. Train target model

    #tgt_trainloader, tgt_valloader = ms.load_tgt_loaders(exp_dict)

    tgt_trainloader = get_coxs2v_trainset(
        exp_dict["still_dir"],
        exp_dict["video2_dir"],
        exp_dict["video2_pairs"],
        train_folds,
        exp_dict["cross_validation_num_fold"],
        data_transform,
        people_per_batch,
        images_per_person,
        video_only=True,
        samples_division_list=[0.6, 0.4],  # [0.6, 0.4]
        div_idx=0)

    tgt_valloader = get_coxs2v_trainset(
        exp_dict["still_dir"],
        exp_dict["video2_dir"],
        exp_dict["video2_pairs"],
        train_folds,
        exp_dict["cross_validation_num_fold"],
        data_transform,
        people_per_batch,
        images_per_person,
        video_only=True,
        samples_division_list=[0.6, 0.4],  # [0.6, 0.4]
        div_idx=1)

    # load models
    tgt_model, tgt_opt, disc_model, disc_opt = ms.load_model_tgt(exp_dict)
    tgt_model.load_state_dict(src_model.state_dict())

    history = fit_target(src_model, tgt_model, tgt_opt, disc_model, disc_opt,
                         src_trainloader, tgt_trainloader, tgt_valloader,
                         history, exp_dict)

    ms.save_model_tgt(exp_dict, history, tgt_model, tgt_opt, disc_model,
                      disc_opt)

    exp_dict["reset_src"] = 0
    exp_dict["reset_tgt"] = 0
    ms.test_latest_model(exp_dict)
Example #6
0
def train(exp_dict):
    history = ms.load_history(exp_dict)

    # Source
    src_trainloader, src_valloader = ms.load_src_loaders(exp_dict)
    #####################
    ## Train source model
    #####################
    src_model, src_opt = ms.load_model_src(exp_dict)

    # Train Source
    for e in range(history["src_train"][-1]["epoch"], exp_dict["src_epochs"]):
        train_dict = ts.fit_src(src_model, src_trainloader, src_opt)

        loss = train_dict["loss"]
        print("Source ({}) - Epoch [{}/{}] - loss={:.2f}".format(
            type(src_trainloader).__name__, e, exp_dict["src_epochs"], loss))

        history["src_train"] += [{"loss": loss, "epoch": e}]

        if e % 50 == 0:
            ms.save_model_src(exp_dict, history, src_model, src_opt)

    # Test Source
    src_acc = test.validate(src_model, src_model, src_trainloader,
                            src_valloader)

    print("{} TEST Accuracy = {:2%}\n".format(exp_dict["src_dataset"],
                                              src_acc))
    history["src_acc"] = src_acc

    ms.save_model_src(exp_dict, history, src_model, src_opt)

    #####################
    ## Train Target model
    #####################
    tgt_trainloader, tgt_valloader = ms.load_tgt_loaders(exp_dict)

    # load models
    tgt_model, tgt_opt, disc_model, disc_opt = ms.load_model_tgt(exp_dict)
    tgt_model.load_state_dict(src_model.state_dict())

    for e in range(history["tgt_train"][-1]["epoch"],
                   exp_dict["tgt_epochs"] + 1):
        # 1. Train disc
        if exp_dict["options"]["disc"] == True:
            tg.fit_disc(src_model,
                        tgt_model,
                        disc_model,
                        src_trainloader,
                        tgt_trainloader,
                        opt_tgt=tgt_opt,
                        opt_disc=disc_opt,
                        epochs=3,
                        verbose=0)

        acc_tgt = test.validate(src_model, tgt_model, src_trainloader,
                                tgt_valloader)

        history["tgt_train"] += [{
            "epoch":
            e,
            "acc_src":
            src_acc,
            "acc_tgt":
            acc_tgt,
            "n_train - " + exp_dict["src_dataset"]:
            len(src_trainloader.dataset),
            "n_train - " + exp_dict["tgt_dataset"]:
            len(tgt_trainloader.dataset),
            "n_test - " + exp_dict["tgt_dataset"]:
            len(tgt_valloader.dataset)
        }]

        print("\n>>> Methods: {} - Source: {} -> Target: {}".format(
            None, exp_dict["src_dataset"], exp_dict["tgt_dataset"]))
        print(pd.DataFrame([history["tgt_train"][-1]]))

        if (e % 5) == 0:
            ms.save_model_tgt(exp_dict, history, tgt_model, tgt_opt,
                              disc_model, disc_opt)
            #ms.test_latest_model(exp_dict)

        # 2. Train center-magnet
        if exp_dict["options"]["center"] == True:
            tg.fit_center(src_model,
                          tgt_model,
                          src_trainloader,
                          tgt_trainloader,
                          tgt_opt,
                          epochs=1)

    ms.save_model_tgt(exp_dict, history, tgt_model, tgt_opt, disc_model,
                      disc_opt)

    exp_dict["reset_src"] = 0
    exp_dict["reset_tgt"] = 0
    ms.test_latest_model(exp_dict)