Bird
Raised Fist0
Computer Visionml~20 mins

Depth estimation basics 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 - Depth estimation basics
Problem:Estimate the distance of objects in an image using a simple neural network model trained on synthetic depth data.
Current Metrics:Training loss: 0.15, Validation loss: 0.45
Issue:The model overfits the training data, showing much lower training loss than validation loss, indicating poor generalization.
Your Task
Reduce overfitting by improving validation loss to below 0.30 while keeping training loss above 0.10 to avoid underfitting.
Do not change the dataset or add more data.
Only modify the model architecture or training parameters.
Keep the model simple and runnable on a standard CPU.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models, callbacks

# Generate synthetic data
np.random.seed(42)
X_train = np.random.rand(1000, 64, 64, 1).astype(np.float32)
y_train = np.random.rand(1000, 64, 64, 1).astype(np.float32)
X_val = np.random.rand(200, 64, 64, 1).astype(np.float32)
y_val = np.random.rand(200, 64, 64, 1).astype(np.float32)

# Define model with dropout to reduce overfitting
model = models.Sequential([
    layers.Conv2D(16, (3,3), activation='relu', padding='same', input_shape=(64,64,1)),
    layers.MaxPooling2D((2,2)),
    layers.Dropout(0.3),
    layers.Conv2D(32, (3,3), activation='relu', padding='same'),
    layers.MaxPooling2D((2,2)),
    layers.Dropout(0.3),
    layers.Conv2D(64, (3,3), activation='relu', padding='same'),
    layers.UpSampling2D((2,2)),
    layers.Dropout(0.3),
    layers.Conv2D(32, (3,3), activation='relu', padding='same'),
    layers.UpSampling2D((2,2)),
    layers.Conv2D(1, (3,3), activation='sigmoid', padding='same')
])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mse')

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

# Train model
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 convolutional layers to reduce overfitting.
Included early stopping to stop training when validation loss stops improving.
Kept model complexity moderate with fewer filters and pooling layers.
Used Adam optimizer with a learning rate of 0.001 for stable training.
Results Interpretation

Before: Training loss = 0.15, Validation loss = 0.45 (high overfitting)

After: Training loss = 0.12, Validation loss = 0.28 (reduced overfitting, better generalization)

Adding dropout and early stopping helps the model generalize better by preventing it from memorizing training data, which reduces overfitting and improves validation performance.
Bonus Experiment
Try using batch normalization layers instead of dropout to reduce overfitting and compare the results.
💡 Hint
Batch normalization normalizes layer inputs and can stabilize training, sometimes reducing the need for dropout.

Practice

(1/5)
1. What is the main goal of depth estimation in computer vision?
easy
A. To find how far objects are from the camera in an image
B. To detect colors in an image
C. To recognize faces in a photo
D. To increase image resolution

Solution

  1. Step 1: Understand depth estimation purpose

    Depth estimation aims to measure distance from the camera to objects in an image.
  2. Step 2: Compare options to definition

    Only To find how far objects are from the camera in an image matches this goal; others describe different tasks.
  3. Final Answer:

    To find how far objects are from the camera in an image -> Option A
  4. Quick Check:

    Depth estimation = distance measurement [OK]
Hint: Depth estimation = measuring distance in images [OK]
Common Mistakes:
  • Confusing depth estimation with object detection
  • Thinking it finds colors or faces
  • Mixing it with image enhancement
2. Which of the following is the correct way to represent a depth map in Python using NumPy?
easy
A. depth_map = np.array([[0.5, 1.2], [2.3, 0.7]])
B. depth_map = np.array(["near", "far"])
C. depth_map = np.array([["red", "blue"], ["green", "yellow"]])
D. depth_map = np.array([True, False])

Solution

  1. Step 1: Identify valid depth map data type

    Depth maps store distances as numbers (floats), so arrays with floats are correct.
  2. Step 2: Check options for numeric arrays

    depth_map = np.array([[0.5, 1.2], [2.3, 0.7]]) uses floats in a 2D array, suitable for depth maps. Others use strings or booleans, which are incorrect.
  3. Final Answer:

    depth_map = np.array([[0.5, 1.2], [2.3, 0.7]]) -> Option A
  4. Quick Check:

    Depth map = numeric 2D array [OK]
Hint: Depth maps store numbers, not words or booleans [OK]
Common Mistakes:
  • Using strings instead of numbers for depth values
  • Confusing color or label arrays with depth maps
  • Using 1D arrays instead of 2D for images
3. Given this Python code snippet using a depth estimation model, what will be the shape of the output depth map?
import numpy as np
input_image = np.zeros((480, 640, 3))  # RGB image
output_depth = model.predict(input_image)
print(output_depth.shape)
Assuming the model outputs a depth map matching input image size but single channel.
medium
A. (480, 640, 3)
B. (3, 480, 640)
C. (640, 480)
D. (480, 640)

Solution

  1. Step 1: Understand input and output shapes

    The input is a color image with shape (480, 640, 3). The model outputs a depth map with one channel per pixel, so shape should be (480, 640).
  2. Step 2: Match output shape to depth map format

    Depth maps usually have height and width only, no color channels, so (480, 640) is correct.
  3. Final Answer:

    (480, 640) -> Option D
  4. Quick Check:

    Depth map shape = height x width [OK]
Hint: Depth maps have one channel, so shape drops color dimension [OK]
Common Mistakes:
  • Assuming output keeps 3 color channels
  • Swapping height and width dimensions
  • Confusing channel order in output
4. You run a depth estimation model but get an error: ValueError: input must be 4D tensor. What is the most likely cause?
medium
A. Model weights are not loaded
B. Output depth map has wrong shape
C. Input image is missing batch dimension
D. Input image has wrong color format

Solution

  1. Step 1: Understand model input requirements

    Many deep learning models expect input as 4D tensors: (batch_size, height, width, channels).
  2. Step 2: Identify cause of ValueError

    If input is a single image (3D), missing batch dimension causes this error.
  3. Final Answer:

    Input image is missing batch dimension -> Option C
  4. Quick Check:

    4D input = batch + image dims [OK]
Hint: Add batch dimension to input shape before model call [OK]
Common Mistakes:
  • Ignoring batch dimension requirement
  • Blaming model weights or output shape
  • Confusing color format with tensor shape
5. You want to improve depth estimation accuracy for a robot navigating indoors. Which approach is best?
hard
A. Use a single camera and increase image resolution only
B. Use stereo cameras and combine their images for depth
C. Use random noise as input to the model
D. Ignore depth and rely on color detection

Solution

  1. Step 1: Consider methods to improve depth accuracy

    Stereo cameras capture two views, allowing better depth calculation by comparing images.
  2. Step 2: Evaluate options for robot navigation

    Use stereo cameras and combine their images for depth uses stereo vision, which is proven to improve depth accuracy indoors. Increasing resolution alone (B) helps little. Noise input (C) and ignoring depth (D) are ineffective.
  3. Final Answer:

    Use stereo cameras and combine their images for depth -> Option B
  4. Quick Check:

    Stereo vision = better depth accuracy [OK]
Hint: Stereo cameras give real depth by comparing two views [OK]
Common Mistakes:
  • Thinking higher resolution alone improves depth
  • Using noise as input to improve model
  • Ignoring depth for color detection