Example #1
0
def test_single_ce():
    start_time = smtp.print_time("全部开始测试!!!")
    ckpt = cfg.dataset.ceface_align_ckpt
    data_dir = cfg.dataset.ceface_align
    ###########################################################################################################
    ckpt_ce = os.listdir(ckpt)
    name = "epoch079_0.02709_3.7538.pth"
    test_mae_arr = []
    for item in ckpt_ce:
        #验证的时候l1loss没什么用
        test_mae_arr.append(
            main({
                "data_dir": data_dir,
                "ifSE": True,
                "l1loss": False,
                "resume": cfg.dataset.ceface_align_ckpt + "/" + item
            }))
    ###########################################################################################################
    print("test_mae_arr: ", test_mae_arr)
    print("test_mae_mean: ", np.mean(test_mae_arr))
    print("test_mae_min: ", min(test_mae_arr))
    print("test_mae_max: ", max(test_mae_arr))

    end_time = smtp.print_time("全部测试结束!!!共耗时:")
    print(smtp.date_gap(start_time, end_time))
Example #2
0
def test_single():
    start_time = smtp.print_time("全部开始测试!!!")
    tf_log = cfg.tf_log[0]
    ckpt = cfg.checkpoint + "/morph2_align_sfv2_l1"  # cfg.ckpt[0]
    data_dir = {
        "morph2": cfg.dataset.morph2,
        "morph2_align": cfg.dataset.morph2_align
    }
    test_mae_morph2 = []
    ckpt_morph2 = os.listdir(ckpt)
    ckpt_morph2 = ["epoch061_0.02777_2.6729.pth"]
    ckpt_morph2.sort()
    ###########################################################################################################
    # name = "epoch074_0.02785_2.6663.pth"
    for name in ckpt_morph2:
        test_mae_morph2.append(
            main({
                "data_dir": data_dir["morph2_align"],
                "ifSE": True,
                "l1loss": True,
                "resume": ckpt + "/" + name
            }))
    test_mae_morph2_print(test_mae_morph2, "morph2_align_sfv2_l1")
    ###########################################################################################################
    end_time = smtp.print_time("全部测试结束!!!共耗时:")
    print(smtp.date_gap(start_time, end_time))
def main():
    start_time = smtp.print_time("开始测试!!!")
    # py脚本额外参数
    args = get_args()
    # main函数传入参数
    img_path = args.img_path
    my_resume = args.my_resume

    if args.opts:
        cfg.merge_from_list(args.opts)

    cfg.freeze()

    # create model_dir
    print("=> creating model_dir '{}'".format(cfg.MODEL.ARCH))
    model = my_model()
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = model.to(device)

    # load checkpoint
    resume_path = my_resume

    if Path(resume_path).is_file():
        print("=> loading checkpoint '{}'".format(resume_path))
        checkpoint = torch.load(resume_path, map_location="cpu")
        model.load_state_dict(checkpoint['state_dict'])
        print("=>ckpt loaded checkpoint '{}'".format(resume_path))
    else:
        raise ValueError(
            "=> no checkpoTrueint found at '{}'".format(resume_path))

    if device == "cuda":
        cudnn.benchmark = True

    print("=> start testing")
    # img_path, img_size, model, device
    predict_age = validate_age_estimation(img_path, cfg.MODEL.IMG_SIZE, model,
                                          device)
    print(f"predict_age: {predict_age:.2f}")
    end_time = smtp.print_time("测试结束!!!")
    print(smtp.date_gap_abs(start_time, end_time))
    return int(round(predict_age))
