Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pre-trained object detection model from TensorFlow Hub.
TensorFlow
import tensorflow_hub as hub model = hub.load('[1]')
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a classification model URL instead of a detection model URL.
Using a BERT model URL which is for NLP, not detection.
✗ Incorrect
The URL 'https://tfhub.dev/tensorflow/ssd_mobilenet_v2/2' points to a pre-trained SSD MobileNet V2 detection model suitable for object detection tasks.
2fill in blank
mediumComplete the code to prepare an input image tensor for the detection model.
TensorFlow
import tensorflow as tf image = tf.io.read_file('image.jpg') image = tf.image.decode_jpeg(image, channels=3) input_tensor = tf.expand_dims(image, axis=[1])
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the batch dimension at the wrong axis causing shape errors.
Not adding a batch dimension at all.
✗ Incorrect
The detection model expects a batch dimension, so we add a new axis at position 0 to create a batch of one image.
3fill in blank
hardFix the error in the code to run the detection model and get predictions.
TensorFlow
detections = model([1])Attempts:
3 left
💡 Hint
Common Mistakes
Passing the raw image tensor without batch dimension.
Passing a numpy array instead of a tensor.
✗ Incorrect
The model expects a batch input tensor, so we must pass 'input_tensor' which has the batch dimension, not the raw image tensor.
4fill in blank
hardFill both blanks to extract detection boxes and scores from the model output.
TensorFlow
boxes = detections['[1]'][0].numpy() scores = detections['[2]'][0].numpy()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'detection_classes' or 'num_detections' instead of boxes or scores.
Not removing the batch dimension by indexing [0].
✗ Incorrect
The detection boxes are stored under 'detection_boxes' and the confidence scores under 'detection_scores' keys in the detections dictionary.
5fill in blank
hardFill all three blanks to filter detection results with scores above 0.5 and print the number of valid detections.
TensorFlow
valid_indices = [i for i, score in enumerate(scores) if score [1] [2]] valid_boxes = boxes[valid_indices] print('Number of detections:', [3](valid_boxes))
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' causing no detections to pass.
Using sum() instead of len() to count boxes.
Not filtering scores correctly.
✗ Incorrect
We filter scores greater than 0.5 using '>' and 0.5, then count valid boxes with len().
