Bird
Raised Fist0
Computer Visionml~20 mins

CV applications (autonomous driving, medical, retail) in Computer Vision - 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 - CV applications (autonomous driving, medical, retail)
Problem:You have a computer vision model trained to classify images from autonomous driving, medical imaging, and retail product photos. The model currently performs very well on training data with 98% accuracy but only achieves 75% accuracy on validation data.
Current Metrics:Training accuracy: 98%, Validation accuracy: 75%, Training loss: 0.05, Validation loss: 0.65
Issue:The model is overfitting. It learns the training data too well but does not generalize to new images.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85% while keeping training accuracy below 95%.
You can only change model architecture and training hyperparameters.
You cannot add more data or use external datasets.
You must keep the same dataset split.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras import layers, models

# Load dataset (placeholder, replace with actual data loading)
# X_train, y_train, X_val, y_val = load_data()

model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(64,64,3)),
    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(3, activation='softmax')
])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_val, y_val), callbacks=[early_stop])
Added dropout layers after convolution and dense layers to reduce overfitting.
Reduced learning rate from default to 0.0005 for smoother training.
Added early stopping to stop training when validation loss stops improving.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 75%, Training loss 0.05, Validation loss 0.65

After: Training accuracy 92%, Validation accuracy 87%, Training loss 0.20, Validation loss 0.35

Adding dropout and early stopping helps the model generalize better by preventing it from memorizing training data. Lower learning rate allows the model to learn more carefully, improving validation accuracy and reducing overfitting.
Bonus Experiment
Try using data augmentation techniques like random flips and rotations to improve validation accuracy further.
💡 Hint
Use TensorFlow's ImageDataGenerator or tf.image functions to create augmented images during training.

Practice

(1/5)
1. Which of the following is a common use of computer vision in autonomous driving?
easy
A. Detecting pedestrians and other vehicles on the road
B. Managing inventory in a warehouse
C. Analyzing blood samples in a lab
D. Recommending products to online shoppers

Solution

  1. Step 1: Understand autonomous driving needs

    Autonomous cars need to see and understand their surroundings to drive safely.
  2. Step 2: Match computer vision tasks to driving

    Detecting pedestrians and vehicles helps the car avoid accidents and navigate roads.
  3. Final Answer:

    Detecting pedestrians and other vehicles on the road -> Option A
  4. Quick Check:

    Autonomous driving = detecting road objects [OK]
Hint: Autonomous driving means seeing road and traffic [OK]
Common Mistakes:
  • Confusing retail or medical uses with driving
  • Thinking CV only works for product tracking
  • Mixing up lab analysis with driving tasks
2. Which Python library is commonly used for image processing in computer vision tasks?
easy
A. NumPy
B. Pandas
C. OpenCV
D. Matplotlib

Solution

  1. Step 1: Identify libraries for image processing

    OpenCV is designed specifically for computer vision and image tasks.
  2. Step 2: Compare other libraries

    NumPy handles arrays, Pandas handles tables, Matplotlib is for plotting, but OpenCV processes images.
  3. Final Answer:

    OpenCV -> Option C
  4. Quick Check:

    Image processing library = OpenCV [OK]
Hint: OpenCV is the go-to for CV image tasks [OK]
Common Mistakes:
  • Choosing NumPy for image processing only
  • Confusing Pandas with image libraries
  • Picking Matplotlib which is for plotting
3. What will the following Python code output when using a pre-trained model to classify an image in a retail store?
import cv2
model = cv2.dnn.readNetFromONNX('product_classifier.onnx')
image = cv2.imread('shelf.jpg')
blob = cv2.dnn.blobFromImage(image, 1/255.0, (224,224), swapRB=True)
model.setInput(blob)
predictions = model.forward()
print(predictions.argmax())
medium
A. The raw image pixels as a list
B. The size of the input image
C. An error because the model file is missing
D. The index of the most likely product class detected

Solution

  1. Step 1: Understand the code flow

    The code loads a model, prepares the image, runs prediction, and prints the class with highest score.
  2. Step 2: Interpret the output

    predictions.argmax() returns the index of the class with the highest confidence, meaning the predicted product.
  3. Final Answer:

    The index of the most likely product class detected -> Option D
  4. Quick Check:

    Model prediction = class index [OK]
Hint: argmax gives highest scoring class index [OK]
Common Mistakes:
  • Thinking it prints raw pixels
  • Assuming it prints image size
  • Expecting an error without checking file presence
4. A medical imaging model is not detecting tumors correctly. The code snippet is:
image = cv2.imread('scan.png')
blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(224,224))
model.setInput(blob)
pred = model.forward()
What is the likely issue causing poor detection?
medium
A. The image is not resized before blob creation
B. The scalefactor should normalize pixel values (e.g., 1/255.0)
C. The model input is missing color channel swap
D. The model file is not loaded

Solution

  1. Step 1: Check image preprocessing

    Pixel values usually need normalization (scaling to 0-1) for good model input.
  2. Step 2: Identify scalefactor problem

    Using scalefactor=1.0 keeps pixel values 0-255, which can confuse the model expecting 0-1.
  3. Final Answer:

    The scalefactor should normalize pixel values (e.g., 1/255.0) -> Option B
  4. Quick Check:

    Normalize pixels for model input [OK]
Hint: Normalize pixels with scalefactor 1/255.0 [OK]
Common Mistakes:
  • Ignoring pixel normalization
  • Assuming resizing alone fixes issues
  • Forgetting color channel order matters
5. In an autonomous driving system, how can computer vision help improve safety during night driving?
hard
A. By using infrared cameras to detect pedestrians in low light
B. By increasing the car's speed automatically
C. By disabling sensors to save power
D. By only relying on GPS data

Solution

  1. Step 1: Understand night driving challenges

    Low light makes it hard for normal cameras to see pedestrians and obstacles.
  2. Step 2: Identify CV solution for low light

    Infrared cameras capture heat signatures, helping detect people even in darkness.
  3. Final Answer:

    By using infrared cameras to detect pedestrians in low light -> Option A
  4. Quick Check:

    Infrared helps see in dark [OK]
Hint: Infrared cameras detect heat at night [OK]
Common Mistakes:
  • Thinking speed increase improves safety
  • Disabling sensors reduces safety
  • Relying only on GPS ignores vision needs