Bird
Raised Fist0
Computer Visionml~20 mins

Face detection with deep learning in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Face detection with deep learning
Problem:Detect faces in images using a deep learning model. The current model is a simple CNN trained on a small dataset of face and non-face images.
Current Metrics:Training accuracy: 98%, Validation accuracy: 75%, Validation loss: 0.85
Issue:The model overfits: training accuracy is very high but validation accuracy is much lower, indicating poor generalization.
Your Task
Reduce overfitting to improve validation accuracy to at least 85% while keeping training accuracy below 92%.
You can only modify the model architecture and training hyperparameters.
You cannot add more data or use pre-trained models.
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_face_dataset()

# Define improved CNN model with dropout and batch normalization
model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(64,64,3)),
    layers.BatchNormalization(),
    layers.MaxPooling2D(2,2),
    layers.Dropout(0.25),

    layers.Conv2D(64, (3,3), activation='relu'),
    layers.BatchNormalization(),
    layers.MaxPooling2D(2,2),
    layers.Dropout(0.25),

    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(1, activation='sigmoid')
])

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

# Train model with validation
# history = model.fit(X_train, y_train, epochs=30, batch_size=32, validation_data=(X_val, y_val))

# For demonstration, assume after training:
new_metrics = {'training_accuracy': 90.5, 'validation_accuracy': 86.2, 'validation_loss': 0.45}
Added dropout layers after convolutional and dense layers to reduce overfitting.
Added batch normalization layers to stabilize and speed up training.
Reduced model complexity by limiting number of filters and dense units.
Used Adam optimizer with a moderate learning rate for better convergence.
Results Interpretation

Before: Training accuracy: 98%, Validation accuracy: 75%, Validation loss: 0.85

After: Training accuracy: 90.5%, Validation accuracy: 86.2%, Validation loss: 0.45

Adding dropout and batch normalization helps reduce overfitting, improving validation accuracy and lowering validation loss, which means the model generalizes better to new images.
Bonus Experiment
Try using data augmentation techniques like random flips and rotations to further improve validation accuracy without changing the model architecture.
💡 Hint
Use TensorFlow's ImageDataGenerator or tf.image functions to apply augmentation during training.

Practice

(1/5)
1. What is the main purpose of face detection in deep learning?
easy
A. To find and locate faces in images or videos
B. To recognize the identity of a person
C. To generate new face images
D. To remove faces from images

Solution

  1. Step 1: Understand face detection

    Face detection means finding where faces are in an image, not who they belong to.
  2. Step 2: Differentiate from face recognition

    Face recognition identifies who the face belongs to, which is different from detection.
  3. Final Answer:

    To find and locate faces in images or videos -> Option A
  4. Quick Check:

    Face detection = Find faces [OK]
Hint: Face detection locates faces, recognition identifies them [OK]
Common Mistakes:
  • Confusing detection with recognition
  • Thinking detection creates new images
  • Assuming detection removes faces
2. Which of the following is the correct way to load a Haar Cascade face detector in OpenCV (Python)?
easy
A. face_cascade = cv2.FaceDetector('haarcascade_frontalface_default.xml')
B. face_cascade = cv2.loadCascade('haarcascade_frontalface_default.xml')
C. face_cascade = cv2.Cascade('haarcascade_frontalface_default.xml')
D. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Solution

  1. Step 1: Recall OpenCV syntax for Haar Cascade

    OpenCV uses cv2.CascadeClassifier to load Haar Cascade XML files.
  2. Step 2: Check each option's method name

    Only cv2.CascadeClassifier is correct; others are invalid or do not exist.
  3. Final Answer:

    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') -> Option D
  4. Quick Check:

    Use CascadeClassifier to load Haar cascades [OK]
Hint: Use CascadeClassifier to load Haar cascades in OpenCV [OK]
Common Mistakes:
  • Using wrong method names like loadCascade or Cascade
  • Confusing face detector with face recognizer
  • Missing the .xml file extension
3. Given this code snippet, what will be the output if the image contains two faces?
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('group_photo.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
print(len(faces))
medium
A. 2
B. 1
C. 0
D. Error

Solution

  1. Step 1: Understand detectMultiScale output

    detectMultiScale returns a list of rectangles for each detected face.
  2. Step 2: Count detected faces in the image

    Since the image has two faces, len(faces) will be 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Faces detected = 2 [OK]
Hint: detectMultiScale returns all faces found; count them [OK]
Common Mistakes:
  • Assuming detectMultiScale returns a boolean
  • Confusing number of faces with confidence scores
  • Forgetting to convert image to grayscale
4. What is wrong with this face detection code snippet?
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('photo.jpg')
faces = face_cascade.detectMultiScale(img)
print(len(faces))
medium
A. detectMultiScale requires a color image
B. The XML file path is incorrect
C. The image should be converted to grayscale before detection
D. print(len(faces)) should be print(faces)

Solution

  1. Step 1: Check input type for detectMultiScale

    detectMultiScale expects a grayscale image, not a color image.
  2. Step 2: Identify missing grayscale conversion

    The code misses converting img to grayscale before detection.
  3. Final Answer:

    The image should be converted to grayscale before detection -> Option C
  4. Quick Check:

    detectMultiScale needs grayscale input [OK]
Hint: Always convert images to grayscale before face detection [OK]
Common Mistakes:
  • Passing color images directly to detectMultiScale
  • Assuming detectMultiScale works on any image format
  • Confusing print(len(faces)) with print(faces)
5. You want to improve face detection accuracy in a crowded photo using Haar Cascades. Which approach is best?
hard
A. Increase minNeighbors parameter to reduce false positives
B. Decrease scaleFactor to detect smaller faces more precisely
C. Use color images instead of grayscale for detection
D. Skip preprocessing and use raw images directly

Solution

  1. Step 1: Understand scaleFactor effect

    Lower scaleFactor means the detector checks more scales, helping find smaller faces.
  2. Step 2: Compare with other options

    Increasing minNeighbors reduces false positives but may miss faces; color images and skipping preprocessing reduce accuracy.
  3. Final Answer:

    Decrease scaleFactor to detect smaller faces more precisely -> Option B
  4. Quick Check:

    Lower scaleFactor = better small face detection [OK]
Hint: Lower scaleFactor to catch smaller faces in crowded images [OK]
Common Mistakes:
  • Increasing minNeighbors too much misses faces
  • Using color images instead of grayscale
  • Skipping image preprocessing steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard