tensorflow--30 seconds to fix object detection _tensorflow

Source: Internet
Author: User
Tags prepare jupyter notebook

Google has released a new TensorFlow object detection API that includes a pre-training model, a Jupyter notebook that publishes models, and useful scripts that can be used to back up models with their own datasets.
Using this API, you can quickly build applications for object detection in some images. Here we take a step-by-step look at how to use the pre-training model to detect objects in an image.

1. First we load some libraries that will be used

Import NumPy as NP  
import OS  
import six.moves.urllib as urllib  
import sys  
import tarfile  
Import TensorFlow as TF  
import zipfile from  

collections import defaultdict from  
io import Stringio  
from Matplotlib import Pyplot as plt from  
pil import Image  

2, next to the environment settings

%matplotlib inline  
sys.path.append ("..")  

3, Object detection loading

From utils import label_map_util from  

utils import visualization_utils as Vis_util  

4. Prepare the Model

Variables any model that uses the Export_inference_graph.py tool output can be loaded here, simply changing the path_to_ckpt point to a new. pb file. Here we use the "mobile network SSD" model.

Model_name = ' ssd_mobilenet_v1_coco_11_06_2017 '  
model_file = Model_name + '. tar.gz ' download_base  
= ' http:// download.tensorflow.org/models/object_detection/'  

path_to_ckpt = model_name + '/FROZEN_INFERENCE_GRAPH.PB '  

path_to_labels = os.path.join (' Data ', ' mscoco_label_map.pbtxt ')  

Num_classes = 90  

5. Download model

Opener = Urllib.request.URLopener ()  
opener.retrieve (download_base + model_file, model_file)  
tar_file = Tarfile.open (model_file) for  
FILE in Tar_file.getmembers ():  
    file_name = Os.path.basename (file.name)  
    if ' FROZEN_INFERENCE_GRAPH.PB ' in file_name:  
        tar_file.extract (file, OS.GETCWD ())  

6. Load (frozen) the TensorFlow model into memory

Detection_graph = tf. Graph () with  
Detection_graph.as_default ():  
    od_graph_def = tf. Graphdef ()  
    with Tf.gfile.GFile (PATH_TO_CKPT, ' RB ') as FID:  
        serialized_graph = Fid.read ()  
        od_graph_def. Parsefromstring (serialized_graph)  
        tf.import_graph_def (Od_graph_def, name= ")  

7. Loading Label Chart

The tag map maps the index to the class name, and when our convolution predicts 5 o'clock, we know it corresponds to the plane. Here we use built-in functions, but any dictionary that returns an integer map to the appropriate character label applies.

Label_map = Label_map_util.load_labelmap (path_to_labels)  
categories = Label_map_util.convert_label_map_to_ Categories (Label_map, max_num_classes=num_classes, use_display_name=true)  
Category_index = Label_map_ Util.create_category_index (categories)  

8, auxiliary Code

def load_image_into_numpy_array (image):  
  (im_width, im_height) = image.size return  
  np.array (Image.getdata ( ). Reshape (  
      (Im_height, Im_width, 3)). Astype (Np.uint8)  

9, testing

 path_to_test_images_dir = ' test_images ' test_image_paths = [Os.path.join (Path_to_test_images_dir, ' image{}.jpg ') . Format (i)) for I in range (1, 3)] image_size = (8) 
With Detection_graph.as_default (): with TF.  
      Session (Graph=detection_graph) as sess:for image_path in test_image_paths:image = Image.open (image_path) # This array will then be used to prepare frames and tags for the picture image_np = Load_image_into_numpy_array (image) # extended dimensions, expected for the model: [1, None, None, 3] image_np_expanded = Np.expand_dims (image_np, axis=0) image_tensor = detection_graph.get_tensor_by  
      _name (' image_tensor:0 ') # each box represents an object being detected.  
      boxes = Detection_graph.get_tensor_by_name (' detection_boxes:0 ') # each score represents the detection of the object's credibility. Scores = Detection_graph.get_tensor_by_name (' detection_scores:0 ') classes = Detection_graph.get_tensor_by_name (' De  
      tection_classes:0 ') num_detections = Detection_graph.get_tensor_by_name (' num_detections:0 ') # performs reconnaissance tasks.  
          (boxes, scores, classes, num_detections) = Sess.run ([boxes, scores, classes, Num_detections],  
 feed_dict={image_tensor:image_np_expanded}) # graphical.     Vis_util.visualize_boxes_and_labels_on_image_array (IMAGE_NP, Np.squeeze (boxes), Np.squeeze (classes). Astype (Np.int32), Np.squeeze (scores), Category_index, Use_normaliz   Ed_coordinates=true, line_thickness=8) plt.figure (figsize=image_size) plt.imshow (IMAGE_NP)

In the Load model section you can try different reconnaissance models to compare speed and accuracy, and put the images you want to detect into the test_image_paths to run.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.