Task
In this project, we will be classifying a fruit and displaying its name as output from the given photo of the fruit as input.
Dataset- https://www.kaggle.com/sshikamaru/fruit-recognition
The dataset consists of 33 selected different kinds of fruits. Each folder is named after a fruit and contains over 400 images of that fruit in different angles and lightings. Based on the given image, we need to classify the fruit as one of the 33 categories.
Preprocessing –
- First, import all the required libraries –
- Download the data and unzip it to access the images and labels from your notebook.
- There are total 33 types of fruits in our dataset. Map them and print them accordingly
output:
{\'Pepper Green\': 0, \'Lemon\': 1, \'Cantaloupe\': 2, \'Passion Fruit\': 3, \'Pineapple\': 4, \'Apricot\': 5, \'Banana\': 6, \'Pomegranate\': 7, \'Pear\': 8, \'Avocado\': 9, \'Potato Red\': 10, \'Plum\': 11, \'Cucumber Ripe\': 12, \'Strawberry\': 13, \'Cactus fruit\': 14, \'Raspberry\': 15, \'Tomato\': 16, \'Pepper Red\': 17, \'Peach\': 18, \'Blueberry\': 19, \'Onion White\': 20, \'Orange\': 21, \'Watermelon\': 22, \'Kiwi\': 23, \'Limes\': 24, \'Apple Granny Smith\': 25, \'Apple Braeburn\': 26, \'Cherry\': 27, \'Grape Blue\': 28, \'Corn\': 29, \'Mango\': 30, \'Clementine\': 31, \'Papaya\': 32}
Split the images into train, validation, test sets
- Perform data augmentation by using ImageDataGenerator so that we can acquire more relevant data from the existing images by making minor alterations to the dataset.
- Divide the training dataset into train set and validation set.
output:
Found 13309 images belonging to 33 classes. Found 3314 images belonging to 33 classes.
Training the model
- Compile and fit the model
output:
Epoch 1/10 415/415 [==============================] - 43s 103ms/step - loss: 2.1697 - accuracy: 0.3067 - val_loss: 0.8547 - val_accuracy: 0.6739 Epoch 2/10 415/415 [==============================] - 42s 102ms/step - loss: 0.7189 - accuracy: 0.7288 - val_loss: 0.3619 - val_accuracy: 0.8514 Epoch 3/10 415/415 [==============================] - 43s 103ms/step - loss: 0.3826 - accuracy: 0.8639 - val_loss: 0.2193 - val_accuracy: 0.9215 Epoch 4/10 415/415 [==============================] - 43s 102ms/step - loss: 0.2220 - accuracy: 0.9222 - val_loss: 0.1437 - val_accuracy: 0.9478 Epoch 5/10 415/415 [==============================] - 43s 104ms/step - loss: 0.2096 - accuracy: 0.9319 - val_loss: 0.0678 - val_accuracy: 0.9789 Epoch 6/10 415/415 [==============================] - 43s 103ms/step - loss: 0.1355 - accuracy: 0.9560 - val_loss: 0.0984 - val_accuracy: 0.9635 Epoch 7/10 415/415 [==============================] - 44s 106ms/step - loss: 0.1193 - accuracy: 0.9596 - val_loss: 0.0309 - val_accuracy: 0.9900 Epoch 8/10 415/415 [==============================] - 43s 103ms/step - loss: 0.1169 - accuracy: 0.9626 - val_loss: 0.0714 - val_accuracy: 0.9740 Epoch 9/10 415/415 [==============================] - 43s 104ms/step - loss: 0.1030 - accuracy: 0.9677 - val_loss: 0.0205 - val_accuracy: 0.9934 Epoch 10/10 415/415 [==============================] - 42s 102ms/step - loss: 0.0963 - accuracy: 0.9704 - val_loss: 0.0706 - val_accuracy: 0.9749
Loss and accuracy graphs –
Predicting a fruit from the test set
- I have chosen a random image from the test set
- Load the image and convert it to appropriate size
- Make predictions
output:
Prediction is Pineapple.
Also Read: Object Classification(Electric Car and Electric Bus Classification)
Conclusion
Hence, we have trained a sequential model in keras to predict the name of the fruit with an image of a fruit as the input.
Notebook link: https://cainvas.ai-tech.systems/use-cases/fruits-classification-app/
Credit: Amruta Koshe
You may also be interested in
- Learning more about Fingerprint pattern classification using Deep Learning
- Reading about Mushroom Classification Using Deep Learning
- Finding out about Deep Learning, the technology enabling machines to interpret complex data and mimic human cognition
Become a Contributor: Write for AITS Publication Today! We’ll be happy to publish your latest article on data science, artificial intelligence, machine learning, deep learning, and other technology topics