Example #4
0
def testall():
    start_time = smtp.print_time("全部开始测试!!!")
    tf_log = cfg.tf_log[0]
    ckpt = cfg.ckpt[0]
    data_dir = {
        "morph2": cfg.dataset.morph2,
        "morph2_align": cfg.dataset.morph2_align
    }
    test_mae_morph2 = []
    ###########################################################################################################
    ##################morph2##################
    # ckpt_morph2 = os.listdir(ckpt["morph2"])
    # ckpt_morph2.sort()
    # for name in ckpt_morph2:
    #     test_mae_morph2.append(
    #         main({"data_dir": data_dir["morph2"], "ifSE": False, "l1loss": False,
    #               "resume": ckpt["morph2"] + "/" + name}))
    # test_mae_morph2_print(test_mae_morph2, "morph2")
    ##################morph2_l1##################
    ckpt_morph2 = os.listdir(ckpt["morph2_l1"])
    ckpt_morph2.sort()
    for name in ckpt_morph2:
        test_mae_morph2.append(
            main({
                "data_dir": data_dir["morph2"],
                "ifSE": False,
                "l1loss": True,
                "resume": ckpt["morph2_l1"] + "/" + name
            }))
    test_mae_morph2_print(test_mae_morph2, "morph2_l1")
    ##################morph2_sfv2##################
    # ckpt_morph2 = os.listdir(ckpt["morph2_sfv2"])
    # ckpt_morph2.sort()
    # for name in ckpt_morph2:
    #     test_mae_morph2.append(
    #         main({"data_dir": data_dir["morph2"], "ifSE": True, "l1loss": False,
    #               "resume": ckpt["morph2_sfv2"] + "/" + name}))
    # test_mae_morph2_print(test_mae_morph2, "morph2_sfv2")
    ##################morph2_sfv2_l1##################
    ckpt_morph2 = os.listdir(ckpt["morph2_sfv2_l1"])
    ckpt_morph2.sort()
    for name in ckpt_morph2:
        test_mae_morph2.append(
            main({
                "data_dir": data_dir["morph2"],
                "ifSE": True,
                "l1loss": True,
                "resume": ckpt["morph2_sfv2_l1"] + "/" + name
            }))
    test_mae_morph2_print(test_mae_morph2, "morph2_sfv2_l1")
    ###########################################################################################################
    ##################morph2_align##################
    # ckpt_morph2 = os.listdir(ckpt["morph2_align"])
    # ckpt_morph2.sort()
    # for name in ckpt_morph2:
    #     test_mae_morph2.append(
    #         main({"data_dir": data_dir["morph2_align"], "ifSE": False, "l1loss": False,
    #               "resume": ckpt["morph2_align"] + "/" + name}))
    # test_mae_morph2_print(test_mae_morph2, "morph2_align")
    ##################morph2_align_l1##################
    ckpt_morph2 = os.listdir(ckpt["morph2_align_l1"])
    ckpt_morph2.sort()
    for name in ckpt_morph2:
        test_mae_morph2.append(
            main({
                "data_dir": data_dir["morph2_align"],
                "ifSE": False,
                "l1loss": True,
                "resume": ckpt["morph2_align_l1"] + "/" + name
            }))
    test_mae_morph2_print(test_mae_morph2, "morph2_align_l1")
    ##################morph2_align_sfv2##################
    # ckpt_morph2 = os.listdir(ckpt["morph2_align_sfv2"])
    # ckpt_morph2.sort()
    # for name in ckpt_morph2:
    #     test_mae_morph2.append(
    #         main({"data_dir": data_dir["morph2_align"], "ifSE": True, "l1loss": False,
    #               "resume": ckpt["morph2_align_sfv2"] + "/" + name}))
    # test_mae_morph2_print(test_mae_morph2, "morph2_align_sfv2")
    ##################morph2_align_sfv2_l1##################
    ckpt_morph2 = os.listdir(ckpt["morph2_align_sfv2_l1"])
    ckpt_morph2.sort()
    for name in ckpt_morph2:
        test_mae_morph2.append(
            main({
                "data_dir": data_dir["morph2_align"],
                "ifSE": True,
                "l1loss": True,
                "resume": ckpt["morph2_align_sfv2_l1"] + "/" + name
            }))
    test_mae_morph2_print(test_mae_morph2, "morph2_align_sfv2_l1")
    ###########################################################################################################
    end_time = smtp.print_time("全部测试结束!!!共耗时:")
    print(smtp.date_gap(start_time, end_time))
            "val_loss_list: ": val_loss_list,
            "MODEL.IMG_SIZE: ": cfg.MODEL.IMG_SIZE,
            "BATCH_SIZE: ": cfg.BATCH_SIZE,
            "LOSS.l1: ": l1loss,
            "TRAIN.LR: ": cfg.TRAIN.LR,
            "TRAIN.LR_DECAY_STEP: ": cfg.TRAIN.LR_DECAY_STEP,
            "TRAIN.LR_DECAY_RATE:": cfg.TRAIN.LR_DECAY_RATE,
            "TRAIN.OPT: ": cfg.TRAIN.OPT,
            "MODEL.ARCH:": cfg.MODEL.ARCH
        })
    return best_val_mae


if __name__ == '__main__':
    # ceface数据集训练
    start_time = smtp.print_time("全部开始训练!!!")
    # get_args()
    tf_log = cfg.dataset.ceface_align_tflog
    ckpt = cfg.dataset.ceface_align_ckpt
    data_dir = cfg.dataset.ceface_align
    ###########################################################################################################
    main({
        "data_dir": data_dir,
        "tensorboard": tf_log,
        "checkpoint": ckpt,
        "ifSE": True,
        "l1loss": False
    })
    time.sleep(180)
    main({
        "data_dir": data_dir,