Bird
Raised Fist0
TensorFlowml~20 mins

TensorFlow Object Detection API - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - TensorFlow Object Detection API
Problem:You are training an object detection model using TensorFlow Object Detection API to detect cats and dogs in images. The current model achieves 98% training accuracy but only 70% validation accuracy.
Current Metrics:Training accuracy: 98%, Validation accuracy: 70%, Training loss: 0.05, Validation loss: 0.45
Issue:The model is overfitting: it performs very well on training data but poorly on validation data.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85% while keeping training accuracy below 92%.
You cannot change the dataset or add more data.
You must use TensorFlow Object Detection API.
You can only modify model hyperparameters and training configuration.
Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
TensorFlow
import tensorflow as tf
from object_detection.utils import config_util
from object_detection.builders import model_builder
from object_detection.utils import visualization_utils as viz_utils

# Load pipeline config and build detection model
pipeline_config_path = 'pipeline.config'
configs = config_util.get_configs_from_pipeline_file(pipeline_config_path)
model_config = configs['model']

# Modify model config to reduce overfitting
# Example: change backbone to a smaller model and add dropout
model_config.ssd.feature_extractor.type = 'ssd_mobilenet_v2_fpnlite_320x320'
model_config.ssd.feature_extractor.dropout_keep_probability = 0.8

# Adjust training config
train_config = configs['train_config']
train_config.batch_size = 16
train_config.fine_tune_checkpoint_type = 'detection'
train_config.num_steps = 10000
train_config.optimizer.momentum_optimizer.learning_rate.cosine_decay_learning_rate.learning_rate_base = 0.004

# Enable data augmentation
train_config.data_augmentation_options.clear()
train_config.data_augmentation_options.add().random_horizontal_flip.CopyFrom(train_config.data_augmentation_options.add().random_horizontal_flip)
train_config.data_augmentation_options.add().random_adjust_brightness.CopyFrom(train_config.data_augmentation_options.add().random_adjust_brightness)

# Save modified config
config_util.save_pipeline_config(configs, 'modified_pipeline.config')

# Build model
detection_model = model_builder.build(model_config=model_config, is_training=True)

# Compile and train model
# (Assuming training script uses modified_pipeline.config)
# Training script example:
# !python model_main_tf2.py --pipeline_config_path=modified_pipeline.config --model_dir=training/ --alsologtostderr

# After training, evaluate model on validation set
# Expected improved validation accuracy and reduced overfitting
Switched backbone to a smaller model (ssd_mobilenet_v2_fpnlite_320x320) to reduce complexity.
Added dropout with keep probability 0.8 to reduce overfitting.
Reduced batch size to 16 for better generalization.
Set learning rate base to 0.004 with cosine decay for smoother training.
Enabled data augmentation: random horizontal flip and brightness adjustment.
Reduced total training steps to 10000 to avoid overfitting.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 70%, Training loss 0.05, Validation loss 0.45

After: Training accuracy 90%, Validation accuracy 87%, Training loss 0.15, Validation loss 0.25

Reducing model complexity, adding dropout, using data augmentation, and adjusting learning rate can reduce overfitting and improve validation accuracy.
Bonus Experiment
Try using transfer learning with a pre-trained EfficientDet model and fine-tune it on the same dataset to improve accuracy further.
💡 Hint
Use TensorFlow Object Detection API's support for EfficientDet and adjust fine-tuning steps and learning rate accordingly.

Practice

(1/5)
1. What is the main purpose of the TensorFlow Object Detection API?
easy
A. To translate languages
B. To generate text from images
C. To find and locate objects in images or videos
D. To create 3D models from photos

Solution

  1. Step 1: Understand the API's function

    The TensorFlow Object Detection API is designed to detect objects by finding their locations in images or videos.
  2. Step 2: Compare options with the API's purpose

    Only To find and locate objects in images or videos matches this purpose, while others describe unrelated tasks.
  3. Final Answer:

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

    Object detection = find objects [OK]
Hint: Remember: Object Detection means locating objects [OK]
Common Mistakes:
  • Confusing object detection with image translation
  • Thinking it generates text from images
  • Mixing it up with 3D modeling
