license: mit
language:
- en
metrics:
- confusion_matrix
- accuracy
- precision
- f1
- recall
pipeline_tag: image-classification
Model Card for Model ID
A custom cnn base model trained specifically to classify images of fruits and vegetables.
Model Details
Model Description
- Model Name: Fruit-Veg-Img-Classifier
- Model Type: Image Classification
- Framework: TensorFlow
- Language: Python, Jupyter Notebook
- License: MIT
- Author: Senadhi Chandrasekara
- Repository: GitHub
- Demo: Streamlit App
Overview
The Fruit-Veg-Img-Classifier is a deep learning-based Convolutional Neural Network (CNN) designed to classify images into 36 categories of fruits and vegetables. This model was initially inspired by a tutorial by KothaEd on YouTube and has been enhanced with custom modifications for improved performance and usability. It is trained on a dataset of approximately 3,825 images, achieving a Top-3 Accuracy of 0.96657383.
Model Architecture
- Input: Images resized to 180x180 pixels.
- Layers:
- Rescaling layer (normalizes pixel values to [0, 1]).
- Three Conv2D + MaxPooling2D blocks (16, 32, 64 filters).
- Flatten layer.
- Dropout (0.2) for regularization.
- Dense layer (128 units) followed by an output layer (36 units with softmax activation).
- Optimizer: Adam
- Loss Function: Sparse Categorical Crossentropy
- Training: 39 epochs with data augmentation (random flips, rotations, zooms).
- Metrics: Accuracy, Top-3 Accuracy
Training Data
- Dataset: Custom dataset of 36 fruit and vegetable classes (e.g., apple, banana, beetroot, etc.).
- Split:
- Train: ~3,115 images
- Validation: ~351 images
- Test: ~359 images
- Source: Google Drive Dataset
- Preprocessing: Resizing, shuffling, batching, and data augmentation.
Performance
- Training Accuracy: ~95-98%
- Validation Accuracy: ~85-95%
- Top-3 Accuracy: 0.96657383
- Evaluation Metrics: Includes confusion matrix, precision, recall, and F1 score (computed via
model-evaluation.ipynb).
Usage
This model is deployed as a web application using Streamlit, accessible online. Users can input images via URL pasting, drag-and-drop, or file browsing. The model predicts the fruit or vegetable class with a confidence score (e.g., "Fruit in image is apple with an accuracy of 99.95%").
Installation
pip install tensorflow streamlit numpy pandas matplotlib huggingface_hub
How to Use
- Load the model from Hugging Face using an access token stored in a
.envfile. - Integrate with the provided
app.pyfor web deployment or useimage-classification-model-refine.ipynbfor local training. - Example inference can be run locally after cloning the repository:
git clone https://github.com/senadhi-Thimanya/image-classification-neural-network-model.git.
Use the code below to get started with the model.
import numpy as np
import tensorflow as tf
from tensorflow import keras
model = tf.keras.models.load_model('Image_classify.keras')
data_train_path = 'Fruits_Vegetables/train'
data_train = tf.keras.utils.image_dataset_from_directory(
data_train_path,
shuffle=True,
image_size=(180,180),
batch_size=32,
validation_split=False)
data_cat = data_train.class_names #download the dataset
image = 'corn.jpg'
image = tf.keras.utils.load_img(image, target_size=(180,180))
img_arr = tf.keras.utils.array_to_img(image)
img_bat=tf.expand_dims(img_arr,0)
predict = model.predict(img_bat)
score = tf.nn.softmax(predict)
print('Veg/Fruit in image is {} with accuracy of {:0.2f}'.format(data_cat[np.argmax(score)],np.max(score)*100))
Limitations
- Requires internet access for Hugging Face model loading in the web app.
- Performance may vary with images significantly different from the training dataset.
- No support for local file I/O in the web app due to Streamlit constraints.
Credits
- Original Tutorial: KothaEd (YouTube: Image Classification Deep Learning Neural Network Model in Python with TensorFlow).
- Enhancements: Custom training (39 epochs), data augmentation, Hugging Face integration, and multi-input web app features by senadhi thimanya.
Citation
If you use this model, please cite the GitHub repository and acknowledge the original tutorial by KothaEd.
Model Card Authors
Senadhi Chandrasekara