def main():
    args = parse_opts()
    set_random_seed(args.random_seed)

    logger_name = "retrieval_data"
    retrieval_dataset_name_suffix = "-retrieval"
    logger = setup_logger(logger_name, None)
    data_path = get_data_path()

    script_path = os.path.expanduser(os.path.dirname(os.path.abspath(__file__)))
    target_path = os.path.join(script_path, "cnnimageretrieval-pytorch", "data")
    mkdir(target_path)

    dataset_train = build_dataset_by_name(data_path, args.dataset_train,
                                        eval_scale=args.dataset_train_scale,
                                        logger_prefix=logger_name)
    retrieval_dataset_train_name = dataset_train.get_name() + retrieval_dataset_name_suffix

    dataset_val = build_dataset_by_name(data_path, args.dataset_val,
                                        eval_scale=args.dataset_val_scale,
                                        logger_prefix=logger_name)
    retrieval_dataset_val_name = dataset_val.get_name() + retrieval_dataset_name_suffix


    datasets_test = []
    retrieval_dataset_test_names = []
    if args.datasets_test:
        if len(args.datasets_test_scale) == 1:
            datasets_test_scale = args.datasets_test_scale * len(args.datasets_test)
        else:
            datasets_test_scale = args.datasets_test_scale
        assert len(args.datasets_test) == len(datasets_test_scale), "Arg datasets-test-scale should be of len 1 or of len equal to the len of datasets-test"

        for dataset_name, scale in zip(args.datasets_test, datasets_test_scale):
            dataset = build_dataset_by_name(data_path, dataset_name,
                                            eval_scale=scale,
                                            logger_prefix=logger_name)
            retrieval_dataset_test_names.append(dataset.get_name() + retrieval_dataset_name_suffix)
            datasets_test.append(dataset)

    # create dataset
    if args.num_random_crops_per_image > 0:
        crop_suffix = f"-rndCropPerImage{args.num_random_crops_per_image}"
        retrieval_dataset_train_name = retrieval_dataset_train_name + crop_suffix
        retrieval_dataset_val_name = retrieval_dataset_val_name + crop_suffix
        retrieval_dataset_test_names = [name + crop_suffix for name in retrieval_dataset_test_names]

    prepare_dataset(target_path,
                    retrieval_dataset_train_name, dataset_train,
                    retrieval_dataset_val_name, dataset_val,
                    args.iou_pos_threshold, args.iou_neg_threshold,
                    args.num_queries_image_to_image,
                    logger,
                    retrieval_dataset_test_names=retrieval_dataset_test_names, datasets_test=datasets_test,
                    num_random_crops_per_image=args.num_random_crops_per_image)
Example #2
0
def main():
    target_object_size = 240
    data_path = get_data_path()
    logger = setup_logger("get_dataset_scales", None)

    for name in DATASET_LIST:
        dataset = build_dataset_by_name(data_path, name, eval_scale=None, logger_prefix="get_dataset_scales")

        image_sizes_by_id = get_image_sizes(dataset)
        average_size, median, q10, q90 = compute_average_object_size(dataset.gtboxframe, image_sizes_by_id)
        print("Average size of object = {0:0.2f} for image size = {1}".format(average_size, dataset.image_size))
        print("Median = {0:0.2f}, q10 = {1:0.2f}, q90 = {2:0.2f}".format(median, q10, q90))
        print("To get objects to size {0}, images should be of size {1:d}".format(target_object_size, int(dataset.image_size * target_object_size / median)))
Example #3
0
def init_logger(args, logger_prefix="detector-retrieval"):
    if args.output_dir:
        mkdir(args.output_dir)

    logger = setup_logger(logger_prefix, args.output_dir if args.output_dir else None)

    if args.config_file:
        with open(args.config_file, "r") as cf:
            config_str = "\n" + cf.read()
            logger.info(config_str)
        logger.info("Running with the default eval section:\n{}".format(cfg.eval))
    else:
        logger.info("Launched with no OS2D config file")
    logger.info("Running args:\n{}".format(args))
    return logger
def init_logger(cfg, config_file):
    output_dir = cfg.output.path
    if output_dir:
        mkdir(output_dir)

    logger = setup_logger("OS2D",
                          output_dir if cfg.output.save_log_to_file else None)

    if config_file:
        logger.info("Loaded configuration file {}".format(config_file))
        with open(config_file, "r") as cf:
            config_str = "\n" + cf.read()
            logger.info(config_str)
    else:
        logger.info("Config file was not provided")

    logger.info("Running with config:\n{}".format(cfg))

    # save config file only when training (to run multiple evaluations in the same folder)
    if output_dir and cfg.train.do_training:
        output_config_path = os.path.join(output_dir, "config.yml")
        logger.info("Saving config into: {}".format(output_config_path))
        # save overloaded model config in the output directory
        save_config(cfg, output_config_path)
