Google Colab for Mobile ML Integration
Google Colab for Mobile ML Integration
1 | Laboratory Activity 12
This activity guides you on the classification example called the iris dataset. The dataset contains 3 classes of 50 instances each, where
each class refers to the type of iris plant.
2. Integrate TensorFlow Lite libraries into an Android project and configure the application for optimal model performance on mobile
devices.
Attribute information:
1. sepal length in cm
2. sepal width in cm
3. petal length in cm
4. petal width in cm
Based on the information given in the input, we will predict whether the plant is Iris Setosa, Iris Versicolour, or Iris Virginica. You can
refer to this link for more (https://archive.ics.uci.edu/ml/datasets/iris) information.
Steps
1. Download the iris data set (file name: iris.data) from this (https://archive.ics.uci.edu/ml/machine-learning-databases/iris/)
(https://archive.ics.uci.edu/ml/machine-learning-databases/iris/) link.
2. Create a new python file with a name iris in the Jupyter notebook. Put the iris.data file in the same directory where iris.ipynb resides.
Copy the following code in the Jupyter notebook file.
https://tip.instructure.com/courses/58608/assignments/2406341 1/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
iris.ipynb
3. Paste the Python code below to create a model and convert to tflite.
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical
# normalizing labels
le = LabelEncoder()
y = to_categorical(y)
model = Sequential()
# input layer
# passing number neurons =64
# relu activation
https://tip.instructure.com/courses/58608/assignments/2406341 2/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
# shape of neuron 4
model.add(Dense(64, activation='relu', input_shape=[4]))
# processing layer
# adding another denser layer of size 64
model.add(Dense(64))
# compiling model
model.compile(optimizer='sgd', loss='categorical_crossentropy',
metrics=['acc'])
tfmodel = converter.convert()
open('iris.tflite', 'wb').write(tfmodel)
4. After executing the line open(‘iris.tflite’,’wb’).write(tfmodel) a new file named iris.tflite will get created in the same directory where
iris.data resides.
https://tip.instructure.com/courses/58608/assignments/2406341 3/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
https://tip.instructure.com/courses/58608/assignments/2406341 4/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
https://tip.instructure.com/courses/58608/assignments/2406341 5/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
9. Click on OK
https://tip.instructure.com/courses/58608/assignments/2406341 6/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
Your model will look like this after clicking on the finish. (It may take some time to load).
11. Copy the code and paste it in the click listener of a button later in MainActivity.Java.(It is shown below).
https://tip.instructure.com/courses/58608/assignments/2406341 7/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for
the activity_main.xml file.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
https://tip.instructure.com/courses/58608/assignments/2406341 8/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
tools:layout_editor_absoluteX="274dp"
tools:layout_editor_absoluteY="256dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/tf2"
android:layout_width="wrap_content"
android:layout_height="77dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/tf3"
android:layout_width="wrap_content"
android:layout_height="77dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/tf4"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal" />
https://tip.instructure.com/courses/58608/assignments/2406341 9/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside
the code to understand the code in more detail.
https://tip.instructure.com/courses/58608/assignments/2406341 10/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
https://tip.instructure.com/courses/58608/assignments/2406341 11/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
https://tip.instructure.com/courses/58608/assignments/2406341 12/16
11/18/24, 2:26 PM 5.2.1 | Laboratory Activity 12
Challenge Tasks
Include label .
Points 20
https://tip.instructure.com/courses/58608/assignments/2406341 13/16
COLLEGE OF INFORMATION TECHNOLOGY EDUCATION
CIT 306-IT32S4 - Mobile Computing
MIDTERM
Create a new python file with a name iris in the Jupyter notebook. Put the iris.data file in
the same directory where iris.ipynb resides. Copy the following code in the Jupyter
notebook file.
Paste the Python code below to create a model and convert to tflite.
Paste the Python code below to create a model and convert to tflite.
After executing the line open(‘iris.tflite’,’wb’).write(tfmodel) a new file named
iris.tflite will get created in the same directory where iris.data resides.
2. Create reflection about the this lab activity. 500 words. I know if the reflection is came
from AI. Please write in your own words.
When I started the instructions provided for Android exercise about Laboratory Activity
12, I discovered the fascinating topic of machine learning integration in mobile
applications. The challenge at hand was developing an application that could identify
the species of an iris plant by measuring its petals and sepals. First, the XML layout
design was created, and EditTexts were used to collect user input for the petals' and
sepals' lengths and widths. After receiving instruction on iris data, a TensorFlow Lite
model was used to analyze these inputs. This model was created with the particular
purpose of classifying iris plants into three different species: Iris Setosa, Iris Versicolour,
and Iris Virginica.
This code helps the computer learn to tell different iris flowers apart based on their
features like petal size. It starts by turning the flower names into numbers using iris
data. Then, it trains the computer to make good guesses about which iris flower it's
seeing. Once it's trained, the model can predict iris types accurately. Finally, the model
gets turned into a special format that works well on phones. So, you can use it on your
phone to identify iris flowers easily.
Combining the machine learning model with the user interface, the Java code was
created in an Android studio. The application launched immediately after the specified
button was clicked, taking the numerical values out of the EditTexts and formatting them
such that they could be inferred. For the purpose of enabling interaction with the
TensorFlow Lite model, a ByteBuffer was first created to retain the input data. A
TensorBuffer was then instantiated. After the TensorBuffer was filled with data, the
model performed inference and predicted the likelihood of each species of iris.
To evaluate the model's performance, I used certain samples in the code. I chose, for
instance, the 35th sample, which was labeled "Iris-setosa" and should have contained
characteristics with values of 4.9, 3.1, 1.5, and 0.2. Nevertheless, the fourth feature had
a mistake. Likewise, with the 38th sample, which was similarly designated "Iris-setosa,"
the predicted feature values were 4.9, 3.6, 1.4, and 0.1; however, the second and third
features had mistakes. These examples were essential for confirming the correctness of
the model and locating any possible problems or mistakes in its predictions. I was able
to evaluate the model's resilience and dependability in practical situations by testing
with such particular circumstances.
The display of the data on a TextView, integrated to show the probability of each iris
species in a logical manner, indicates the data that is being shown in the EditText.
Serving as a platform for the model's output, the TextView showed users how likely it
was that the plant belonged to each species, giving them important insights into the
classification procedure. Looking back on this project, I was amazed by how we
combined machine learning with Android apps to make them smart enough to predict
stuff based on what users input. It wasn't just about making the app work; it was about
learning how to blend these two big things together. Android and machine learning. It
made me see all the cool things we can do when we mix tech and creativity. It wasn't
always easy, but it definitely made me more excited about exploring this world where
technology and innovation meet. And it's just the beginning.