<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by mayank singhal on Medium]]></title>
        <description><![CDATA[Stories by mayank singhal on Medium]]></description>
        <link>https://medium.com/@techmayank2000?source=rss-652d95cb4243------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_UNh_03_UZYBvg-BhMd-2w.jpeg</url>
            <title>Stories by mayank singhal on Medium</title>
            <link>https://medium.com/@techmayank2000?source=rss-652d95cb4243------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 20:00:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@techmayank2000/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Object Detection using SSD MobilenetV2 using Tensorflow API : Can detect any single class from…]]></title>
            <link>https://medium.com/@techmayank2000/object-detection-using-ssd-mobilenetv2-using-tensorflow-api-can-detect-any-single-class-from-31a31bbd0691?source=rss-652d95cb4243------2</link>
            <guid isPermaLink="false">https://medium.com/p/31a31bbd0691</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[coco-dataset]]></category>
            <category><![CDATA[mobilenet]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[mayank singhal]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 11:55:31 GMT</pubDate>
            <atom:updated>2020-07-07T12:38:52.811Z</atom:updated>
            <content:encoded><![CDATA[<h3>Object Detection using SSD Mobilenet and Tensorflow Object Detection API : Can detect any single class from coco dataset.</h3><p><a href="https://arxiv.org/abs/1512.02325">SSD (Single Shot MultiBox Detector)</a> is a popular algorithm in object detection. It’s generally faster than Faster RCNN. In this post, I will give you a brief about what is object detection, what is tenforflow API, what is the idea behind neural networks and specifically how SSD architecture works. Then I’ll provide you the step by step approach on how to implement SSD MobilenetV2 trained over COCO dataset using Tensorflow API. In this tutorial you can detect any single class from the classes provided by COCO dataset. After this, I believe you can implement your own SSD with some patience. In this post, I will follow the original architecture from the paper.</p><h3>What Are Covered</h3><ol><li><strong>Object Detection</strong></li><li><strong>SSD(Single Shot Detector) Architecture</strong></li><li><strong>Tensorflow API for object detection</strong></li><li><strong>Implementation</strong></li></ol><h3>What is Object Detection?</h3><p><strong>Object detection</strong> is a computer technology related to computer vision and image processing that deals with detecting instances of semantic objects of a certain class (such as humans, buildings, or cars) in digital images and videos. Well-researched domains of object detection include face detection and Pedestrian Detection. Object detection has applications in many areas of computer vision, including image retrieval and video surveillance.</p><p>Object detection as the term suggest is the procedure to detect the objects in real world. For example, dog,car,humans, birds etc. In this process we can detect the presence of any still object with much ease. another great thing that can be done with it is that detection of multiple objects in a single frame can be done easily. For Example, in the image below the SSD model has detected mobile phone, laptop, coffee, glasses in a single shot. It detects different objects in a single shot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*rspH1V1cBp38J-QUYtb0cg.png" /><figcaption>Output from SSD Mobilenet Object Detection Model</figcaption></figure><h3>SSD MobileNet Architecture</h3><p>The SSD architecture is a single convolution network that learns to predict bounding box locations and classify these locations in one pass. Hence, SSD can be trained end-to-end. The SSD network consists of base architecture (MobileNet in this case) followed by several convolution layers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*rweWAcDJPhBfjO-H3uaBtQ.png" /><figcaption>SSD Mobilenet Layered Architecture</figcaption></figure><p>By using SSD, we only need to <strong>take one single shot to detect multiple objects within the image</strong>, while regional proposal network (RPN) based approaches such as R-CNN series that need two shots, one for generating region proposals, one for detecting the object of each proposal. Thus, SSD is much faster compared with two-shot RPN-based approaches.</p><p>For more details about SSD architecture and its working, please read it’s official paper <a href="https://arxiv.org/pdf/1512.02325.pdf">here</a></p><h3>Tensorflow Object Detection API</h3><p>Before moving to Tensorflow API, let’s understand <em>what is API? To explain it better let’s take an example:</em></p><p>Imagine you’re sitting at a table in a restaurant with a menu of choices to order from. The kitchen is the part of the “system” that will prepare your order. What is missing is the critical link to communicate your order to the kitchen and deliver your food back to your table. That’s where the waiter or API comes in. The waiter is the messenger — or API — that takes your request or order and tells the kitchen — the system — what to do. Then the waiter delivers the response back to you. In this case, it is the food. So, here your helpful waiter is analogous to you API.</p><p>Now, similarly, the TensorFlow object detection API is the framework for creating a deep learning network that solves object detection problems.</p><p>There are already pretrained models in their framework which they refer to as Model Zoo. This includes a collection of pretrained models trained on the COCO dataset, the KITTI dataset, and the Open Images Dataset.model</p><p>They are also useful for initializing your models when training on the novel dataset. The various architectures used in the pretrained model are described in this table</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*yj_3nuwIwxc7sAGtmd7TOw.png" /><figcaption>TensorFlow Object Detection Model Zoo</figcaption></figure><p>For more about TensorFlow object detection API, visit their github repo <a href="https://github.com/tensorflow/models/tree/master/research/object_detection">here</a>.</p><h3>Implementation</h3><p>start with one new colab notebook and follow the steps one by one.</p><p><strong><em>step 1</em></strong></p><p>Install tensorflow version 2 or higher</p><pre>!pip install -U --pre tensorflow==&quot;2.*&quot;</pre><p><strong>step 2</strong></p><p>make sure to install <em>pycocotools</em> for coco detection API.</p><pre>!pip install pycocotools</pre><p><strong>step 3</strong></p><p>get <em>tensorflow/models</em> by cloning the repository.</p><pre>import os<br>import pathlib<br><br>if &quot;models&quot; in pathlib.Path.cwd().parts:<br>  while &quot;models&quot; in pathlib.Path.cwd().parts:<br>    os.chdir(&#39;..&#39;)<br>elif not pathlib.Path(&#39;models&#39;).exists():<br>  !git clone --depth 1 <a href="https://github.com/tensorflow/models">https://github.com/tensorflow/models</a></pre><p>move (cd) to <em>research</em> directory of the repo</p><pre>cd models/research</pre><p><strong>step 4</strong></p><p>compile <em>protobufs</em></p><pre>!protoc object_detection/protos/*.proto --python_out=.</pre><p>install <em>object_detection</em> python package</p><pre>!pip install object_detection</pre><p><strong>step 5</strong></p><p>import required libraries</p><pre>import numpy as np<br>import os<br>import six.moves.urllib as urllib<br>import sys<br>import tarfile<br>import tensorflow as tf<br>import zipfile<br><br>from collections import defaultdict<br>from io import StringIO<br>from matplotlib import pyplot as plt<br>from PIL import Image<br>from IPython.display import display</pre><p>install <em>tf_slim</em> python package:</p><pre>!pip install tf_slim</pre><p>import object detection modules:</p><pre>from object_detection.utils import ops as utils_ops<br>from object_detection.utils import label_map_util<br>from object_detection.utils import visualization_utils as vis_util</pre><p><strong>step 6</strong></p><p>function to load your model</p><pre>def load_model(model_name):<br>  base_url = &#39;http://download.tensorflow.org/models/object_detection/&#39;<br>  model_file = model_name + &#39;.tar.gz&#39;<br>  model_dir = tf.keras.utils.get_file(<br>    fname=model_name, <br>    origin=base_url + model_file,<br>    untar=True)<br><br>  model_dir = pathlib.Path(model_dir)/&quot;saved_model&quot;<br><br>  model = tf.saved_model.load(str(model_dir))<br>  model = model.signatures[&#39;serving_default&#39;]<br><br>  return model</pre><p>this is the code to load your label map. Label maps map indices to category names/Class names. For example when our neural network predicts 1, it will correspond to “person” class or if it will predict, suppose 18, it will correspond to “dog” category.</p><pre>PATH_TO_LABELS = &#39;object_detection/data/mscoco_label_map.pbtxt&#39;<br>category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)</pre><p>this is the path to your test images. This will help you to check your model detections over the given class. You can change your test images by going to models/research/object_detection/test_images to check the accuracy of SSD mobilenet over the given class.</p><pre>PATH_TO_TEST_IMAGES_DIR = pathlib.Path(&#39;object_detection/test_images&#39;)<br>TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob(&quot;*.jpg&quot;)))<br>TEST_IMAGE_PATHS</pre><p><strong>step 7</strong></p><p>load your object detection SSD mobilenet v1 model for object detection</p><pre>model_name = &#39;ssd_mobilenet_v1_coco_2017_11_17&#39;<br>detection_model = load_model(model_name)</pre><p>Now, check the model’s input signature.</p><pre>print(detection_model.inputs)<br>detection_model.output_dtypes</pre><p>Now add this wrapper function which is calling the model and returns the output.</p><pre>def run_inference_for_single_image(model, image):<br>  image = np.asarray(image)<br>  input_tensor = tf.convert_to_tensor(image)<br>  input_tensor = input_tensor[tf.newaxis,...]<br>  output_dict = model(input_tensor)<br>  num_detections = int(output_dict.pop(&#39;num_detections&#39;))<br>  output_dict = {key:value[0, :num_detections].numpy() <br>                 for key,value in output_dict.items()}<br>  output_dict[&#39;num_detections&#39;] = num_detections</pre><pre>output_dict[&#39;detection_classes&#39;] = output_dict[&#39;detection_classes&#39;].astype(np.int64)<br>   <br>  if &#39;detection_masks&#39; in output_dict:</pre><pre>detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(<br>              output_dict[&#39;detection_masks&#39;], output_dict[&#39;detection_boxes&#39;],<br>               image.shape[0], image.shape[1])      <br>    detection_masks_reframed = tf.cast(detection_masks_reframed &gt; 0.5,<br>                                       tf.uint8)<br>    output_dict[&#39;detection_masks_reframed&#39;] = detection_masks_reframed.numpy()<br>    <br>  return output_dict</pre><p><strong>step 8</strong></p><p>Now, this is the main step where you can just pass on the class id corresponding to the category you want to detect in the class_id parameter of the function given below.(provided it should be present in the coco dataset). You can check the class id and their respective classes <a href="https://github.com/tensorflow/models/blob/master/research/object_detection/data/mscoco_label_map.pbtxt">here</a>.</p><pre>def show_inference(model, image_path,class_id):<br>  image_np = np.array(Image.open(image_path))<br>  output_dict = run_inference_for_single_image(model, image_np)<br>  boxes = []<br>  classes = []<br>  scores = []<br>  for i,x in enumerate(output_dict[&#39;detection_classes&#39;]):<br>    if x==class_id and output_dict[&#39;detection_scores&#39;][i] &gt; 0.5:<br>      classes.append(x)<br>      boxes.append(output_dict[&#39;detection_boxes&#39;][i])<br>      scores.append(output_dict[&#39;detection_scores&#39;][i])<br>  boxes = np.array(boxes)<br>  classes = np.array(classes)<br>  scores = np.array(scores)<br>  vis_util.visualize_boxes_and_labels_on_image_array(<br>      image_np,<br>      boxes,<br>      classes,<br>      scores,<br>      category_index,<br>      instance_masks=output_dict.get(&#39;detection_masks_reframed&#39;, None),<br>      use_normalized_coordinates=True,<br>      line_thickness=2)<br>  <br>  display(Image.fromarray(image_np))</pre><p><strong>step 9</strong></p><p>this is the final step to see your output on the test images.</p><pre>for image_path in TEST_IMAGE_PATHS:<br>  show_inference(detection_model, image_path, class_id)</pre><h3>Results</h3><p>Let’s suppose we have two images in the test_images directory and I’ve passed class_id to be 1 which corresponds to “person” so in both the images only persons are detected.</p><pre>for image_path in TEST_IMAGE_PATHS:<br>  show_inference(detection_model, image_path, 1)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*ukslqyimOdSanXEPCvDK6w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9xpt7bIznOa4NGPAb_C5Wg.png" /><figcaption>In this case only persons are detected in the second image and no other object is detected.</figcaption></figure><p>Now, let’s change the class_id to be 18 which corresponds to “dog” category.</p><pre>for image_path in TEST_IMAGE_PATHS:<br>  show_inference(detection_model, image_path, 18)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*7r1QJEq6KchNziiLvICe6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZK0xRXTDpG2rWcf1fQwgGQ.png" /><figcaption>Here, only dogs are detected and no other object.</figcaption></figure><p>In the next post I’ll discuss about how to train and implement your custom object detection model using Tensorflow object detection API.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31a31bbd0691" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>