<jupyter_start><jupyter_text>Demo of PatchMatch with Pytorch on Images This implementation is made to take 4 images as input for Deep Image AnalogyTo see a demo on two images, just copy paste the same two images as 4 inputs and set `config['distance_mode'] = 'unidirectional'`<jupyter_code>config = {}
config['patch_size'] = {1:5, 2:5, 3:3, 4:3, 5:3}
config['n_iter'] = 1
config['distance_mode'] = 'unidirectional' # 'unidirectional' or 'bidirectional'
config['random_search_max_step'] = {1:4, 2:4, 3:6, 4:6, 5:14} 

# Load the images
im1 = plt.imread(os.path.join("Images_for_tests", "image1.jpg"))
im2 = plt.imread(os.path.join("Images_for_tests", "image2.jpg"))

# Chagne images shape from [heigth, width, channels] to [channels, heigth, width]
imgs = [np.transpose(im1, axes=(2,0,1)), np.transpose(im2, axes=(2,0,1))]

# Turns them into autograd.Variables
imgs_torch = [Variable(torch.from_numpy(img).unsqueeze(0).float()) for img in imgs]

# Runs the PatchMatch algorithm
NNF_ab = DeepPatchMatch.computeNNF(imgs_torch[0], imgs_torch[1], imgs_torch[0], imgs_torch[1], 1, config)

# Reconverting NNF_ab to a ndarray of shape [heigth, width, channels]
NNF = np.transpose(NNF_ab.numpy(), axes=(1,2,0))

# Displays the original images and resulting Warp
plt.figure(figsize=(16, 12))
plt.subplot(131)
plt.imshow(im1)
plt.title("Image A")
plt.axis("off")

plt.subplot(132)
plt.imshow(im2)
plt.title("Image B")
plt.axis("off")

plt.subplot(133)
plt.imshow(im2[NNF[:,:,0], NNF[:,:,1], :])
plt.title("Warp")
plt.axis("off")
plt.show()

# Displays the NNF (mapping function)
zer = np.zeros(shape=(NNF.shape[0], NNF.shape[1], 1), dtype=NNF.dtype)
colorNNF = np.concatenate((np.expand_dims(NNF[:,:,0], 2), zer, np.expand_dims(NNF[:,:,1], 2)), axis=2)

plt.imshow(colorNNF.astype(np.uint8))
plt.title("NNF")
plt.axis("off")
plt.show()<jupyter_output>Row : 100
Row : 200
Row : 300
Row : 400
PatchMatch done!
{'fraction_non_alphanumeric': 0.17606747496046388, 'fraction_numerical': 0.04428044280442804, 'mean_word_length': 6.385214007782102, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '414582', 'license': 'MIT', 'n_tokens_mistral': 828, 'n_tokens_neox': 773, 'n_words': 174, 'path': 'Tests/PatchMatch-Pytorch.ipynb', 'repo_name': 'julienroyd/Pytorch-DeepImageAnalogy', 'size': '1897'}