Tensorflow Lite Converter Example!!

Let us deploy Deep learning TensorFlow model on edge devices using TF Lite. 

There are three different ways we can use TensorFlow lite converter

  1. Convert TF SaveModel to TF Lite 
  2. Convert Keras PreBuilt Model to TF Lite
  3. Concrete Function to TF Lite
  1. Convert TF SaveModel to TF Lite:- 

Let us create a simple model using TensorFlow and save that model using the TF SaveModel. To develop this model we will use TensorFlow API. In this example, we will show how to convert SaveModel into TF Lite FlatBuffer.

# we will train 
import tensorflow as tf# Construct a basic TF model.root = tf.train.Checkpoint()root.v1 = tf.Variable(3.)root.v2 = tf.Variable(2.)root.f = tf.function(lambda x: root.v1 * root.v2 * x)
# Save the model into temp directoryexport_dir = "/tmp/test_saved_model"input_data = tf.constant(1., shape=[1, 1])to_save = root.f.get_concrete_function(input_data)tf.saved_model.save(root, export_dir, to_save)
# Convert the model into TF Lite.converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)tflite_model = converter.convert()
#save model 
tflite_model_files = pathlib.Path(‘/tmp/save_model_tflite.tflite’)
tflite_model_file.write_bytes(tflite_model)

2. Convert Keras PreBuilt Model to TF Lite:-

In this section, we have explored how to convert the prebuilt Keras model into the TF lite model. We will run inference on a pre-trained tf.keras MobileNet model to TensorFlow Lite.

import numpy as npimport tensorflow as tf
# Load the MobileNet keras model.# we will create tf.keras model by loading pretrained model on #imagenet dataset
model = tf.keras.applications.MobileNetV2(    weights="imagenet", input_shape=(224, 224, 3))
# here we pretrained model no need use SaveModel 
# here we will pass model directly to TFLiteConverter
converter = tf.lite.TFLiteConverter.from_keras_model(model)tflite_model = converter.convert()

#if you want to save the TF Lite model use below steps or else skip
tflite_model_files = pathlib.Path(‘/tmp/pretrainedmodel.tflite’)
tflite_model_file.write_bytes(tflite_model)
# Load TFLite model using interpreter and allocate tensors.interpreter = tf.lite.Interpreter(model_content=tflite_model)interpreter.allocate_tensors()

3. Concrete Function to TF Lite:- 

In order to convert TensorFlow 2.0 models to TensorFlow Lite, the model needs to be exported as a concrete function. If you have developed your model using TF 2.0 then this is for you. We will convert concrete function into the TF Lite model. In this section also we will use the Keras MobileNet model.

import tensorflow as tf
# load mobilenet model of keras 
model = tf.keras.applications.MobileNetV2(weights="imagenet", input_shape=(224, 224, 3))

We will tf.function to create a callable tensorflow graph of our model.

#get callable graph from model. 
run_model = tf.function(lambda x: model(x))
# to get the concrete function from callable graph 
concrete_funct = run_model.get_concrete_function(tf.Tensorpec(model.inputs[0].shape, model.inputs[0].dtype))

#convert concrete function into TF Lite model using TFLiteConverter
converter =  tf.lite.TFLiteConverter.from_concrete_functions([concrete_funct])
tflite_model = converte.convert()
#save model 
tflite_model_files = pathlib.Path(‘/tmp/concretefunc_model.tflite’)
tflite_model_file.write_bytes(tflite_model)

CLI TF Lite Converter:-

Apart from this python API we can also use Command Line Interface to convert model. TF lite converter to convert SaveModel to the TFLite model.

The TensorFlow Lite Converter has a command-line tool tflite_convert which supports basic models.

#! /usr/bin/env/  bash
tflite_convert = --saved_model_dir=/tmp/mobilenet_saved_model \
--output_file=/tmp/mobilenet.tflite

 --output_file. Type: string. Specifies the full path of the output file.

--saved_model_dir. Type: string. Specifies the full path to the directory containing the SavedModel generated in 1.X or 2.X.

 --keras_model_file. Type: string. Specifies the full path of the HDF5 file containing the tf.keras model generated in 1.X or 2.X.

#! /usr/bin/env/  bash
tflite_convert = --keras_model_file=model.h5 \
--output_file=/tmp/mobilenet_keras.tflite

The converter supports SavedModel directories, tf.keras models, and concrete functions.

For now, we will end off with these options only. Next article we will explore converting RNN model and Quantized Models.

How to ask for a lift to self-driving car????

How to ask for a lift to self-driving car????

I just want to clarify one thing before deep dive inside this question, I am not an expert I am a learner.

This is the question which comes to my mind when I am thinking the self-driving car in a hometown environment. This is just a visionary thought. Implementing self-driving car in India and implementing the same thing in other countries has there own challenges.

I always asked for a lift on road in my college days. I am traveling daily 25KM for college from my hometown.

this is the software engineer who will ask for lift !!hahha

Why this question?

I am exploring little about the self-driving car. Generally, in computer science, we try to mimic human intelligence. In a self-driving car we can say we are cloning drivers behavior, learn some more common patterns and many more things. For this post, I am interested in drivers behavioral cloning, computer vision and machine to machine communication or connected cars.

How car(machine) will understand someone really needs a lift and the person who is asking for lift is a genuine one. For this question, there might be multiple answers. In some cases, some incident or road accident happen How self-driving car will approach to help them? as human do.

So I have two questions

  1. Human asks for a lift.
  2. Car asks for help.(Car to Car help)

Human ask for the lift :-

You are traveling from your workplace to your home when suddenly the car stalls and stops.

You recognize it as an engine problem, but there is no garage for miles ahead. You need to ask for a lift to a self-driving car.

Every human being has a unique approach. Some people are biased toward girls. Isn’t it? What about machines?

Solution:-

With the help Computer vision and NLP, we can achieve a simple solution to detect pedestrian lift posture.

  1. Proper posture.
  2. Give Proper direction which way.
  3. NLP(Natural Language Processing) to find & set the destination path.

There is much more technology required like sensors fusion, google map, GPS and many more.

Car ask for help:-

A self-driving car is one good option to minimize the road accident, but in some cases, some exceptional incident happen or road accident happen. In that case How car will help another car, the way human has an approach to handle the situation and help other peoples if some car accident happen. Or else we can take above example where a car is suddenly stalls and stops and there is no garage.

Solution:-

Car to car communication will be the best one. Car-to-car or vehicle-to-vehicle communication, it lets cars broadcast their position, speed, steering-wheel position, brake status, and other data to other vehicles within a few hundred meters. The other cars can use such information to build a detailed picture of what’s unfolding around them, revealing trouble that even the most careful and alert driver, or the best sensor system, would miss or fail to anticipate.

Due to some problem car stalls or stop, so that car will communicate to another car for towing service or another kind of help. The solution is in a more general way like the car will connect to local hospitals or local police station or generate some alert sound. The car will be enough intelligent to

A problem statement is one but solution may be different based on context, terrain, and ecosystem.

To implement this solution automotive manufacturers need to agree upon communication standards and data privacy.

Let me know your solutions on this or let me help out to understand a problem in more depth.

If you enjoyed this article, please don’t forget to Clap.

For more stories.

Lets connect on Stackoverflow , LinkedIn , FacebookTwitter.

Architecture for IoT applications.

Architecture for IoT applications.

Architecture that’s built to heal.

If you are not aware of the software architecture then go through this tutorial Software architecture. In this tutorial, we are gone through software architecture, not the hardware architecture & electronic device connectivity.

What is IOT?

Before going into the what is IOT let me tell one story that we all know? The story of blind men & elephant originated in the Indian subcontinent. It is a story of a group of blind men (or men in the dark) who touch an elephant to learn what it is like. Each one feels a different part, but only one part, such as the side or the tusk. They then compare notes and learn that they are in complete disagreement. The same thing with IOT we did.

The internet of things (IoT) is the internetworking of physical devices, vehicles (also referred to as “connected devices” and “smart devices”), buildings and other items — embedded with electronics, software, sensors, actuators, and network connectivity that enable these objects to collect and exchange data.

— wikipedia

If you overlook the definition, then we say there is nothing new it is just the stack of technologies which you already familiar. So someone says it is sensor programming, embedded programming, big data, machine learning, map reduce, etc. And one seeing man come and say this is NOT (this is the elephant).

The term “The Internet of Things” was coined by Kevin Ashton in a presentation to Proctor & Gamble in 1999. Ashton is a co-founder of MIT’s Auto-ID Lab. He pioneered RFID use in supply-chain management.

Now you are aware of what is IOT & software architecture. Then let us combine both things.

How we define IOT application architecture?

For defining architecture I will not go into the electronic & hardware part, I will keep you at the software stack and How we will need to arrange the software technologies as part of our dream IOT product.

This is just the blocks I arranged. Let us discuss in brief. We will explain from bottom to top.

  1. The bottom & core part of IOT application is the sensors and electronic devices that are able to connect to things & grab the data from it.

2. The sensor collects the data but that we need to convert it into understandable format & connect those sensor devices using some protocol that we need to configure here in layer two and also filter data i.e put some threshold for your data for taking a smart decision.

3. Network connectivity, connect your device with wireless connectivity or internet wired connection. This connectivity is changed based on context & domain.

4. We can say this layer as a security layer or application abstraction layer or data abstraction where we can apply security to our product. This layer position should be changeable based on the domain & How we want to apply abstraction to our application.

5. At this stage, we will persist our logic, use this data for taking a smart decision or for reporting purpose. This is the important layer, where our actual product & business logic comes into the picture.

6. This layer where we can say its presentation layer or decision was taken layer. Based on the requirement we can display reports or applying machine learning or some custom logic and takes a smart decision and send a signal back to the sensors.

7. This is where our God exists. For whom we are designing this whole product. The user interacts with this layer. This is the UI layer.

This is the brief description about we can arrange the blocks to design our IOT product.

IOT is simply connectivity of application, device and data.

For the more technical way, we can understand the same architecture.

There are number IOT device vendors & service provider every one has there own SDK & different protocols also there is a number of ways to connect a device to a network.

For more technical, I have designed simple IOT architecture using the Java stack. Here What we are following architecture for our use case.

On the left side is our sensors or core part of our IOT application.

Define architecture is art. There is a number perspective which is needed to be considered while designing architecture. This is a simple & general perspective to design IOT application architecture. Internet of Things for Architects.

This is just an idea. Dig deeper find more & let me know as well.

For more stories.

Lets connect on Stackoverflow , LinkedIn , Facebook& Twitter.

Android for a smart device.

Android for a smart device.

For more stories.

Android Things is Google’s new OS for smart devices.

What is Android Things?

If you can build an app, you can build a smart device.

Android things are Google android based IOT (Internet of Things) platform. Android things have android os stack with additional support for IOT. Google IO 2015 google announced Brillo & Wave for IOT.

Android things pushed the boundaries of android mobile os to work on hardware peripheral devices & drivers.

Overview of Android Things.

Android things platform is android based but this is different from the Android mobile OS. Below image describes the overview architecture of Android things. Android Things provide you the ease & power of Android. Enhance your device with google & android things.

Don’t reinvent wheel, build autonomous vehicle.

Android things platform come with Android SDK, Google services Java API F/W with things support a library. If you are already familiar with android or java then download preview version & build your own IOT device.

Android Things is an additional plugin in Android stack, don’t worry to learn from scratch just take few minutes & build it.

Use android infrastructure? Why we have to use Android Infrastructure?

Android is built in a mature platform for device program. Android is available for mobile, watch, TV, car and know for devices. The best part of Android Things development is very similar to traditional Android mobile development and involves writing apps using the Android framework with android studio IDE and tools. You need is a development board flashed with the Android Things OS and the required peripherals for your device.

Though Android Things devices will be able to integrate with Android (and or other ) devices, they would do so through Weave, a related but distinct communications system that Google launched alongside Brillo back in 2015.

The hope is that experienced developers will be able to quickly adapt, get up to speed and start work on a new product.

Android is now every where isn’t it? Arduino kit.

Advantages of Android Things.

  1. Open source.
  2. Android ecosystem.
  3. New APIs for IOT devices.
  4. Google API & services are build in.
  5. Trusted Security.
  6. The power of google at fingertips.

Getting started Android Things.

Build you first APP.

To download system image & test application.

Make Hands dirty with more sample.

Go through the Android things Github page https://github.com/androidthings.

This is just trailer. Go deeper find more & let me know as well.

If you enjoyed this article, please don’t forget to Clap.

For more stories.

Lets connect on Stackoverflow , LinkedIn , Facebook& Twitter.