Welcome to the fascinating world of "Fruit Classification using Deep Learning"! Our project harnesses the power of convolutional neural networks, specifically the MobileNetV2 architecture within the TensorFlow framework, to classify various types of fruits with remarkable accuracy. With a meticulously curated dataset comprising a wide array of fruit images, our model achieves an impressive 99.9% accuracy during training and maintains a robust 99.5% accuracy in testing. Beyond its performance, this project extends its accessibility by offering a Gradio-based machine learning application. This intuitive application allows users to effortlessly input images of fruits, receiving swift and accurate predictions regarding their classification. In this fusion of technology and nature, our project not only showcases the capabilities of deep learning in image recognition but also opens doors for broader applications in agriculture, food industry, and education.
Dataset
For this project, we gathered a comprehensive dataset from [https://www.kaggle.com/datasets/moltean/fruits], encompassing a diverse range of fruit images. The dataset consists of training, testing, and validation sets, each containing high-resolution images of fruits in JPEG format with dimensions of 224 x 224 pixels.
Model Architecture
We employed the MobileNetV2 architecture to train our model. Utilizing the Adam optimizer with a learning rate of 0.0001, our model exhibits exceptional performance. Below is a summary of the model architecture:
image/png
e metrics were achieved during training and validation:
Training accuracy: 99.9%
Validation accuracy: 99.5%
image/png
Gradio-based ML App
The project involves creating a Gradio app for fruits classification. It utilizes a pre-trained fruit classification model (model.h5) and class labels stored in a CSV file (classLabel1.csv). Users can upload images of fruits to the app, and it provides real-time predictions for the fruits's class label.Users can upload images of fruits to the app, and it provides real-time predictions for the fruits's class label. The code integrates Gradio, TensorFlow, and pandas to create an interactive and user-friendly interface for fruit classification.
Here's a step-by-step description of how the code sets up a Gradio app for fruit classification:
1. Importing Libraries:
The code starts by importing the necessary libraries:
python
1import gradio as gr
2import tensorflow as tf
3from tensorflow.keras.models import load_model
4from tensorflow.keras.preprocessing import image
5from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6import pandas as pd
7import numpy as np
2. Loading the Trained Model:
The code loads the pre-trained fruit classification model (model.h5) using TensorFlow's load_model function.
The predict_fruit function is defined to take an image as input, preprocess it, make predictions using the loaded model, and return the predicted class label.
python
1defpredict_fruit(img):2# Preprocess the image3 img_array = image.img_to_array(img)4 img_array = np.expand_dims(img_array, axis=0)5 img_array = preprocess_input(img_array)67# Make predictions using the loaded model8 predictions = model.predict(img_array)910# Custom decoding based on model's classes11 top_prediction_index = np.argmax(predictions)12 top_prediction = class_labels[top_prediction_index]1314return top_prediction
5. Creating Gradio Interface:
The gr.Interface class is used to create the Gradio interface:
python
1iface = gr.Interface(2 fn=predict_fruit,3 inputs=gr.Image(),4 outputs=gr.Textbox(),5 live=True,6 title='Fruit Classification App',7 description='Upload an image of a fruit to get the predicted class label.'8)
fn: Specifies the prediction function.
inputs: Set to gr.Image() to accept an image as input.
outputs: Set to gr.Textbox() to display the predicted class label as text.
live: Set to True for live updates.
title: Sets the title of the Gradio interface.
description: Provides a description of the interface.
6. Launching the Gradio App:
The iface.launch() method is called to launch the Gradio app, providing a link to the app.
iface.launch()
7. Running the Code:
After running the code, users can click on the provided link to open the Gradio app in a new tab. The app allows users to upload an image of a fruit, and it provides real-time predictions for the fruit's class label.
Sample Code (main.py)
python
1import gradio as gr
2import tensorflow as tf
3from tensorflow.keras.models import load_model
4from tensorflow.keras.preprocessing import image
5from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6import pandas as pd
7import numpy as np
89# Load the pre-trained model10model_path ='model.h5'11model = load_model(model_path)1213# Load class labels from CSV14csv_file_path ='classLabels.csv'15df_class_labels = pd.read_csv(csv_file_path)16class_labels = df_class_labels['ClassLabel'].tolist()1718# Define prediction function19defpredict_fruit(img):20# Preprocess the image21 img_array = image.img_to_array(img)22 img_array = np.expand_dims(img_array, axis=0)23 img_array = preprocess_input(img_array)2425# Make predictions using the loaded model26 predictions = model.predict(img_array)2728# Custom decoding based on model's classes29 top_prediction_index = np.argmax(predictions)30 top_prediction = class_labels[top_prediction_index]3132return top_prediction
3334# Create Gradio interface35iface = gr.Interface(36 fn=predict_fruit,37 inputs=gr.Image(),38 outputs=gr.Textbox(),39 live=True,40 title='Fruit Classification App',41 description='Upload an image of a fruit to get the predicted class label.'42)4344# Launch the Gradio app45iface.launch()
Conclusion
This project demonstrates the seamless integration of deep learning techniques with user-friendly interfaces to solve real-world problems. By harnessing the power of convolutional neural networks and tools like Gradio, we can create efficient solutions for image classification tasks. The provided Gradio app empowers users to effortlessly classify fruits, showcasing the potential of AI in various domains. We invite you to explore our project repository and experience the magic of fruit classification with deep learning!