TK

Fruits Classification using Edge Impulse

Mamadou K. KEITA
ILLUMINATION
Published in
7 min readMar 21, 2023

--

Edge Impulse Logo

Many platforms allow you to build machine learning models with no prior experience and no code required. These platforms are called no/low code platforms and are becoming increasingly known in the industry due to the speed of building and deployment. One of my favorites is Edge Impulse.

In this article, I will show you step-by-step how to create a fruit classification model using Edge Impulse and run it on your phone.

Prerequisites:

  • Access to Internet
  • Edge impulse account
  • Download the dataset
  • GitHub
  • Some knowledge of python

Before we dive into the tutorial, we need to understand what Edge Impulse is and how it works

Edge Impulse:

It is a platform that enables developers to create intelligent device solutions using machine learning. Edge Impulse provides tools for collecting data, training machine learning models, and deploying those models to embedded devices.

With Edge Impulse, developers can build machine learning models that can run on small, low-power devices, making it possible to add intelligence to a wide range of Internet of Things (IoT) and other connected devices. The platform supports a variety of hardware platforms, including Arm Cortex-M microcontrollers, and provides a range of pre-built signal processing and machine learning blocks to make it easier to get started.

We can see that Edge Impulse can be a great tool for people looking to add some intelligent capabilities to their IoT and other embedded devices.

So now let us move to the practice!!

1. Create an account:

To create your account, click on this link: https://studio.edgeimpulse.com/signup. You should land on this page: (put the image here)

sign up form

Make sure to correctly fill out the form.

2. Create your project:

Once you finish creating your account, you will have to create a project by clicking on “create a new project.”

After that, the box below will pop up. Select “images” and after clicking “getting started.

Now you are in your project working space. You can see all the actions you can do in the navigation bar.

NB: You can have up to three collaborators on your project.

3. Download the Dataset from GitHub:

Every machine learning project starts with data. “Data is the most vital ingredient in Machine Learning and Artificial Intelligence. It refers to information that has been converted into a form that is more efficient for processing and transfer.” The dataset we will use contains (number of images)images of fruits and vegetables classified inside different folders labeled into their names. The dataset is on Git Hub: https://github.com/Mamadou2727/fruit-classification-dataset. You can either clone it or download the zip file, it’s up to you.
NB: In this tutorial, we will build a model that can distinguish between nine different fruits — we have used Apple Braeburn, Apple Granny, Avocado, Banana, Lemon, Mango, Orange, Pineapple, and Tomato.

4. Import Data into our project:

Click on “Let’s collect some data” and click on “Go to the uploader.

Choose the automatic split option and enter the label name according to the dataset subfolder being used. For example, if you are uploading data for the dataset folder called “Apple Braeburn,” your label name should be Apple Braeburn too.

Edge Impulse will automatically split your data into 80% training sets, and 20 % testing sets. You can find the information about your data repartition. If everything is correct it should look like this:

5. Designing an impulse:

After collecting data for your project, you can now create your Impulse. A complete Impulse will consist of 3 main building blocks: an input block, a processing block, and a learning block.

This view is one of the most important, here you will build your machine learning pipeline.

Add Transfer Learning in your learning block as shown in the below image and save the impulse.

5.1 Configuring the processing block

After saving your impulse, you will notice a slight change in your navigation: you will see “Image” and “Transfer Learning” under “Create impulse.” Now you will have to click on “Image” to set up the rules/parameters for your images. Make sure it is the same as the below image.

Click Generate features to start the process.

Afterward the ‘Feature Explorer’ will load. This is a plot of all the data in your dataset. Because images have a lot of dimensions (here: 96x96x3=27,648 features) we run a process called ‘dimensionality reduction’ on the dataset before visualizing this. Here the 27,648 features are compressed down to just 3 and then clustered based on similarity.

5.2 Configuring the transfer learning model

With all data processed it is time to start training a neural network. Neural networks are algorithms, modeled loosely after the human brain, designed to recognize patterns. The network that we are training here will take the image data as input and try to map this to one of the three classes.

A solid working computer vision model is extremely difficult to create from scratch because it requires a wide range of input data to generalize well, and training such models on a GPU can take days. Transfer learning is being used to make this process simpler and quicker. With only the top layers of a neural network needing to be retrained, you can build more trustworthy models, train in a lot less time, and require much fewer datasets.

To configure the transfer learning model, click Transfer Learning in the menu on the left. Here you can select the base model (the one selected by default will work, but you can change this based on your size requirements), optionally enable data augmentation (images are randomly manipulated to make the model perform better in the real world), and the rate at which the network learns.

Set:

  • Number of training cycles to 20.
  • Learning rate to 0.0005.

After training our model, we will get 83.3% accuracy, which is good for the data we have.

6. Validating your model

With the model trained let us try it out on some test data. When collecting the data, we split the data up between a training and a testing dataset. The model was trained only on the training data, and thus we can use the data in the testing dataset to validate how well the model will work in the real world. This will help us ensure the model has not learned to overfit the training data, which is a common occurrence.

To validate your model, go to Model Testing, and click Classify All, which will classify all our testing data. Here we hit 100% accuracy, which is excellent.
NB: You will not always get 100% accuracy. You must make sure that your accuracy is good enough (I recommend 75% or more) to do your task.

7. Running the model on your device

With the impulse designed, trained, and verified you can deploy this model back to your device. This makes the model run without an internet connection, minimizes latency, and runs with minimum power consumption. Edge Impulse can package up the complete impulse — including the preprocessing steps, neural network weights, and classification code — in a single C++ library that you can include in your embedded software.

In this tutorial, we will run our model on a smartphone. To do so, click “Deployment,” scroll down, select “Mobile phone”, and build. Scan the QR code that will be generated and start using your model.

Congratulations! You have just made your first fruit classification model. It was easy, right?

If you are interested in more edge impulse tutorials, you can check the edge impulse community projects website.

Feel free to message me if you have any questions about AI, Machine Learning, Deep Learning, etc.

--

--

Mamadou K. KEITA
ILLUMINATION

Machine Learning Engineer & Researcher, Entrepreneur, Impact driven. "Technology and Innovation to serve People"