<jupyter_start><jupyter_text>CHAPTER 4  Section 1Learning how to represent common data formats into usable tensors for Deep Learning models.<jupyter_code>import imageio
import torch
from matplotlib import pyplot as plt
# imageio can be used to easily read in PNG and JPG images into tensors
img = imageio.imread('/Users/ramanshsharma/Downloads/goodimageofme.jpg')
img.shape
type(img)
plt.imshow(img)
arr = torch.from_numpy(img)
arr.shape # H x W x C
arr = arr.permute(2, 0, 1)
arr.shape # C x H x W<jupyter_output><empty_output><jupyter_text>example thing<jupyter_code>arr = arr.float()
arr /= 255.
arr # normalized
# practicing loading multiple images from disk
import os
data_dir = "../Chapter_4/imgs/"
filenames = [name for name in os.listdir(data_dir) if os.path.splitext(name)[-1] == '.png']
batch = torch.zeros(3, 3, 1800, 2880, dtype=torch.uint8) # most common bit size for images is uint8
batch.size()
for i, img in enumerate(filenames):
    read = imageio.imread(os.path.join(data_dir, img))
    arr = torch.from_numpy(read)
    arr = arr.permute(2, 0, 1) # reshaping as earlier to C x H x W
    # some images have more than 3 channels. So to remove other than 3 channels
    arr = arr[:3].to(torch.uint8)
    batch[i] = arr
# preprocessing that is requires in neural networks
batch = batch.float()
print(batch.dtype)
# either we can do
# batch /= 255.0 for image normalization
# or

for c in range(batch.shape[1]): # over all channels
    mean = torch.mean(batch[:, c]) # mean over all examples, over channel c
    std = torch.std(batch[:, c]) # std over all examples, over channel c
    batch[:, c] = (batch[:, c] - mean) / std

fig = plt.gcf()
fig.set_size_inches(15.5, 7.5)
plt.imshow(batch[2].permute(1, 2, 0))
# practicing one-hot vectors
a = torch.tensor([0, 1, 2, 4, 4, 5, 6, 7, 8, 9], dtype=torch.long)
a = a.long()
print(a.dtype)
a.shape
onehotvec = torch.zeros(a.shape[0], 13)
onehotvec.shape
a.unsqueeze(1)
onehotvec.scatter_(1, a.unsqueeze(1), 1)<jupyter_output><empty_output><jupyter_text>Exploring advanced boolean indexing<jupyter_code>a = torch.rand(1, 1000)
a
a = a < 0.5
a.shape, a.dtype, torch.sum(a, 1)
torch.cat([torch.tensor([1, 2, 3, 4]), torch.tensor([12, 13, 45, 56])], axis=0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13693131132917039, 'fraction_numerical': 0.03612845673505798, 'mean_word_length': 4.497549019607843, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '663273', 'license': 'MIT', 'n_tokens_mistral': 889, 'n_tokens_neox': 829, 'n_words': 253, 'path': 'Chapter_4/Chapter_4.ipynb', 'repo_name': 'ramanshsharma2806/pytorch_practice', 'size': '2242'}