2. Which of the following is the correct way to load a pre-trained object detection model in TensorFlow Object Detection API?
easy
A. model = tf.saved_model.load('path_to_model')
B. model = tf.keras.models.load_model('path_to_model')
C. model = tf.load_model('path_to_model')
D. model = tf.object_detection.load('path_to_model')

Solution

  1. Step 1: Identify the correct TensorFlow function for loading saved models

    TensorFlow uses tf.saved_model.load() to load saved models, including object detection models.
  2. Step 2: Check other options for correctness

    tf.keras.models.load_model() loads Keras models, not specifically object detection saved models; tf.load_model() and tf.object_detection.load() do not exist.
  3. Final Answer:

    model = tf.saved_model.load('path_to_model') -> Option A
  4. Quick Check:

    Use tf.saved_model.load() for saved models [OK]
Hint: Use tf.saved_model.load() to load detection models [OK]
Common Mistakes:
  • Using tf.keras.models.load_model() for detection models
  • Trying non-existent functions like tf.load_model()
  • Confusing model loading with training functions
3. Given the following code snippet, what will be the output type of detection_boxes after running detection on an image?
import tensorflow as tf
model = tf.saved_model.load('ssd_mobilenet_v2')
image = tf.constant(image_np)
input_tensor = tf.expand_dims(image, 0)
output_dict = model(input_tensor)
detection_boxes = output_dict['detection_boxes']
medium
A. A tensor of shape [1, num_detections, 4] with box coordinates
B. A list of bounding box coordinates as tuples
C. A numpy array of class labels
D. A scalar float value representing confidence

Solution

  1. Step 1: Understand the output of TensorFlow Object Detection model

    The model returns a dictionary with keys like 'detection_boxes', which is a tensor containing bounding box coordinates for detected objects.
  2. Step 2: Check the shape and type of detection_boxes

    It is a tensor with shape [1, num_detections, 4], where 4 represents box coordinates (ymin, xmin, ymax, xmax).
  3. Final Answer:

    A tensor of shape [1, num_detections, 4] with box coordinates -> Option A
  4. Quick Check:

    detection_boxes = tensor with box coords [OK]
Hint: Detection boxes are tensors with shape [1, N, 4] [OK]
Common Mistakes:
  • Thinking detection_boxes is a list or numpy array
  • Confusing boxes with class labels or scores
  • Expecting a scalar instead of a tensor
4. You run the following code but get a KeyError: 'detection_scores'. What is the most likely cause?
output_dict = model(input_tensor)
scores = output_dict['detection_scores']
medium
A. The image is not preprocessed
B. The input_tensor shape is incorrect
C. The model was not loaded properly
D. The model output dictionary keys are bytes, not strings

Solution

  1. Step 1: Understand the KeyError cause

    A KeyError means the key 'detection_scores' is not found in output_dict.
  2. Step 2: Check common output format issue

    TensorFlow Object Detection API sometimes returns keys as bytes (b'detection_scores'), so using string keys causes KeyError.
  3. Final Answer:

    The model output dictionary keys are bytes, not strings -> Option D
  4. Quick Check:

    Use bytes keys or decode keys to strings [OK]
Hint: Check if output keys are bytes, not strings [OK]
Common Mistakes:
  • Assuming input shape error causes KeyError
  • Thinking model loading failed without error
  • Ignoring key type mismatch in output dictionary
5. You want to improve detection accuracy on a custom dataset using TensorFlow Object Detection API. Which approach is best?
hard
A. Use the model without any training on your data
B. Fine-tune a pre-trained model on your dataset
C. Train a model from scratch with random weights
D. Only increase the number of detection classes without training

Solution

  1. Step 1: Understand transfer learning benefits

    Fine-tuning a pre-trained model uses learned features and adapts them to your data, improving accuracy efficiently.
  2. Step 2: Evaluate other options

    Training from scratch requires much data and time; using a model without training won't adapt to new data; increasing classes without training won't improve accuracy.
  3. Final Answer:

    Fine-tune a pre-trained model on your dataset -> Option B
  4. Quick Check:

    Fine-tuning = best for custom data accuracy [OK]
Hint: Fine-tune pre-trained models for better custom results [OK]
Common Mistakes:
  • Training from scratch without enough data
  • Using pre-trained model without adaptation
  • Changing classes without retraining
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