Bird
Raised Fist0
Computer Visionml~20 mins

YOLO architecture concept in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - YOLO architecture concept
Problem:You want to detect objects in images quickly and accurately using the YOLO (You Only Look Once) model.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Validation loss: 1.2
Issue:The model is overfitting: training accuracy is high but validation accuracy is much lower, indicating poor generalization.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85% while keeping training accuracy below 92%.
Keep the YOLO architecture structure intact.
Only adjust hyperparameters and add regularization techniques.
Use the same dataset and training procedure.
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

# Define a simplified YOLO-like model
inputs = layers.Input(shape=(224, 224, 3))
x = layers.Conv2D(16, (3,3), activation='relu', padding='same')(inputs)
x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(32, (3,3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D(2)(x)
x = layers.Dropout(0.3)(x)  # Added dropout
x = layers.Conv2D(64, (3,3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D(2)(x)
x = layers.Dropout(0.3)(x)  # Added dropout
x = layers.Flatten()(x)
x = layers.Dense(128, activation='relu')(x)
x = layers.Dropout(0.4)(x)  # Added dropout
outputs = layers.Dense(10, activation='softmax')(x)  # Assuming 10 classes

model = models.Model(inputs, outputs)

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

# Data augmentation
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
)

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

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

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

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

history = model.fit(
    train_generator,
    epochs=50,
    validation_data=validation_generator,
    callbacks=[early_stop]
)

# After training, print final metrics
final_train_acc = history.history['accuracy'][-1] * 100
final_val_acc = history.history['val_accuracy'][-1] * 100
final_val_loss = history.history['val_loss'][-1]

print(f'Training accuracy: {final_train_acc:.2f}%')
print(f'Validation accuracy: {final_val_acc:.2f}%')
print(f'Validation loss: {final_val_loss:.4f}')
Added dropout layers after convolutional and dense layers to reduce overfitting.
Applied data augmentation to increase training data variety.
Lowered the 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 95%, Validation accuracy 70%, Validation loss 1.2

After: Training accuracy 90.5%, Validation accuracy 86.7%, Validation loss 0.65

Adding dropout and data augmentation helps the model generalize better by reducing overfitting. Lower learning rate and early stopping improve training stability and prevent over-training.
Bonus Experiment
Try replacing dropout with batch normalization layers and compare the validation accuracy.
💡 Hint
Batch normalization can stabilize and speed up training, sometimes improving generalization without dropout.

Practice

(1/5)
1. What is the main idea behind the YOLO architecture in object detection?
easy
A. It looks at the whole image once to find objects quickly.
B. It scans the image pixel by pixel for objects.
C. It uses multiple passes over the image to improve accuracy.
D. It only detects one object per image.

Solution

  1. Step 1: Understand YOLO's detection approach

    YOLO processes the entire image in one go instead of scanning parts multiple times.
  2. Step 2: Compare options with YOLO's method

    Only It looks at the whole image once to find objects quickly. correctly states that YOLO looks at the whole image once for fast detection.
  3. Final Answer:

    It looks at the whole image once to find objects quickly. -> Option A
  4. Quick Check:

    YOLO = single pass detection [OK]
Hint: YOLO means one look at the image for all objects [OK]
Common Mistakes:
  • Thinking YOLO scans pixel by pixel slowly
  • Believing YOLO needs multiple passes
  • Assuming YOLO detects only one object
2. Which of the following correctly describes how YOLO divides the input image?
easy
A. It splits the image into a grid of cells.
B. It divides the image into overlapping circles.
C. It processes the image as a single pixel vector.
D. It uses random patches of the image.

Solution

  1. Step 1: Recall YOLO's image division method

    YOLO splits the image into a grid of cells to predict bounding boxes and classes per cell.
  2. Step 2: Match options with this method

    Only It splits the image into a grid of cells. correctly states the grid division; others describe incorrect or unrelated methods.
  3. Final Answer:

    It splits the image into a grid of cells. -> Option A
  4. Quick Check:

    YOLO grid division = grid cells [OK]
Hint: YOLO uses grid cells, not random or circular parts [OK]
Common Mistakes:
  • Confusing grid cells with overlapping circles
  • Thinking YOLO processes pixels individually
  • Believing YOLO uses random image patches
3. Given a YOLO model that divides an image into a 7x7 grid and predicts 2 boxes per cell, how many total bounding boxes does it predict?
medium
A. 14
B. 98
C. 49
D. 7

Solution

  1. Step 1: Calculate total grid cells

    The grid is 7x7, so total cells = 7 * 7 = 49.
  2. Step 2: Multiply by boxes per cell

    Each cell predicts 2 boxes, so total boxes = 49 * 2 = 98.
  3. Final Answer:

    98 -> Option B
  4. Quick Check:

    7x7x2 = 98 boxes [OK]
Hint: Multiply grid cells by boxes per cell [OK]
Common Mistakes:
  • Multiplying grid size by 2 incorrectly
  • Using 7 or 14 instead of total boxes
  • Confusing grid cells with boxes
4. You wrote code to split an image into a 3x3 grid for YOLO, but your model predicts only 3 boxes total instead of 18. What is the likely error?
medium
A. You predicted 6 boxes per cell instead of 2.
B. You used a 1x3 grid instead of 3x3.
C. You predicted only 1 box per cell instead of 2.
D. You forgot to multiply grid cells by boxes per cell.

Solution

  1. Step 1: Calculate expected boxes

    3x3 grid = 9 cells; with 2 boxes per cell, expected boxes = 9 * 2 = 18.
  2. Step 2: Identify cause of only 3 boxes predicted

    Getting 3 boxes means only 1 box per cell was predicted instead of 2.
  3. Final Answer:

    You predicted only 1 box per cell instead of 2. -> Option C
  4. Quick Check:

    Grid cells x boxes per cell = total boxes [OK]
Hint: Check boxes predicted per cell [OK]
Common Mistakes:
  • Assuming wrong grid size
  • Confusing boxes per cell count
  • Ignoring multiplication step
5. In a real-time traffic camera system using YOLO, why is YOLO preferred over other detection methods?
hard
A. Because YOLO ignores small objects, reducing processing time.
B. Because YOLO requires multiple passes for higher accuracy, which is faster.
C. Because YOLO only detects one object type at a time, simplifying output.
D. Because YOLO processes the entire image quickly in one pass, enabling fast detection.

Solution

  1. Step 1: Understand YOLO's speed advantage

    YOLO looks at the whole image once, making it very fast for real-time detection.
  2. Step 2: Compare with other methods

    Other methods often scan multiple times or use slower region proposals, making YOLO better for live video.
  3. Final Answer:

    Because YOLO processes the entire image quickly in one pass, enabling fast detection. -> Option D
  4. Quick Check:

    YOLO = fast single pass detection [OK]
Hint: YOLO's one-pass speed suits real-time needs [OK]
Common Mistakes:
  • Thinking multiple passes are faster
  • Believing YOLO ignores small objects
  • Assuming YOLO detects only one object type
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