How to Shift image(data augmentation)

111455: <p>I want to implement the data augmentation like below image.<br>
I found the similar topic, however I could not find the solutions by using kornia<br>
<a href="https://discuss.pytorch.org/t/is-there-any-function-to-shift-image-by-x-axis-or-y-asix/80838">https://discuss.pytorch.org/t/is-there-any-function-to-shift-image-by-x-axis-or-y-asix/80838</a></p>
<p>Is there any data augmentation method to shift image by x axis or y asix?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/e/7/e75d11d9dda9bf65e1621421924ff28ff7f0baad.jpeg" alt="kRaqh" data-base62-sha1="x0JHbVFnanx9agwwViJl4hY7gPH" width="447" height="338"></p>

user_123454321: <p>Something like this ?</p>
<pre><code class="lang-auto">import torch
import kornia
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

img = np.array(Image.open('pepper.png'))

img_tensor = torch.tensor(img.transpose([2, 0, 1])).float()

fig = plt.figure(figsize=(15, 15))
for i in range(4):
  for j in range(4):
      ax = plt.subplot(4, 4, i*4 + j +1)
      img_translated = kornia.random_affine(
          img_tensor,
          degrees=0,
          translate=(0.3, 0.3)
      )
      ax.imshow(img_translated.squeeze().permute(1, 2, 0).byte())
</code></pre>
<p>Output:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c9c344044eef9c5bc1620ee99a08c18672c6908a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c9c344044eef9c5bc1620ee99a08c18672c6908a" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9c344044eef9c5bc1620ee99a08c18672c6908a_2_545x500.jpeg" alt="image" data-base62-sha1="sMSkezSDnyCAt7g5LNjL7NlvR9g" width="545" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9c344044eef9c5bc1620ee99a08c18672c6908a_2_545x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9c344044eef9c5bc1620ee99a08c18672c6908a_2_817x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/9/c9c344044eef9c5bc1620ee99a08c18672c6908a.jpeg 2x" data-dominant-color="341D16"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">882×809 154 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

111455: <p>Thanks for quickly response.<br>
Can I change shift mode from ‘zeros’ to ‘nearest’ as shown in below image?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/0/20cfd59739ea9e2a568396a8f78da26e0c8281b0.jpeg" alt="width_shift_range" data-base62-sha1="4GgyQBoxgu6Kfuvgol376idDV1S" width="347" height="285"></p>

user_123454321: <p>That function does not seem to have padding_mode, but one can instantiate a RandomAffine class to get the padding mode like this</p>
<pre><code class="lang-auto">affine = kornia.augmentation.RandomAffine(degrees=0, translate=(0.3, 0.3), padding_mode='border')  
fig = plt.figure(figsize=(15, 15))
for i in range(4):
  for j in range(4):
      ax = plt.subplot(4, 4, i*4 + j +1)
      img_translated = affine(img_tensor)
      ax.imshow(img_translated.squeeze().permute(1, 2, 0).byte())
</code></pre>