示例#1
0
    def __get_attempt(self, exp, feature_vectors, which_metric):
        # index reference for set creation
        CMC = MetricRunner.CMC
        STR = MetricRunner.STR
        # index reference for cosine_distances which is in the format of [(car_id, cosine_distance)]
        CHOSEN_CAR_INDEX = 0
        COMP_CAR_INDEX = 1
        COSINE_DISTANCE_INDEX = 2

        logging.info("Generate set of images")
        camsets = exp.generate()

        logging.info("Match target car to its respective vector")
        target_car_filename = utils.get_basename(exp.target_car.filepath)
        logging.info("target {}".format(target_car_filename))
        target_car_vector = feature_vectors[target_car_filename]

        logging.info("Identify chosen set vs comparison set")
        # chosen
        chosen_set = {
            CMC: [exp.target_car],
            STR: camsets[0],
        }.get(which_metric)
        # comparison
        comp_sets = {
            CMC: camsets,
            STR: camsets[1:],
        }.get(which_metric)

        logging.info("Calculate cosine distances between the sets")
        cosine_distances = list()
        for chosen_car in chosen_set:
            logging.info(">> Match chosen car to its respective vector")
            chosen_car_filename = utils.get_basename(chosen_car.filepath)
            chosen_car_vector = feature_vectors[chosen_car_filename]
            for comp_set in comp_sets:
                for comp_car in comp_set:
                    logging.info(">> Match comparison car to its respective vector")
                    comp_car_filename = utils.get_basename(comp_car.filepath)
                    comp_car_vector = feature_vectors[comp_car_filename]
                    logging.info(">> Calculate the cosine distance")
                    cosine_distance = scipy.spatial.distance.cosine(chosen_car_vector, comp_car_vector)
                    cosine_distances.append((chosen_car.car_id, comp_car.car_id, cosine_distance))
                    logging.info(">> chosen {}, comp {}, cosine distance {}".format(chosen_car.filepath, comp_car.filepath, cosine_distance))
        
        logging.info("Sort the cosine distances")
        cosine_distances = sorted(cosine_distances, key=lambda tupl:tupl[COSINE_DISTANCE_INDEX])

        logging.info("Determine how many times we have to go through the sorted list to find the matching target car")
        attempt = {
            CMC: utils.get_index_of_tuple(cosine_distances, COMP_CAR_INDEX, exp.target_car.car_id),
            STR: utils.get_index_of_pairs(cosine_distances, CHOSEN_CAR_INDEX, COMP_CAR_INDEX, exp.target_car.car_id),
        }.get(which_metric)

        return attempt
示例#2
0
def main(args):
    # extract arguments from command line
    dataset_path = args.dataset_path
    set_type = args.set_type
    dataset_type = args.dataset_type
    dataset = chip.DatasetFactory.create_dataset(dataset_type, dataset_path,
                                                 set_type)
    num_cams = args.num_cams
    num_cars_per_cam = args.num_cars_per_cam
    drop_percentage = args.drop_percentage

    seed = args.seed

    # create the generator
    exp = ExperimentGenerator(dataset, num_cams, num_cars_per_cam,
                              drop_percentage, seed)

    # generate the experiment
    set_num = 1
    print("=" * 80)
    for camset in exp.generate():
        print("Set #{}".format(set_num))
        print("Target car: {}".format(
            utils.get_basename(exp.target_car.filepath)))
        print("-" * 80)
        for image in camset:
            print("filepath: {}".format(image.filepath))
            print("car id: {}".format(image.car_id))
            print("camera id: {}".format(image.cam_id))
            print("timestamp: {}".format(utils.get_timestamp(image.time)))
            if image.misc is not None:
                for key, value in image.misc.items():
                    print("{}: {}".format(key, value))
            print("-" * 80)
        print("=" * 80)
        set_num = set_num + 1
    return
示例#3
0
def test_get_basename():
    TEST_FILEPATHS = (("/path/to/file/hello.py", "hello.py"), ("hello.py",
                                                               "hello.py"))

    for test_input, answer in TEST_FILEPATHS:
        assert answer == utils.get_basename(test_input)