DAY 73-100 DAYS MLCODE: Object Detection using TensorFlow

My Tech World

DAY 73-100 DAYS MLCODE: Object Detection using TensorFlow

January 23, 2019 100-Days-Of-ML-Code blog 0

In the previous few blogs. we discussing the Object detection using ImageAI Library, in this blog, we’ll discuss the Object Detection using TensorFlow.

TensorFlow’s Object Detection API is a very powerful tool and can quickly allow to build and deploy powerful image recognition applications.

Let’s clone the Tensorflow models:

git clone https://github.com/tensorflow/models.git

The above command will download the repository which contains a number of different models implemented in TensorFlow. We are interested in using Object detection using Tensorflow.

Now follow the instruction mentioned for the Object detection. First install all the requirements.

# For CPU
pip install tensorflow
# For GPU
pip install tensorflow-gpu
pip install Cython
pip install contextlib2
pip install pillow
pip install lxml
pip install jupyter
pip install matplotlib

COCO API installation

Download the cocoapi and copy the pycocotools subfolder to the /models/research directory which we have downloaded at step 1.

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools /models/research/

Protobuf Compilation

Let’s now compile the Protobuf. The Tensorflow Object Detection API uses Protobufs to configure model and training parameters. Before we start using the framework c, the Protobuf libraries must be compiled. This can be done by running the following command from the directory /models/research/ :

# From tensorflow/models/research/
protoc object_detection/protos/*.proto –python_out=.

Add Libraries to PYTHONPATH

Add Libraries to PYTHONPATH”. Navigate a terminal console to the models/research/ folder and enter the below command

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

Below is the code which help to detect the object in single image. This entire code is copied from here.

def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
‘num_detections’, ‘detection_boxes’, ‘detection_scores’,
‘detection_classes’, ‘detection_masks’
]:
tensor_name = key + ‘:0’
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if ‘detection_masks’ in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict[‘detection_boxes’], [0])
detection_masks = tf.squeeze(tensor_dict[‘detection_masks’], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict[‘num_detections’][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict[‘detection_masks’] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name(‘image_tensor:0’)

# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})

# all outputs are float32 numpy arrays, so convert types as appropriate
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]
if ‘detection_masks’ in output_dict:
output_dict[‘detection_masks’] = output_dict[‘detection_masks’][0]
return output_dict

Now let’s try to detect the traffic light in the image. Our input for the model is

Input Image
Input Image

Now above code is using ssd_mobilenet_v1_coco_2017_11_17 pre trained model. We’ll load a (frozen) Tensorflow model into memory and use the above code to detect the object in above picture.

Below code will read the images and then convert the image into Numpy array. Once array is ready, expand dimensions as the model expects images to have shape: [1, None, None, 3]. Then call function run_inference_for_single_image and then we can display the image.

image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# 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,
instance_masks=output_dict.get(‘detection_masks’),
use_normalized_coordinates=True,
line_thickness=8)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)

Output image :

Object Detection
Object is detected successfully

In conclusion, TensorFlow object detection library is very powerful tool and we can easily use this to create the Object detection software. You can find the entire code in the TensorFlow object repository.