Let’s start! The first phase is to create a Neural Network for image classification. We will use a Neural Network Builder from TensorFlow and the CIFAR-10 dataset. Tensorflow is an essential for Artificial Intelligence library to develop and train our neural network.
The dataset comprises +50,000 images and is crucial for training image recognition models. We will be able to put our model to the test with Taipy, and build an application.
Our model will be trained on the ten categories present in the CIFAR dataset:
airplane ✈️
automobile 🚗
bird 🦜
cat 🐈
deer 🦌
dog 🐶
frog 🐸
horse 🐴
ship ⚓
Our model will be able to classify images into these ten categories.
Creating a Neural Network Builder
Prerequisites
Python- The Python programming language should be available on your computer
virtualenv - A tool for creating isolated virtual Python environments I will be using virtualenv for this project; however, you can use your preference, like venv, or Conda, and adapt your commands.
IMPORTANT : Depending on your setup, you might need to use the command python or python3 when running commands in the terminal
Setup
Ok, time to build! Run these commands to set your project up:
Now let's install the real deal, two Python libraries: TensorFlow and numpy- a library for mathematical operations on arrays.
Use the following command:
pip install tensorflow numpy
Download CIFAR dataset
But first, data! Now, let’s download the CIFAR-10 dataset from here.
The one we need is the CIFAR-10 Python version. When you download and unzip the files, you should see a folder named cifar-10-batches-py. Copy that folder and all the files inside the project we just created: neural-network-builder/venv.
Python script: generate-model.py
Create a file called generate-model.py. This will be our Python script for the model training and exporting.
Add the code below to the generate-model.py.
Run the generate-model file
Moment of truth! Run your file. This file will build, train, and save our model.
Depending on your computer, this process can take more or less time.
Let’s focus on the parameter called epochs in our script, which is set to epochs=50.
An epoch is an important hyperparameter, representing one complete cycle through all training datasets. Each sample will update the model’s parameters. This cycle is not about the time it takes but the number of times it runs through the data. This key parameter influences the training process. Indeed, having more epochs affects the model’s learning rate.
The higher the number, the longer it will take to train the model.
Underfitting could happen if there are insufficient epochs for the model to identify the underlying patterns in the data. However, an excessive number of epochs might cause the model to overfit the training set, resulting in subpar generalization on fresh, untried data.
To run the script, use this command here (you might need to use Python or python3):
Now, a little patience! Wait for your model to be trained and saved in the model folder. When creating our GUI front end, we will copy that model folder. Now, let’s add a GUI to play with our model!
Build our Taipy interface
Setup
Navigate back to the main project folder ml-photo-app and then run these scripts to set up our interface:
Now, let’s install the Python libraries we will be using:
Taipy
TensorFlow
Numpy
Pillow ( PIL) - Python imaging library
Run this command:
Frontend folder
Let’s get our fresh new model and copy it from our neural-network-builder project into our frontend folder. Create two files in our root folder inside of frontend:
index.py
index.css.
These are the files we will run for our GUI.
Set your CSS preferences: index.css
Add this code to the index.css file we just made:
Create the index.py Python script
To create an application with Taipy, you can use Markdown, the Python API, or HTML. In this tutorial, we will use the HTML method, but feel free to use whichever option!
And lastly, add this code to the index.py script:
Our application will run on the 8000 port here, but feel free to change it.
Run the application
With the use_realoder set to *True”, if you make any changes to your GUI code, you don’t have to rerun everything; just refresh your application page.
To run our app, use this command here:
How to use the application?
Upload any .png, preferably with an image part of these ten categories!
airplane ✈️
automobile 🚗
bird 🦜
cat 🐈
deer 🦌
dog 🐶
frog 🐸
horse 🐴
ship ⚓
Have fun seeing how your application classifies images!