Created
April 6, 2020 07:07
-
-
Save Alakhator/d6acdd58fb1d084c7975b6936892a269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_inference_for_single_image(model, image): | |
image = np.asarray(image) | |
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`. | |
input_tensor = tf.convert_to_tensor(image) | |
# The model expects a batch of images, so add an axis with `tf.newaxis`. | |
input_tensor = input_tensor[tf.newaxis,...] | |
# Run inference | |
output_dict = model(input_tensor) | |
# All outputs are batches tensors. | |
# Convert to numpy arrays, and take index [0] to remove the batch dimension. | |
# We're only interested in the first num_detections. | |
num_detections = int(output_dict.pop('num_detections')) | |
output_dict = {key:value[0, :num_detections].numpy() | |
for key,value in output_dict.items()} | |
output_dict['num_detections'] = num_detections | |
# detection_classes should be ints. | |
output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64) | |
# Handle models with masks: | |
if 'detection_masks' in output_dict: | |
# Reframe the the bbox mask to the image size. | |
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( | |
output_dict['detection_masks'], output_dict['detection_boxes'], | |
image.shape[0], image.shape[1]) | |
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5, | |
tf.uint8) | |
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy() | |
return output_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment