Heat Convertor - Android + ML
Published: 2021-02-07 06:59:57

---
title: Heat Convertor - Android + ML
published: true
description: Building a heat converter app with Machine Learning in Android from Scratch
tags: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/4sgqf3xvi8f0rjkqemxs.png
 
---

---


### Building a heat converter app with Machine Learning in Android  from Scratch
#### Why this story?
Most of the students I met around last year, had common questions that,
They know how to train a model, but they struggle to use them?
Some of them, have confusion in where to start, how to build, how to export, Using it in android.

So, literally I just planned to build a real-time project from scratch.
In this project, we are going to use,
Tech Stack
Python ( prepare dataset ) + TensorFlow (train & build tflite model).
Android ( Java ).

IDE Used
Colab for python (Integrated python Notebook from Google)
https://colab.research.google.com
Android Studio
https://developer.android.com/studio


---

Let's start from building an ML Model in python
Create a New project in colab.

2. Set a run time, ( Hosted runtime )
![image](https://dev-to-uploads.s3.amazonaws.com/i/m9tqfova5lnommkwa39j.png)
 
3. Let's Start the code,
I will link the notebook which has the whole code along with its comments.
https://colab.research.google.com/drive/1WgOsInNfnT9HgUVixr6NC31m5dPNvyKn?usp=sharing
Just replicate this, and it is straight forward.
Let me explain the code,
## Import the following,
Tensorflow for basic ML Functions
Numpy for Nested Array representation

```
import tensorflow as tf
import numpy as np
```
## Prepare Dataset in an array,
X -> Celcius
Y -> Fahrenheit
```
celcius_list = np.array([0, 2, 8, 10, 11, 16, 22, 25, 66, 76, 83])
fahrenheit_list = np.array([32, 35.6, 46.4, 50, 51.8, 60.8, 71.6, 77, 150.8, 168.8, 181.4])
```
## Creating a Neural Network
```
layer = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([layer])
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))
```
## Training a Neural network with Dataset
```
history = model.fit(celcius_list, fahrenheit_list, epochs=1100, verbose=False)
```
## Testing & Visualizing the Network
![image](https://dev-to-uploads.s3.amazonaws.com/i/bkwipo2n1ajdo38m3j89.png)

## Importing the model to TFlite (As Android Asset)
![image](https://dev-to-uploads.s3.amazonaws.com/i/l16n0gglvkx8f7b1mvlb.png)
 
 
Now you can download the tflite file
Feel free to use my code linked above


---

It's Android time
1. Create a new project in android studio
2. Try to replicate this UI
![image](https://dev-to-uploads.s3.amazonaws.com/i/5adirsocmp1ep4oxulty.png)
 
3. Load the downloaded tflite model in the android code.
![image](https://dev-to-uploads.s3.amazonaws.com/i/o9xekfi9f56azuwf6nac.png)
 
4. In the OnCreate Function, paste this code.
![image](https://dev-to-uploads.s3.amazonaws.com/i/0sdp80645rbdk88byb2y.png)
 
5. Now your input has to be sent to the tflite file that has been loaded, So I am creating a function named doInference.
```
private Float doInference(String value) {
    float[] input = new float[1];
    input[0] = Float.valueOf(value);
    float[][] output = new float[1][1];
    tflite.run(input, output);
    return output[0][0];
}
```
After building this app, you will get this result,
![image](https://cdn-images-1.medium.com/max/600/1*Lg5U9vNHJQFFyZYTbXkIyA.gif)
 
Hurrah !!!
The App is working fine,
To check that we can use google converter,
![image](https://dev-to-uploads.s3.amazonaws.com/i/9s2kzr0bwhfjr16mrlpm.png)
 
We can get 99.5% accuracy, to get 100% we have to feed with some more data.
If you find any doubts, please feel free to ping me.
With love,
## Build for Billions