Esempio n. 1
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)
Esempio n. 2
0
    for exp_name in args.expList:
        exp_dict = experiments.get_experiment_dict(args, exp_name)
        exp_dict["reset_src"] = args.reset_src
        exp_dict["reset_tgt"] = args.reset_tgt

        # SET SEED
        np.random.seed(10)
        torch.manual_seed(10)
        torch.cuda.manual_seed_all(10)

        history = ms.load_history(exp_dict)


        # Main options
        if args.mode == "test_model":
            results[exp_name] = ms.test_latest_model(exp_dict, verbose=0)

        elif args.mode == "train":
            train.train(exp_dict)


        if args.mode == "copy_models":
            results[exp_name] = ms.copy_models(
                exp_dict, path_dst="{}/".format(exp_name))

        # MISC
        if args.mode == "plot_src":

            src_losses = np.array(pd.DataFrame(history["src_train"])["loss"])
            src_epochs = np.array(pd.DataFrame(history["src_train"])["epoch"])
Esempio n. 3
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)
Esempio n. 4
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)