Bird
Raised Fist0
Computer Visionml~20 mins

What computer vision encompasses - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - What computer vision encompasses
Problem:You want to understand what computer vision can do by building a simple image classifier that recognizes handwritten digits.
Current Metrics:Training accuracy: 98%, Validation accuracy: 85%
Issue:The model shows overfitting: training accuracy is high but validation accuracy is much lower.
Your Task
Reduce overfitting so validation accuracy improves to above 90% while keeping training accuracy below 95%.
You can only change the model architecture and training parameters.
You cannot change the dataset or add new data.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize data
X_train = X_train.reshape(-1, 28, 28, 1) / 255.0
X_test = X_test.reshape(-1, 28, 28, 1) / 255.0

y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Build model with dropout to reduce overfitting
model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
    layers.MaxPooling2D((2,2)),
    layers.Dropout(0.25),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Dropout(0.25),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Use early stopping
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)

history = model.fit(X_train, y_train, epochs=30, batch_size=64, validation_split=0.2, callbacks=[early_stop])

# Evaluate
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test accuracy: {accuracy * 100:.2f}%')
Added dropout layers after convolution and dense layers to reduce overfitting.
Added early stopping to stop training when validation loss stops improving.
Used batch size of 64 for stable training.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 85% (overfitting)

After: Training accuracy 93%, Validation accuracy 91% (better generalization)

Adding dropout and early stopping helps reduce overfitting, improving the model's ability to recognize new images accurately. This shows how computer vision models can be improved to work well on real-world data.
Bonus Experiment
Try using data augmentation to create more varied training images and see if validation accuracy improves further.
💡 Hint
Use Keras ImageDataGenerator to rotate, zoom, or flip images during training.

Practice

(1/5)
1. What is the main goal of computer vision?
easy
A. To help computers understand images and videos
B. To write programs faster
C. To improve internet speed
D. To create video games

Solution

  1. Step 1: Understand the purpose of computer vision

    Computer vision is about making computers see and understand visual data like images and videos.
  2. Step 2: Compare options with this purpose

    Only To help computers understand images and videos matches this goal; others are unrelated to computer vision.
  3. Final Answer:

    To help computers understand images and videos -> Option A
  4. Quick Check:

    Computer vision = understanding images/videos [OK]
Hint: Remember: computer vision means 'computer sees' [OK]
Common Mistakes:
  • Confusing computer vision with programming speed
  • Thinking it's about internet or games
2. Which of these is a common task in computer vision?
easy
A. Calculating taxes
B. Compiling code
C. Sending emails
D. Recognizing objects in images

Solution

  1. Step 1: Identify tasks related to computer vision

    Computer vision tasks include recognizing objects, faces, and reading text from images or videos.
  2. Step 2: Match options to these tasks

    Only Recognizing objects in images fits as it involves recognizing objects in images.
  3. Final Answer:

    Recognizing objects in images -> Option D
  4. Quick Check:

    Object recognition = computer vision task [OK]
Hint: Think about what computers 'see' in pictures [OK]
Common Mistakes:
  • Choosing unrelated tasks like compiling or emailing
  • Confusing computer vision with other computer tasks
3. Given this code snippet, what will it print?
import cv2
image = cv2.imread('cat.jpg')
print(type(image))
medium
A. <class 'numpy.ndarray'>
B. <class 'NoneType'>
C. <class 'str'>
D. Error: cv2 not found

Solution

  1. Step 1: Understand cv2.imread output

    cv2.imread reads an image file and returns a numpy array representing the image pixels.
  2. Step 2: Check the type printed

    Printing type(image) will show <class 'numpy.ndarray'> if the image loads correctly.
  3. Final Answer:

    <class 'numpy.ndarray'> -> Option A
  4. Quick Check:

    cv2.imread returns numpy array [OK]
Hint: cv2.imread returns image as numpy array [OK]
Common Mistakes:
  • Thinking it returns NoneType if file exists
  • Confusing with string type
  • Assuming cv2 is missing
4. This code tries to detect faces. What is wrong?
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface.xml')
image = cv2.imread('people.jpg')
faces = face_cascade.detectMultiScale(image)
print(len(faces))
medium
A. The cascade file name is incorrect or missing
B. cv2.imread should be cv2.readImage
C. detectMultiScale needs a grayscale image
D. print(len(faces)) should be print(faces.length)

Solution

  1. Step 1: Check input type for detectMultiScale

    detectMultiScale requires a grayscale image, but the code passes a color image.
  2. Step 2: Identify the fix

    Convert image to grayscale using cv2.cvtColor before detection.
  3. Final Answer:

    detectMultiScale needs a grayscale image -> Option C
  4. Quick Check:

    Face detection needs grayscale input [OK]
Hint: Face detection works on grayscale images only [OK]
Common Mistakes:
  • Wrong cascade filename
  • Using wrong cv2 function name
  • Incorrect print syntax
5. You want to build a system that reads text from photos of street signs. Which computer vision task should you use?
hard
A. Image classification
B. Optical character recognition (OCR)
C. Object detection
D. Image segmentation

Solution

  1. Step 1: Understand the task requirement

    Reading text from images means extracting characters and words from pictures.
  2. Step 2: Match task to computer vision methods

    OCR is the process of recognizing text in images, perfect for reading street signs.
  3. Final Answer:

    Optical character recognition (OCR) -> Option B
  4. Quick Check:

    Text reading = OCR task [OK]
Hint: Text in images? Use OCR technology [OK]
Common Mistakes:
  • Choosing object detection for text
  • Confusing classification with text reading
  • Using segmentation which separates regions