Example #5
0
def main():
    args = parse_opts()
    set_random_seed(args.random_seed)
    crop_suffix = f"-rndCropPerImage{args.num_random_crops_per_image}"

    logger_name = "retrieval_data"
    retrieval_dataset_name_suffix = "-retrieval"
    logger = setup_logger(logger_name, None)
    data_path = get_data_path()

    script_path = os.path.expanduser(os.path.dirname(
        os.path.abspath(__file__)))
    target_path = os.path.join(script_path, "cnnimageretrieval-pytorch",
                               "data")
    mkdir(target_path)

    dataset_train = build_dataset_by_name(data_path,
                                          args.dataset_train,
                                          eval_scale=args.dataset_train_scale,
                                          logger_prefix=logger_name)
    retrieval_dataset_train_name = dataset_train.get_name(
    ) + retrieval_dataset_name_suffix

    dataset_val = build_dataset_by_name(data_path,
                                        args.dataset_val,
                                        eval_scale=args.dataset_val_scale,
                                        logger_prefix=logger_name)
    retrieval_dataset_val_name = dataset_val.get_name(
    ) + retrieval_dataset_name_suffix

    if args.datasets_test:
        if len(args.datasets_test_scale) == 1:
            datasets_test_scale = args.datasets_test_scale * len(
                args.datasets_test)
        else:
            datasets_test_scale = args.datasets_test_scale
        assert len(args.datasets_test) == len(
            datasets_test_scale
        ), "Arg datasets-test-scale should ne of len 1 or of len equal to the len of datasets-test"

        datasets_test = []
        retrieval_dataset_test_names = []
        for dataset_name, scale in zip(args.datasets_test,
                                       datasets_test_scale):
            dataset = build_dataset_by_name(data_path,
                                            dataset_name,
                                            eval_scale=scale,
                                            logger_prefix=logger_name)
            retrieval_dataset_test_names.append(dataset.get_name() +
                                                retrieval_dataset_name_suffix)
            datasets_test.append(dataset)

    # prepare data images for train and val
    tgt_image_path_trainval = os.path.join(target_path, "train",
                                           retrieval_dataset_train_name, "ims")
    mkdir(tgt_image_path_trainval)
    logger.info(
        f"Train set {retrieval_dataset_train_name} with no random crops")
    db_images_train = save_cropped_boxes(dataset_train,
                                         tgt_image_path_trainval,
                                         extension="")

    # create val subset: add all boxes from images that have at least one validation box (can add some boxes from train as distractors)
    logger.info(f"Val set {retrieval_dataset_val_name} with no random crops")
    db_images_val = save_cropped_boxes(dataset_val,
                                       tgt_image_path_trainval,
                                       extension="")

    # prepare data images for trainval with crops
    tgt_image_path_trainval_randcrops = os.path.join(
        target_path, "train", retrieval_dataset_train_name + crop_suffix,
        "ims")
    mkdir(tgt_image_path_trainval_randcrops)

    logger.info(
        f"Train set {retrieval_dataset_train_name} with {args.num_random_crops_per_image} crops per image"
    )
    db_images_train_randomCrops = save_cropped_boxes(
        dataset_train,
        tgt_image_path_trainval_randcrops,
        extension="",
        num_random_crops_per_image=args.num_random_crops_per_image)

    # create val subset: add all boxes from images that have at least one validation box (can add some boxes from train as distractors)
    logger.info(
        f"Val set {retrieval_dataset_val_name} with {args.num_random_crops_per_image} crops per image"
    )
    db_images_val_randomCrops = save_cropped_boxes(
        dataset_val,
        tgt_image_path_trainval_randcrops,
        extension="",
        num_random_crops_per_image=args.num_random_crops_per_image)

    # prepare data images for test
    dbs_images_test = {}
    if datasets_test:
        for dataset_test, dataset_name in zip(datasets_test,
                                              retrieval_dataset_test_names):
            tgt_image_path_test = os.path.join(
                target_path, "test", dataset_name, "jpg"
            )  # the folder name should be always "test" - from cirtorch
            mkdir(tgt_image_path_test)
            logger.info(f"Eval dataset: {dataset_name}")
            dbs_images_test[dataset_name] = save_cropped_boxes(
                dataset_test, tgt_image_path_test)

            # prepare data images for test with random crops
            tgt_image_path_test = os.path.join(
                target_path, "test", dataset_name + crop_suffix, "jpg"
            )  # the folder name should be always "test" - from cirtorch
            mkdir(tgt_image_path_test)
            logger.info(f"Eval dataset: {dataset_name + crop_suffix}")
            dbs_images_test[dataset_name + crop_suffix] = save_cropped_boxes(
                dataset_test,
                tgt_image_path_test,
                num_random_crops_per_image=args.num_random_crops_per_image)

    # save GT images from train
    db_classes_train = save_class_images(
        dataset_train,
        os.path.join(target_path, "train", retrieval_dataset_train_name,
                     "ims"),
        extension="")
    db_classes_train_randomCrops = save_class_images(
        dataset_train,
        os.path.join(target_path, "train",
                     retrieval_dataset_train_name + crop_suffix, "ims"),
        extension="")

    # save GT images from val
    db_classes_val = save_class_images(
        dataset_val,
        os.path.join(target_path, "train", retrieval_dataset_train_name,
                     "ims"),
        extension="")
    db_classes_val_randomCrops = save_class_images(
        dataset_val,
        os.path.join(target_path, "train",
                     retrieval_dataset_train_name + crop_suffix, "ims"),
        extension="")

    # save GT images for testing
    dbs_classes_test = {}
    if args.datasets_test:
        for dataset_test, dataset_name in zip(datasets_test,
                                              retrieval_dataset_test_names):
            dbs_classes_test[dataset_name] = save_class_images(
                dataset_test,
                os.path.join(target_path, "test", dataset_name, "jpg"))
            dbs_classes_test[dataset_name + crop_suffix] = save_class_images(
                dataset_test,
                os.path.join(target_path, "test", dataset_name + crop_suffix,
                             "jpg"))

    # merge databases
    logger.info(
        f"Processing trainval set from {retrieval_dataset_train_name} and {retrieval_dataset_val_name}"
    )
    db_train = create_train_database_queries(
        db_images_train,
        db_classes_train,
        iou_pos_threshold=args.iou_pos_threshold,
        iou_neg_threshold=args.iou_neg_threshold,
        logger=logger)
    db_val = create_train_database_queries(
        db_images_val,
        db_classes_val,
        iou_pos_threshold=args.iou_pos_threshold,
        iou_neg_threshold=args.iou_neg_threshold,
        logger=logger)

    logger.info(
        f"Processing trainval set from {retrieval_dataset_train_name} and {retrieval_dataset_val_name} with {args.num_random_crops_per_image} random crops"
    )
    db_train_randomCrops = create_train_database_queries(
        db_images_train_randomCrops,
        db_classes_train_randomCrops,
        iou_pos_threshold=args.iou_pos_threshold,
        iou_neg_threshold=args.iou_neg_threshold,
        logger=logger)
    db_val_randomCrops = create_train_database_queries(
        db_images_val_randomCrops,
        db_classes_val_randomCrops,
        iou_pos_threshold=args.iou_pos_threshold,
        iou_neg_threshold=args.iou_neg_threshold,
        logger=logger)

    dbs_test = {}
    if args.datasets_test:
        for dataset_name in retrieval_dataset_test_names:
            logger.info(
                f"Processing test set {dataset_name} with {args.num_random_crops_per_image} random crops"
            )
            dbs_test[dataset_name] = create_test_database_queries(
                dbs_images_test[dataset_name],
                dbs_classes_test[dataset_name],
                iou_pos_threshold=args.iou_pos_threshold,
                iou_neg_threshold=args.iou_neg_threshold,
                logger=logger)

            logger.info(f"Processing test set {dataset_name + crop_suffix}")
            dbs_test[dataset_name +
                     crop_suffix] = create_test_database_queries(
                         dbs_images_test[dataset_name + crop_suffix],
                         dbs_classes_test[dataset_name + crop_suffix],
                         iou_pos_threshold=args.iou_pos_threshold,
                         iou_neg_threshold=args.iou_neg_threshold,
                         logger=logger)

    # save trainval to disk
    db_trainval = {"train": db_train, "val": db_val}
    db_fn = os.path.join(
        os.path.join(target_path, "train", retrieval_dataset_train_name),
        f"{retrieval_dataset_train_name}.pkl")
    with open(db_fn, "wb") as f:
        pickle.dump(db_trainval, f)

    # save train separately for whitening
    db_fn = os.path.join(
        os.path.join(target_path, "train", retrieval_dataset_train_name),
        f"{retrieval_dataset_train_name}-whiten.pkl")
    with open(db_fn, "wb") as f:
        pickle.dump(db_train, f)

    # save trainval with random crops to disk
    db_trainval_randomCrops = {
        "train": db_train_randomCrops,
        "val": db_val_randomCrops
    }
    db_fn = os.path.join(
        os.path.join(target_path, "train",
                     retrieval_dataset_train_name + crop_suffix),
        f"{retrieval_dataset_train_name}{crop_suffix}.pkl")
    with open(db_fn, "wb") as f:
        pickle.dump(db_trainval_randomCrops, f)

    db_fn = os.path.join(
        os.path.join(target_path, "train",
                     retrieval_dataset_train_name + crop_suffix),
        f"{retrieval_dataset_train_name}{crop_suffix}-whiten.pkl")
    with open(db_fn, "wb") as f:
        pickle.dump(db_train_randomCrops, f)

    # save test to disk
    if args.datasets_test:
        for dataset_name in retrieval_dataset_test_names:
            db_fn = os.path.join(
                os.path.join(target_path, "test", dataset_name),
                f"gnd_{dataset_name}.pkl")
            with open(db_fn, "wb") as f:
                pickle.dump(dbs_test[dataset_name], f)

            # save test with random crops to disk
            db_fn = os.path.join(
                os.path.join(target_path, "test", dataset_name + crop_suffix),
                f"gnd_{dataset_name}{crop_suffix}.pkl")
            with open(db_fn, "wb") as f:
                pickle.dump(dbs_test[dataset_name + crop_suffix], f)