<jupyter_start><jupyter_text>`image-ops`Task: perform broadcast and reduction operations on a tensor representing a batch of color images  Setup<jupyter_code>from fastai.vision.all import *

# Make one-channel images display in greyscale.
# See https://forums.fast.ai/t/show-image-displays-color-image-for-mnist-sample-dataset/78932/4
# But "Grays" is inverted, so we use "gray" instead.
matplotlib.rc('image', cmap='gray')<jupyter_output><empty_output><jupyter_text>Download dataset.<jupyter_code>path = untar_data(URLs.PETS) / "images"<jupyter_output><empty_output><jupyter_text>Make a stable order for the images: first sort, then randomize using a known seed.<jupyter_code>set_seed(333)
image_files = get_image_files(path).sorted().shuffle()<jupyter_output><empty_output><jupyter_text>Define how we're going to split the data into a training and validation set.<jupyter_code>splitter = RandomSplitter(valid_pct=0.2, seed=42)<jupyter_output><empty_output><jupyter_text>In this dataset, cat breeds start with a capital letter, so we can get the label from the filename.<jupyter_code>def cat_or_dog(x):
    return 'cat' if x[0].isupper() else 'dog'

def get_y(file_path):
    return cat_or_dog(file_path.name)<jupyter_output><empty_output><jupyter_text>Define a standard image-classification `DataBlock`.<jupyter_code>dblock = DataBlock(blocks    = (ImageBlock, CategoryBlock),
                   get_y     = get_y,
                   splitter  = splitter,
                   item_tfms = Resize(224))<jupyter_output><empty_output><jupyter_text>Override `shuffle_fn` so that the images never actually get shuffled (batch order is consistent).<jupyter_code>dataloaders = dblock.dataloaders(image_files, batch_size=9, shuffle_fn=lambda idxs: idxs)<jupyter_output><empty_output><jupyter_text>Since we set the `shuffle_fn` to the identity above, the images will always get loaded in the same order, so the first batch will always be the same:<jupyter_code>batch = dataloaders.train.one_batch()
images_orig, labels = batch
images = images_orig.clone()
show_image_batch((images, labels))<jupyter_output><empty_output><jupyter_text>Task 1. **Evaluate `images.shape`**. What does each number represent?<jupyter_code>images.shape<jupyter_output><empty_output><jupyter_text>The first value is batch size. The second value is the number of colour channels. The third is height and the fourth is width.  2. **Evaluate `labels`**. Explain those numbers, with the help of `dataloaders.train.vocab`.<jupyter_code>labels
dataloaders.train.vocab<jupyter_output><empty_output><jupyter_text>0 is the label for 'cat'1 is the label for 'dog' 3. **Show the first image in the batch.** (Use `show_image`.)<jupyter_code>show_image(images[0])<jupyter_output><empty_output><jupyter_text>4. **Show the average image.** Hint: you can compute this by taking the `.mean(axis=___)`; think about what the blank is.<jupyter_code>show_image(images.mean(axis=0))<jupyter_output><empty_output><jupyter_text>**Part B: Show the average of the middle 3 images.**You'll need to use *slicing* to compute this.<jupyter_code>show_image(images[3:6].mean(axis=0))<jupyter_output><empty_output><jupyter_text>5. **Show the grayscale version of all of the images.**- *Do this by making minimal changes to the previous exercise; do not import anything new.*- For simplicity, just use an equal weighting of the red, green, and blue channels.- You can use `show_images` to show all of the images.<jupyter_code># your code here
show_images(255 - images)<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>6. **Invert the color of the images** (e.g., black becomes white). Show the result.<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>The next exercise will require you to assign to slices. It wil also require you to "skip" dimensions in slicing. To prepare, study what this does:<jupyter_code>images.shape
images[:, 0].shape
images[:, :, 0, 0] = 0.0
print(images[0, 0, 0, 0], images[5, 1, 0, 0], images[0, 0, 5, 1])
# restore the original images
images = images_orig.clone()<jupyter_output><empty_output><jupyter_text>7. **Black out (set to 0) the top 75 pixels of each image, then grey out (set to 0.5) the right 50 pixels of each image.**<jupyter_code># your code here
# restore the original images for the next step
images = images_orig.clone()<jupyter_output><empty_output><jupyter_text>8. **Show only the red color channel** by zeroing out the green and blue channels.<jupyter_code># your code here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10754062442943217, 'fraction_numerical': 0.020996896111009678, 'mean_word_length': 5.482840236686391, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 72, 'https://': 1, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '98082', 'license': 'Apache-2.0', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1757, 'n_words': 607, 'path': 'hw/04/old/image-ops.ipynb', 'repo_name': 'gnawnahtan/cs-344', 'size': '5477'}