<jupyter_start><jupyter_text>Transformation<jupyter_code>%matplotlib inline
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from torchvision.transforms import *

np.random.seed(37)

image = Image.open('./shapes/train/circle/0001.jpg', 'r')
print('width ', image.width, 'height ', image.height)

plt.imshow(image)<jupyter_output>width  144 height  144<jupyter_text>PIL transforms  Crop<jupyter_code>t1 = CenterCrop(100)
t2 = CenterCrop(144)
t3 = CenterCrop(175)

fig, axes = plt.subplots(1, 3, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))
ax[2].imshow(t3(image))

ax[0].set_title('Zoom In')
ax[1].set_title('Normal')
ax[2].set_title('Zoom Out')
t1 = RandomCrop(size=(144, 144), padding=1)
t2 = RandomCrop(size=(144, 144), padding=5)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>ColorJitter<jupyter_code>t1 = ColorJitter(brightness=10, contrast=0, saturation=0, hue=0.5)
t2 = ColorJitter(brightness=10, contrast=0, saturation=0.1, hue=0.5)
t3 = ColorJitter(brightness=10, contrast=0.5, saturation=0.1, hue=0.5)

fig, axes = plt.subplots(1, 3, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))
ax[2].imshow(t3(image))<jupyter_output><empty_output><jupyter_text>Grayscale<jupyter_code>t1 = Grayscale(num_output_channels=1)
t2 = Grayscale(num_output_channels=3)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))
t1 = RandomGrayscale(p=0.1)
t2 = RandomGrayscale(p=0.6)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>Pad<jupyter_code>t1 = Pad(5)
t2 = Pad(10)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomAffine<jupyter_code>t1 = RandomAffine(degrees=(0, 30))
t2 = RandomAffine(degrees=(30, 50), shear=3)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomApply<jupyter_code>t1 = RandomApply([CenterCrop(175), 
                  RandomAffine(degrees=(0, 30)), 
                  Pad(3)], p=0.5)
t1 = RandomApply([CenterCrop(175), 
                  RandomAffine(degrees=(0, 30)), 
                  Pad(3)], p=0.5)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomChoice<jupyter_code>t1 = RandomChoice([CenterCrop(175), 
                  RandomAffine(degrees=(0, 30)), 
                  Pad(3)])
t1 = RandomChoice([CenterCrop(175), 
                  RandomAffine(degrees=(0, 30)), 
                  Pad(3)])

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomFlip<jupyter_code>t1 = RandomHorizontalFlip(p=0.99)
t2 = RandomHorizontalFlip(p=0.99)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomOrder<jupyter_code>t1 = RandomOrder([CenterCrop(175), 
                  RandomAffine(degrees=(0, 30)), 
                  Pad(3)])
t1 = RandomOrder([CenterCrop(175), 
                  RandomAffine(degrees=(0, 30)), 
                  Pad(3)])

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomPerspective<jupyter_code>t1 = RandomPerspective(distortion_scale=0.8, p=0.99)
t2 = RandomPerspective(distortion_scale=0.8, p=0.99)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>RandomRotation<jupyter_code>t1 = RandomRotation(degrees=(45, 90))
t2 = RandomRotation(degrees=(45, 90))

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>Resize<jupyter_code>t1 = Resize(size=(200, 200))
t2 = Resize(size=(130, 130))

fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))<jupyter_output><empty_output><jupyter_text>Tensor transforms<jupyter_code>import torch

data = ToTensor()(image)
c, h, w = data.shape[0], data.shape[1], data.shape[2]<jupyter_output><empty_output><jupyter_text>Normalize<jupyter_code>t = Normalize(mean=[0.5, 0.5, 0.5], std=[1., 1., 1.])
o = t(data)
o.shape<jupyter_output><empty_output><jupyter_text>RandomErasing<jupyter_code>t = RandomErasing(p=0.5)
o = t(data)
o.shape<jupyter_output><empty_output><jupyter_text>Conversion transforms  PIL image<jupyter_code>t1 = transforms.Compose([
    ToTensor(),
    ToPILImage()
])
t2 = transforms.Compose([
    ToTensor(),
    Normalize(mean=[0.5, 0.5, 0.5], std=[1., 1., 1.]),
    ToPILImage()
])
t3 = transforms.Compose([
    ToTensor(),
    RandomErasing(p=0.9),
    ToPILImage()
])

fig, axes = plt.subplots(1, 3, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(t1(image))
ax[1].imshow(t2(image))
ax[2].imshow(t3(image))

ax[0].set_title('Normal')
ax[1].set_title('Normalize')
ax[2].set_title('Erasing')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18857039841355688, 'fraction_numerical': 0.06489994591671173, 'mean_word_length': 5.741190765492102, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 68, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '574198', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2373, 'n_words': 326, 'path': 'sphinx/pytorch-intro/source/transformation.ipynb', 'repo_name': 'oneoffcoder/books', 'size': '5547'}