Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load an image and convert it to a tensor for image-to-image transformation.
Prompt Engineering / GenAI
from PIL import Image import torchvision.transforms as transforms image = Image.open('input.jpg') transform = transforms.Compose([ transforms.ToTensor(), ]) tensor_image = [1](image)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Image.open again instead of the transform.
Trying to call transforms directly without Compose.
✗ Incorrect
The transform object applies the ToTensor() operation to convert the PIL image into a tensor.
2fill in blank
mediumComplete the code to define a simple convolutional layer for image-to-image transformation.
Prompt Engineering / GenAI
import torch.nn as nn conv_layer = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=[1], padding=1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using kernel size 1 which is too small to capture features.
Using kernel size 7 which is large and uncommon here.
✗ Incorrect
A kernel size of 3 is common for convolutional layers to capture local features effectively.
3fill in blank
hardFix the error in the code to apply a ReLU activation after the convolutional layer.
Prompt Engineering / GenAI
import torch.nn.functional as F output = F.[1](conv_layer(input_tensor))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sigmoid which is less common for convolutional activations.
Using softmax which is for classification outputs.
✗ Incorrect
ReLU is the correct activation function to apply after convolution for non-linearity in image tasks.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps image filenames to their transformed tensors if the image size is greater than 256.
Prompt Engineering / GenAI
image_tensors = {filename: [1] for filename, image in images.items() if image.size[0] [2] 256} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform without calling it as a function.
Using '<' instead of '>' in the condition.
✗ Incorrect
We apply the transform to the image and filter images with width greater than 256 pixels.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps image names to their grayscale tensors if the height is less than 128.
Prompt Engineering / GenAI
grayscale_images = {name: [1](image.convert([2])) for name, image in dataset.items() if image.size[1] [3] 128} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'RGB' instead of 'L' for grayscale conversion.
Using '>' instead of '<' in the size condition.
✗ Incorrect
We convert images to grayscale with 'L', apply the transform, and filter images with height less than 128.