This repository contains an image classification model developed using Convolutional Neural Networks (CNN) for classifying fruits into 6 different categories: fresh apples, fresh bananas, fresh oranges, rotten apples, rotten bananas, and rotten oranges. The model uses a deep learning approach to classify images of fruits into these categories with high accuracy.
This project employs a CNN architecture to perform classification of fruit images. The model is designed to classify images into six classes:
- Fresh Apples
- Fresh Bananas
- Fresh Oranges
- Rotten Apples
- Rotten Bananas
- Rotten Oranges
This model uses the "Fruits Fresh and Rotten for Classification" dataset available on Kaggle. The dataset consists of images of both fresh and rotten fruits, categorized into six classes: fresh apples, fresh bananas, fresh oranges, rotten apples, rotten bananas, and rotten oranges.
The dataset was created and shared by Sriram R on Kaggle. You can access the dataset here: Fruits Fresh and Rotten for Classification Dataset.
I would like to extend my gratitude to the dataset creator for providing this valuable resource that made this project possible.
The model is a Convolutional Neural Network (CNN) built from scratch using the Keras Sequential API. Below are the details of the architecture:
- Input Layer:
- Input shape:
(img_height, img_width, 3)(where the image height and width are specified, and 3 corresponds to the RGB color channels).
- Input shape:
- Convolutional Layer 1:
- Number of filters: 32
- Filter size:
(3, 3) - Activation function: ReLU (Rectified Linear Unit)
- Padding: Valid (no padding)
- Stride: 1 (default)
- Max-Pooling Layer 1:
- Pool size:
(2, 2) - Stride: 2 (default)
- Pool size:
- Convolutional Layer 2:
- Number of filters: 64
- Filter size:
(3, 3) - Activation function: ReLU
- Max-Pooling Layer 2:
- Pool size:
(2, 2) - Stride: 2 (default)
- Pool size:
- Convolutional Layer 3:
- Number of filters: 128
- Filter size:
(3, 3) - Activation function: ReLU
- Max-Pooling Layer 3:
- Pool size:
(2, 2) - Stride: 2 (default)
- Pool size:
- Flatten Layer:
- Flattens the 3D output into a 1D array to feed into the fully connected layers.
- Fully Connected (Dense) Layer:
- Number of neurons: 256
- Activation function: ReLU
- Dropout Layer:
- Dropout rate: 0.5 (50% of the neurons are randomly dropped during training to prevent overfitting).
- Output Layer:
- Number of neurons: 6 (corresponding to the 6 fruit classes).
- Activation function: Softmax (to output probabilities for each class in multi-class classification).
The architecture begins with three convolutional layers, each followed by max-pooling layers. The convolutional layers extract features from the input images, while the pooling layers reduce the spatial dimensions to avoid overfitting. After flattening the output, it is passed through a fully connected layer with 256 neurons, followed by a dropout layer for regularization. The output layer, with 6 neurons, uses the softmax activation to predict the class probabilities for the six fruit categories.
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Fresh Apples | 0.94 | 0.95 | 0.94 | 395 |
| Fresh Banana | 0.99 | 0.96 | 0.98 | 381 |
| Fresh Oranges | 0.97 | 0.96 | 0.96 | 388 |
| Rotten Apples | 0.91 | 0.91 | 0.91 | 601 |
| Rotten Banana | 0.99 | 0.97 | 0.98 | 530 |
| Rotten Oranges | 0.89 | 0.95 | 0.92 | 403 |
- Accuracy: 93%
- Macro Average:
- Precision: 0.95
- Recall: 0.95
- F1-Score: 0.95
- Weighted Average:
- Precision: 0.95
- Recall: 0.95
- F1-Score: 0.95
- Training Accuracy: 90.54%
- Test Accuracy: 94.8%
- Validation Accuracy: 93.6%
The dataset consists of images categorized into the six fruit classes. The images were preprocessed to:
- Resize all images to a uniform size (e.g., 150*150 pixels).
- Normalize pixel values to the range [0, 1] to help with faster convergence during training.
- Augment the dataset with transformations like rotation, flipping, and zooming to improve generalization and prevent overfitting.
The model was trained on a split dataset:
- Training Set: 80% of the dataset
- Validation Set: 10% of the dataset
- Test Set: 10% of the dataset
We used the following parameters:
- Epochs: 15
- Batch Size: 32
- Optimizer: Adam optimizer with a learning rate of 0.0001.
- Loss Function: Categorical cross-entropy.
The model was evaluated using accuracy, precision, recall, and F1-score to determine its performance on both the training and test datasets. The results above show the model’s effectiveness in distinguishing between fresh and rotten fruits.
1. Python 3.x installed. 2. TensorFlow installed. 3. Numpy installed.
You can load the trained model using the following code:
from keras.models import load_modelmodel = load_model('fruit_classification_model.h5')
Once the model is loaded, you can classify a new image using the following code:
import numpy as np from keras.preprocessing import imageimg_path = 'path_to_image.jpg' # Replace with the path to the image img = image.load_img(img_path, target_size=(150, 150))
img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) # Add batch dimension img_array = img_array / 255.0 # Normalize the image
prediction = model.predict(img_array) classes = ['Fresh Apples', 'Fresh Bananas', 'Fresh Oranges', 'Rotten Apples', 'Rotten Bananas', 'Rotten Oranges'] predicted_class = classes[np.argmax(prediction)]
print(f'The predicted class is: {predicted_class}')
- Input: An image of a fresh apple.
- Output: The predicted class is: Fresh Apples.
The model demonstrates good performance, with an accuracy of 94.8% on the test set. The precision and recall scores for the classes are generally high, particularly for rotten bananas, which has an impressive precision of 0.99 and recall of 0.97.
The model shows some difficulty with rotten apples, having a lower recall (0.91), which might be due to image quality or similarity with other fruit types. However, the rotten bananas and other classes have very high precision and recall, which is a good indicator that the model can differentiate between fresh and rotten fruits quite well.
This fruit classification model provides a reliable solution for distinguishing between fresh and rotten fruits, using deep learning techniques with Convolutional Neural Networks (CNN). The model achieved a high test accuracy of 94.8% and can be easily used for classifying images of fruits into one of six categories.
- Increase dataset size for better model generalization.
- Enhance model architecture by experimenting with more CNN layers or more advanced models like ResNet or Inception.
- Implement real-time fruit classification using camera feeds for practical applications.
