0
0
Prompt Engineering / GenAIml~10 mins

Image understanding and description in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load an image using PIL.

Prompt Engineering / GenAI
from PIL import Image
img = Image.[1]('example.jpg')
Drag options to blanks, or click blank then click option'
Aopen
Bimport
Cread
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'load' or 'read' instead of 'open' causes errors.
Trying to use 'import' as a method.
2fill in blank
medium

Complete the code to convert an image to a tensor for model input.

Prompt Engineering / GenAI
import torchvision.transforms as transforms
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
tensor_img = transform([1])
Drag options to blanks, or click blank then click option'
Ainput_data
Bimage_path
Cimage_tensor
Dimg
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a file path string instead of an image object.
Passing an already tensor variable.
3fill in blank
hard

Fix the error in the code to generate image captions using a pretrained model.

Prompt Engineering / GenAI
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
model = VisionEncoderDecoderModel.from_pretrained('nlpconnect/vit-gpt2-image-captioning')
processor = ViTImageProcessor.from_pretrained('nlpconnect/vit-gpt2-image-captioning')
tokenizer = AutoTokenizer.from_pretrained('nlpconnect/vit-gpt2-image-captioning')

pixel_values = processor(images=img, return_tensors='pt').[1]()
output_ids = model.generate(pixel_values)
caption = tokenizer.decode(output_ids[0], skip_special_tokens=True)
print(caption)
Drag options to blanks, or click blank then click option'
Asqueeze
Bunsqueeze
Cflatten
Dreshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using squeeze removes dimensions and causes errors.
Using flatten or reshape changes tensor shape incorrectly.
4fill in blank
hard

Fill both blanks to create a dictionary of image features and their lengths.

Prompt Engineering / GenAI
features = {img_id: [1] for img_id, img in images.items() if len([2]) > 0}
Drag options to blanks, or click blank then click option'
Aprocessor(images=img, return_tensors='pt').pixel_values
Bimg
Cimg_id
Dimages
Attempts:
3 left
💡 Hint
Common Mistakes
Using img_id or images instead of the image object.
Not using the processor to get pixel values.
5fill in blank
hard

Fill all three blanks to filter captions longer than 5 words and create a summary dictionary.

Prompt Engineering / GenAI
summary = {img_id: caption for img_id, caption in captions.items() if len(caption.[1]()) > [2] and caption.[3](' ') > 0}
Drag options to blanks, or click blank then click option'
Asplit
B5
Ccount
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using strip instead of split for word count.
Comparing length to wrong number.
Using count with wrong character.