Complete the code to load a pre-trained TensorFlow object detection model.
model = tf.saved_model.load('[1]')
model.compile() before loading the model.tf.keras.Model() which creates a new model instead of loading.The tf.saved_model.load() function loads a saved TensorFlow model from the given directory path.
Complete the code to prepare an image tensor for object detection input.
input_tensor = tf.convert_to_tensor(image)[1].numpy() converts tensor to numpy array, which is not needed here.Adding [tf.newaxis, ...] adds a batch dimension, which is required for the model input.
Fix the error in the code to get detection results from the model.
detections = model(input_tensor)[1].numpy() directly on the model output dictionary causes errors..items() or .pop() is not correct here.The model returns a dictionary of tensors inside a batch dimension, so we select the first element with [0].
Fill both blanks to filter detection boxes with scores above 0.5.
high_score_idxs = tf.where(detections['detection_scores'] [1] [2])
< instead of greater than >.We use tf.where() to find indices where detection scores are greater than 0.5.
Fill all three blanks to create a dictionary of filtered detection boxes and classes.
filtered_detections = {
'boxes': tf.gather(detections['detection_boxes'], [1]),
'scores': tf.gather(detections['detection_scores'], [2]),
'classes': tf.gather(detections['detection_classes'], [3])
}tf.gather().We use tf.gather() with the indices of high scoring detections to filter boxes, scores, and classes. Since tf.where() returns a 2D tensor, we select the first column for classes.
