コード例 #1
0
def get_reconstruction_objective_values(dp: DataProvider,
                                        image_type: ImageType, t):
    """
    This function dumps the output of the objective function given a permutation from the greedy comparator
    :return: a tuple of 2 lists of log probabilities (scalars)
    """
    solver = image_type_to_solver_with_comparator[image_type]
    comparator = solver._t_to_comparator[t]
    inputs = dp.get_fish_images(
    ) if image_type == ImageType.IMAGES else dp.get_docs_images()
    inputs = [Shredder.shred(im, t, shuffle_shreds=False) for im in inputs]
    correct_reconstruction_probas = []
    incorrect_reconstruction_probas = []
    for i, stacked_shreds in enumerate(inputs):
        print('#{}-{}-{}'.format(i, image_type, t))
        ltr_adj_probas, ttb_adj_probas = AdjacencyMatrixBuilder.build_adjacency_matrices(
            comparator, stacked_shreds)
        _, correct_log_objective = ObjectiveFunction.compute(
            np.arange(t**2), ltr_adj_probas, ttb_adj_probas)
        predicted_permutation, log_objective = solver.predict(
            stacked_shreds, return_log_objective=True)
        if np.array_equal(predicted_permutation, np.arange(t**2)):
            correct_reconstruction_probas.append(log_objective)
        else:
            incorrect_reconstruction_probas.append(
                (log_objective, correct_log_objective))

    return correct_reconstruction_probas, incorrect_reconstruction_probas
コード例 #2
0
 def prepare_data(self, num_samples, resize=None):
     num_samples = 2000
     dp = DataProvider()
     ts = (1, 2, 4, 5)
     fish = dp.get_fish_images(num_samples=num_samples, resize=resize)
     docs = dp.get_docs_images(num_samples=num_samples, resize=resize)
     fish = list(itertools.chain(*[shred_shuffle_and_reconstruct(fish, t) for t in ts]))
     docs = list(itertools.chain(*[shred_shuffle_and_reconstruct(docs, t) for t in ts]))
     if resize is not None:
         fish = list_of_images_to_numpy(fish)
         docs = list_of_images_to_numpy(docs)
     return fish, docs
コード例 #3
0
def get_adjacent_crops_probabilities(dp: DataProvider, image_type: ImageType,
                                     t):
    """
    This function calculates the output of the comparator on adjacent crops
    :return: list of probabilities (scalars)
    """
    comparator = image_type_to_t_to_comparator[image_type][t]
    inputs = dp.get_fish_images(
    ) if image_type == ImageType.IMAGES else dp.get_docs_images()
    inputs = shred_and_resize_to(inputs, t,
                                 (comparator.width, comparator.height))
    adj_probabilities = []
    for stacked_shreds in inputs:
        for left_idx in range(t**2):
            right_idx = left_idx + 1
            if left_idx % t == t - 1:
                continue
            softmax = comparator.predict_is_left_probability(
                [stacked_shreds[left_idx]], [stacked_shreds[right_idx]])
            adj_probabilities.append(softmax[0][1])

    return adj_probabilities
コード例 #4
0
def get_non_adjacent_crops_probabilities(dp: DataProvider,
                                         image_type: ImageType, t):
    """
    This function calculates the output of the comparator on non adjacent crops
    :return: list of probabilities (scalars)
    """
    comparator = image_type_to_t_to_comparator[image_type][t]
    inputs = dp.get_fish_images(
    ) if image_type == ImageType.IMAGES else dp.get_docs_images()
    inputs = shred_and_resize_to(inputs, t,
                                 (comparator.width, comparator.height))
    non_adj_probabilities = []
    for stacked_shreds in inputs:
        left_idx, right_idx = 0, 1
        while left_idx + 1 == right_idx:
            left_idx, right_idx = tuple(
                np.random.choice(t**2, 2, replace=False))
        softmax = comparator.predict_is_left_probability(
            [stacked_shreds[left_idx]], [stacked_shreds[right_idx]])
        non_adj_probabilities.append(softmax[0][1])

    return non_adj_probabilities
コード例 #5
0
def get_number_of_images_with_same_patches_and_number_of_same_patches(
        data_provider: DataProvider,
        image_type: ImageType,
        t: int,
        width_height=None):
    inputs = data_provider.get_fish_images() if image_type == ImageType.IMAGES else data_provider.get_docs_images()

    if width_height is None:
        inputs = list(map(lambda image: Shredder.shred(image, t), inputs))
    else:
        inputs = shred_and_resize_to(inputs, t, width_height)

    number_of_pictures_with_same_patches = 0
    number_of_patches_with_similar_in_same_picture = 0

    for stacked_shreds in inputs:
        picture_has_similar_patches = False

        for left_shred in range(t**2):
            picture_has_similar_to_this_shred = False

            for right_shred in range(t**2):

                if left_shred != right_shred and np.all(stacked_shreds[left_shred] == stacked_shreds[right_shred]):
                    picture_has_similar_to_this_shred = True

            if picture_has_similar_to_this_shred:
                picture_has_similar_patches = True
                number_of_patches_with_similar_in_same_picture += 1

        if picture_has_similar_patches:
            number_of_pictures_with_same_patches += 1

    return \
        number_of_pictures_with_same_patches, \
        number_of_patches_with_similar_in_same_picture, \
        len(inputs), \
        len(inputs) * (t ** 2)