Created
August 15, 2020 02:03
-
-
Save Namburger/47d14bfffbe645683f6e6ae6ec5cc80c 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
# Do a Quick Evaluation on the inference graph model. | |
import numpy as np | |
import os | |
import sys | |
import tensorflow as tf | |
from collections import defaultdict | |
from matplotlib import pyplot as plt | |
from PIL import Image | |
from object_detection.utils import ops as utils_ops | |
from object_detection.utils import label_map_util | |
from object_detection.utils import visualization_utils as vis_util | |
%matplotlib inline | |
# Initialize tf.Graph() | |
detection_graph = tf.Graph() | |
with detection_graph.as_default(): | |
od_graph_def = tf.GraphDef() | |
with tf.gfile.GFile('/content/inference_graph/frozen_inference_graph.pb', 'rb') as fid: | |
serialized_graph = fid.read() | |
od_graph_def.ParseFromString(serialized_graph) | |
tf.import_graph_def(od_graph_def, name='') | |
# Loads labels | |
label_map = label_map_util.load_labelmap('/content/dataset/pet_label_map.pbtxt') | |
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=2, use_display_name=True) | |
category_index = label_map_util.create_category_index(categories) | |
# Run Inference and populates results in a dict. | |
def run_inference(graph, image): | |
with graph.as_default(): | |
with tf.Session() as sess: | |
ops = tf.get_default_graph().get_operations() | |
all_tensor_names = [output.name for op in ops for output in op.outputs] | |
tensor_dict = {} | |
tensor_keys = ['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes'] | |
for key in tensor_keys: | |
tensor_name = key + ':0' | |
if tensor_name in all_tensor_names: | |
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name) | |
# Actual inference. | |
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0') | |
output_dict = sess.run(tensor_dict, feed_dict={image_tensor: np.expand_dims(image, 0)}) | |
output_dict['num_detections'] = int(output_dict['num_detections'][0]) | |
output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.uint8) | |
output_dict['detection_boxes'] = output_dict['detection_boxes'][0] | |
output_dict['detection_scores'] = output_dict['detection_scores'][0] | |
return output_dict | |
test_image_path = [os.path.join('/content/test', 'image{}.jpg'.format(i)) for i in range(1, 6)] | |
for image_path in test_image_path: | |
print('Evaluating:', image_path) | |
image = Image.open(image_path) | |
img_width, img_height = image.size | |
image_np = np.array(image.getdata()).reshape((img_height, img_width, 3)).astype(np.uint8) | |
# Run inference. | |
output_dict = run_inference(detection_graph, image_np) | |
# Visualization of the results of a detection. | |
vis_util.visualize_boxes_and_labels_on_image_array( | |
image_np, | |
output_dict['detection_boxes'], | |
output_dict['detection_classes'], | |
output_dict['detection_scores'], | |
category_index, | |
use_normalized_coordinates=True, | |
line_thickness=8) | |
plt.figure(figsize=(12, 8)) | |
plt.imshow(image_np) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment