def test_shuffle_shreds(self): im_path = os.path.join(self.__class__.shredder_tests_root, 'fish_orig.JPEG') im_grayscaled = cv2.cvtColor(cv2.imread(im_path), cv2.COLOR_RGB2GRAY) t = 5 shuffled = Shredder.shred(im_grayscaled, t, shuffle_shreds=True) reconstructed = Shredder.reconstruct(shuffled) trimmed = im_grayscaled[0:reconstructed.shape[0], 0:reconstructed.shape[1]] assert np.linalg.norm(trimmed - reconstructed) > 1000 # just some magic number
def test_shreds_both_color_and_grayscaled(self): im_path = os.path.join(self.__class__.shredder_tests_root, 'fish_orig.JPEG') im_color = cv2.imread(im_path) im_grayscaled = cv2.cvtColor(im_color, cv2.COLOR_RGB2GRAY) t = 5 res_from_color = Shredder.shred(im_color, t) res_from_grayscaled = Shredder.shred(im_grayscaled, t) [np.testing.assert_equal(c1, c2) for c1, c2 in zip(res_from_grayscaled, res_from_color)]
def shred_shuffle_and_reconstruct(x, t): f = lambda v: Shredder.reconstruct(Shredder.shred(v, t, shuffle_shreds=True)) if type(x) == np.ndarray: if x.ndim == 2: return f(x) assert x.ndim == 3, 'Invalid dimensions' return list_of_images_to_numpy([f(im) for im in x[:]]) else: # Here, we got a list of images with different size return [f(v) for v in x]
def test_reconstruction(self): for im_path in [os.path.join(self.__class__.shredder_tests_root, 'fish_orig.JPEG'), os.path.join(self.__class__.shredder_tests_root, 'doc_orig.jpg')]: im = cv2.imread(im_path) im_grayscaled = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) for t in (2, 4, 5): shredded = Shredder.shred(im, t) reconstructed = Shredder.reconstruct(shredded) trimmed = im_grayscaled[0:reconstructed.shape[0], 0:reconstructed.shape[1]] np.testing.assert_equal(reconstructed, trimmed)
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
def test_shreds_proper_sizes_and_number_of_crops(self): for im_path in [os.path.join(self.__class__.shredder_tests_root, 'fish_orig.JPEG'), os.path.join(self.__class__.shredder_tests_root, 'doc_orig.jpg')]: im = cv2.imread(im_path) for t in (2, 4, 5): shredded = Shredder.shred(im, t) assert len(shredded) == t ** 2 for crop in shredded: assert crop.shape == (im.shape[0] // t, im.shape[1] // t)
def predict(images): reconstructed_image = Shredder.reconstruct(images) print('It is ', end='') if fish_or_doc_classifier.is_fish([reconstructed_image])[0]: print('image') solver_with_comparator = image_type_to_solver_with_comparator[ ImageType.IMAGES] else: print('document') solver_with_comparator = image_type_to_solver_with_comparator[ ImageType.DOCUMENTS] labels = solver_with_comparator.predict(images) return labels
def visualize_crops(crops_list, show=False, save_path=None): t = round(math.sqrt(len(crops_list))) grid_color = 255 img = Shredder.reconstruct(crops_list) if t > 1: dx = img.shape[0] // t dy = img.shape[1] // t img[:, ::dy] = grid_color img[::dx, :] = grid_color plt.imshow(img, cmap='gray') plt.axis('off') if show: plt.show() if save_path is not None: plt.savefig(save_path) plt.clf()
def evaluate_image_for_permutation(self, shred_index_to_image, permutation, sample_index=None): t = int(round(math.sqrt(len(permutation)))) if isinstance(shred_index_to_image, str): shred_index_to_image = DataProvider.read_image( shred_index_to_image) if np.shape(shred_index_to_image)[0] != len(permutation): shred_index_to_image = Shredder.shred(shred_index_to_image, t) shreds_permuted = shred_index_to_image[permutation] permutation_predicted = self.predict(shreds_permuted) current_accuracy = np.average(permutation_predicted == permutation) if not np.isclose(current_accuracy, 1.0): print('On #{} 0-1 is {}: {}!={}'.format(sample_index, current_accuracy, permutation, permutation_predicted)) visualize = True if visualize: directory_path = 'problems/{}/{}'.format(t, self._image_type) os.makedirs(directory_path, exist_ok=True) time_stamp = int(time.time()) Visualizer.visualize_crops( shreds_permuted[np.argsort(permutation)], show=False, save_path=os.path.join( directory_path, '{}-original.png'.format(time_stamp))) Visualizer.visualize_crops( shreds_permuted[np.argsort(permutation_predicted)], show=False, save_path=os.path.join( directory_path, '{}-restored.png'.format(time_stamp))) print('visualized') return current_accuracy
def compute(crop_position_in_original_image, left_index_to_right_index_to_probability, top_index_to_bottom_index_to_probability): t = int(round(math.sqrt(len(crop_position_in_original_image)))) row_to_column_to_crop_index = \ Shredder.shred_index_to_original_index_to_row_to_column_to_shred_index(crop_position_in_original_image) objective = 1.0 log_objective = 0.0 for row in range(t): for column in range(t): if row + 1 < t: top_index = row_to_column_to_crop_index[row][column] bottom_index = row_to_column_to_crop_index[row + 1][column] objective *= top_index_to_bottom_index_to_probability[ top_index][bottom_index] try: log_objective += math.log( top_index_to_bottom_index_to_probability[top_index] [bottom_index]) except ValueError: log_objective = float("-inf") if column + 1 < t: left_index = row_to_column_to_crop_index[row][column] right_index = row_to_column_to_crop_index[row][column + 1] objective *= left_index_to_right_index_to_probability[ left_index][right_index] # TODO: bug bug bug, fails when executing log!!!! try: log_objective += math.log( left_index_to_right_index_to_probability[ left_index][right_index]) except ValueError: log_objective = float("-inf") return objective, log_objective
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)
def evaluate(self, images: list, ts=(2, 4, 5), epochs=1): index_to_accuracy = list() for t in ts: sample_index_to_shred_index_to_image = [ Shredder.shred(image, t) for image in images ] accuracies = list() for epoch in range(epochs): for sample_index, shred_index_to_image in enumerate( sample_index_to_shred_index_to_image): permutation = np.random.permutation(t**2) # permutation = np.arange(t ** 2) current_accuracy = self.evaluate_image_for_permutation( shred_index_to_image, permutation, sample_index) accuracies.append(current_accuracy) current_accuracy = np.average(accuracies) print('For t={} and image_type={} 0-1 is {}'.format( t, self._image_type, current_accuracy)) index_to_accuracy.append(current_accuracy) return np.average(index_to_accuracy)
def shred_and_resize_to_single_image(image): return resize_to(Shredder.shred(image, t), resize_shape)