Created
October 5, 2020 15:19
-
-
Save seatedro/7ec7b5a2e46705470b8950fa1490fa5a to your computer and use it in GitHub Desktop.
InferenceOnTFObjectDetection
This file contains hidden or 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
import numpy as np | |
from PIL import Image | |
from google.colab.patches import cv2_imshow | |
def load_image_into_numpy_array(path): | |
"""Load an image from file into a numpy array. | |
Puts image into numpy array to feed into tensorflow graph. | |
Note that by convention we put it into a numpy array with shape | |
(height, width, channels), where channels=3 for RGB. | |
Args: | |
path: the file path to the image | |
Returns: | |
uint8 numpy array with shape (img_height, img_width, 3) | |
""" | |
return np.array(Image.open(path)) | |
image_path = "PATH TO YOUR INFERENCE IMAGE" | |
print('Running inference for {}... '.format(image_path), end='') | |
image_np = load_image_into_numpy_array(image_path) | |
# Things to try: | |
# Flip horizontally | |
# image_np = np.fliplr(image_np).copy() | |
# Convert image to grayscale, (You could uncomment this to try and see how the model reacts to a grayscale image) | |
# image_np = np.tile( | |
# np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8) | |
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`. | |
input_tensor = tf.convert_to_tensor(image_np) | |
# The model expects a batch of images, so add an axis with `tf.newaxis`. | |
input_tensor = input_tensor[tf.newaxis, ...] | |
detections = detect_fn(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(detections.pop('num_detections')) | |
detections = {key: value[0, :num_detections].numpy() | |
for key, value in detections.items()} | |
detections['num_detections'] = num_detections | |
# detection_classes should be ints. | |
detections['detection_classes'] = detections['detection_classes'].astype(np.int64) | |
image_np_with_detections = image_np.copy() | |
viz_utils.visualize_boxes_and_labels_on_image_array( | |
image_np_with_detections, | |
detections['detection_boxes'], | |
detections['detection_classes'], | |
detections['detection_scores'], | |
category_index, | |
use_normalized_coordinates=True, | |
max_boxes_to_draw=200, | |
min_score_thresh=.4, # Adjust this value to set the minimum probability boxes to be classified as True | |
agnostic_mode=False) | |
cv2_imshow(image_np_with_detections) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, I have a question about the saving model and loading model. the above code store as a checkpoint of the model can I load this model and save it in a different format like .h5 or .pb. it is possible or not I try a lot of code but nothing works correctly.