Bird
Raised Fist0
Computer Visionml~20 mins

Face recognition concept in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Face recognition concept
Problem:We want to build a model that can recognize faces from images. Currently, the model trains well on the training data but performs poorly on new images.
Current Metrics:Training accuracy: 98%, Validation accuracy: 65%, Training loss: 0.05, Validation loss: 1.2
Issue:The model is overfitting: it learns the training faces too well but cannot generalize to new faces.
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.
Do not add more training data.
Keep the input image size and dataset fixed.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Data augmentation to help generalization
train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    validation_split=0.2
)

val_datagen = ImageDataGenerator(
    rescale=1./255,
    validation_split=0.2
)

train_generator = train_datagen.flow_from_directory(
    'face_dataset/train',
    target_size=(128, 128),
    batch_size=32,
    class_mode='categorical',
    subset='training'
)

validation_generator = val_datagen.flow_from_directory(
    'face_dataset/train',
    target_size=(128, 128),
    batch_size=32,
    class_mode='categorical',
    subset='validation'
)

# Build a simpler CNN with dropout
model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(128,128,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(train_generator.num_classes, activation='softmax')
])

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

history = model.fit(
    train_generator,
    epochs=30,
    validation_data=validation_generator
)
Added dropout layers after convolution and dense layers to reduce overfitting.
Applied data augmentation to training images to increase data variety.
Reduced learning rate to 0.0005 for smoother training.
Simplified model architecture by limiting number of filters and layers.
Results Interpretation

Before: Training accuracy was 98% but validation accuracy was only 65%, showing overfitting.

After: Training accuracy dropped to 92% but validation accuracy improved to 87%, indicating better generalization.

Adding dropout and data augmentation helps the model not memorize training data and perform better on new images, reducing overfitting.
Bonus Experiment
Try using a pretrained model like MobileNetV2 as a feature extractor and fine-tune it for face recognition.
💡 Hint
Use transfer learning by freezing early layers of MobileNetV2 and training only the last layers on your dataset.

Practice

(1/5)
1. What is the main goal of face recognition in computer vision?
easy
A. To convert images into black and white
B. To detect objects like cars and trees in images
C. To generate new faces from random noise
D. To identify or verify a person using their facial features

Solution

  1. Step 1: Understand the purpose of face recognition

    Face recognition is designed to find out who a person is by looking at their face.
  2. Step 2: Compare with other options

    Options A, B, and C describe different tasks unrelated to identifying people by faces.
  3. Final Answer:

    To identify or verify a person using their facial features -> Option D
  4. Quick Check:

    Face recognition = Identify person by face [OK]
Hint: Face recognition means finding who the face belongs to [OK]
Common Mistakes:
  • Confusing face recognition with object detection
  • Thinking face recognition creates new images
  • Mixing up image color processing with recognition
2. Which of the following is the correct step to train a face recognition model?
easy
A. Collect face images with labels and train the model to learn features
B. Randomly assign labels to images without training
C. Use face images only without any labels
D. Train the model using only text data about faces

Solution

  1. Step 1: Identify training requirements

    Training a face recognition model needs images and their correct labels to learn who is who.
  2. Step 2: Eliminate incorrect options

    Options A, B, and D miss key parts: no labels (A), random labels (B), or no images (D), which won't train properly.
  3. Final Answer:

    Collect face images with labels and train the model to learn features -> Option A
  4. Quick Check:

    Training needs images + labels [OK]
Hint: Training needs labeled face images, not random or unlabeled data [OK]
Common Mistakes:
  • Ignoring the need for labels during training
  • Thinking unlabeled images are enough
  • Confusing text data with image data for training
3. Given this Python snippet for face recognition, what will be the output?
known_faces = {"Alice": [0.1, 0.2, 0.3], "Bob": [0.4, 0.5, 0.6]}
new_face = [0.1, 0.2, 0.31]

# Simple distance check
def recognize(face):
    for name, features in known_faces.items():
        dist = sum((f - nf) ** 2 for f, nf in zip(features, face)) ** 0.5
        if dist < 0.05:
            return name
    return "Unknown"

print(recognize(new_face))
medium
A. Bob
B. Alice
C. Unknown
D. Error

Solution

  1. Step 1: Calculate distance between new_face and known faces

    Distance to Alice: sqrt((0.1-0.1)^2 + (0.2-0.2)^2 + (0.3-0.31)^2) = sqrt(0 + 0 + 0.0001) = 0.01 < 0.05
  2. Step 2: Check distance condition and return name

    Since distance to Alice is less than 0.05, function returns "Alice" immediately.
  3. Final Answer:

    Alice -> Option B
  4. Quick Check:

    Distance < 0.05 means match = Alice [OK]
Hint: Check distance to known faces; closest under threshold wins [OK]
Common Mistakes:
  • Calculating distance incorrectly
  • Confusing which face is closer
  • Ignoring the threshold condition
4. The following code is meant to recognize a face but always returns "Unknown". What is the bug?
known_faces = {"Alice": [0.1, 0.2, 0.3]}
new_face = [0.1, 0.2, 0.3]

def recognize(face):
    for name, features in known_faces.items():
        dist = sum((f - nf) ** 2 for f, nf in zip(features, face)) ** 0.5
        if dist > 0.05:
            return name
    return "Unknown"

print(recognize(new_face))
medium
A. The new_face list has wrong length
B. The known_faces dictionary is empty
C. The distance condition should be less than 0.05, not greater
D. The function does not return anything

Solution

  1. Step 1: Analyze the distance condition in the if statement

    The code returns name if distance > 0.05, but it should return if distance < 0.05 to indicate a match.
  2. Step 2: Understand why it always returns "Unknown"

    Since distance is 0 (exact match), condition dist > 0.05 is false, so loop never returns name.
  3. Final Answer:

    The distance condition should be less than 0.05, not greater -> Option C
  4. Quick Check:

    Match if distance < threshold, not > [OK]
Hint: Match when distance is small, not large [OK]
Common Mistakes:
  • Using wrong comparison operator in distance check
  • Assuming empty dictionary causes issue
  • Ignoring return statements in loop
5. You want to improve a face recognition system to handle many people with similar features. Which approach helps most?
hard
A. Use a deeper neural network to extract more detailed face features
B. Reduce the number of training images per person
C. Ignore feature differences and use only face color
D. Train the model without labels to save time

Solution

  1. Step 1: Understand the challenge of similar faces

    When faces look alike, the model needs better features to tell them apart.
  2. Step 2: Evaluate options for improving feature extraction

    A deeper neural network can learn more complex and detailed features, improving recognition accuracy.
  3. Final Answer:

    Use a deeper neural network to extract more detailed face features -> Option A
  4. Quick Check:

    Better features = deeper network [OK]
Hint: More complex models capture finer face details [OK]
Common Mistakes:
  • Reducing training data lowers accuracy
  • Ignoring features and using color is ineffective
  • Training without labels prevents learning identities
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