Imagine you have a photo with pixel values ranging from 0 to 255. Why is it helpful to normalize these values to a smaller range like 0 to 1 before analysis?
Think about how computers handle very large or very different numbers during learning.
Normalizing pixel values scales them to a consistent range, which helps the model process data more efficiently and learn patterns faster.
Given a color image with shape (300, 400, 3), what will be the shape after resizing it to (150, 200)?
import numpy as np from PIL import Image original_image = np.zeros((300, 400, 3), dtype=np.uint8) resized_image = Image.fromarray(original_image).resize((200, 150)) output_shape = np.array(resized_image).shape
Remember the order of width and height in the resize function.
The resize function takes size as (width, height), so resizing to (200, 150) means width=200 and height=150. The output shape keeps the 3 color channels.
You want to classify images of animals into categories like cats, dogs, and birds. Which model type is most suitable?
Think about which model type is designed to understand spatial patterns in images.
CNNs are designed to capture spatial features in images, making them ideal for image classification tasks.
After training an image classifier, you want to know how often it predicts the correct label. Which metric should you use?
Consider a metric that counts correct predictions over total predictions.
Accuracy measures the proportion of correct predictions, making it suitable for classification tasks.
Consider this code snippet that tries to convert an image to grayscale and normalize it:
import cv2
image = cv2.imread('photo.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
norm = gray / 255.0
print(norm.shape)What is the cause of the error if the image file is missing?
What happens if the image file path is wrong or file is missing?
If the image file is missing, cv2.imread returns None. Passing None to cv2.cvtColor causes an error because it expects an image array.