Task
A gemstone (gem, fine gem, jewel, precious stone, or semi-precious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewellery or other adornments.
In this project, we will classify the gemstone based on the given image.
Dataset — https://www.kaggle.com/lsind18/gemstones-images
The dataset consists of 40 different kinds of gemstones. The training folder contains 40different folders, each named after a gemstone, and containing 30–40 images each.
Preprocessing
- Start off by importing all the required libraries
- Download the data and unzip it to access the images and labels from your notebook.
- There are 40 total categories of gemstones in our dataset. Map them and print them accordingly.
output :
[\'Morganite\', \'Chrysoberyl\', \'Zoisite\', \'Serpentine\', \'Spodumene\', \'Spinel\', \'Onyx Red\', \'Iolite\', \'Larimar\', \'Chrome Diopside\', \'Carnelian\', \'Prehnite\', \'Andradite\', \'Sapphire Pink\', \'Aventurine Green\', \'Sapphire Yellow\', \'Quartz Beer\', \'Andalusite\', \'Rhodochrosite\', \'Alexandrite\', \'Quartz Smoky\', \'Cats Eye\', \'Danburite\', \'Tigers Eye\', \'Topaz\', \'Peridot\', \'Variscite\', \'Lapis Lazuli\', \'Quartz Rose\', \'Blue Lace Agate\', \'Chalcedony\', \'Hessonite\', \'Ametrine\', \'Sunstone\', \'Emerald\', \'Ruby\', \'Diamond\', \'Aventurine Yellow\', \'Dumortierite\', \'Chrysoprase\'] 40
Read the images and labels of gemstones from training data
- Functions to read images and labels from our dataset –
- Store training images and labels and get the shape.
Shape of train images: (2856, 100, 100, 3) Shape of train labels: (2856,)
Split the training dataset into train and validation sets
- Perform data augmentation on the training images, so that we can achieve more relevant data. Each image is rotated in different ways and multiple images are formed so that the model can learn the features of each gemstone effectively.
Training a sequential model
output:
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 100, 100, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 50, 50, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 50, 50, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 25, 25, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 25, 25, 64) 36928 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 12, 12, 64) 0 _________________________________________________________________ conv2d_3 (Conv2D) (None, 12, 12, 64) 36928 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 6, 6, 64) 36928 _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 3, 3, 64) 0 _________________________________________________________________ conv2d_5 (Conv2D) (None, 3, 3, 64) 36928 _________________________________________________________________ max_pooling2d_5 (MaxPooling2 (None, 1, 1, 64) 0 _________________________________________________________________ flatten (Flatten) (None, 64) 0 _________________________________________________________________ dense (Dense) (None, 87) 5655 _________________________________________________________________ activation (Activation) (None, 87) 0 _________________________________________________________________ dropout (Dropout) (None, 87) 0 _________________________________________________________________ dense_1 (Dense) (None, 87) 7656 _________________________________________________________________ activation_1 (Activation) (None, 87) 0 ================================================================= Total params: 180,415 Trainable params: 180,415 Non-trainable params: 0
Fit our dataset to the above model, and calculate the accuracy
output:
18/18 [==============================] - 0s 9ms/step - loss: 1.7533 - accuracy: 0.5245 loss:1.7532774209976196 accuracy:0.5244755148887634
Accuracy and loss graphs –
Making Predictions
Read images and labels from the test set using the above-mentioned methods, and make predictions on random images with our model.
The images with [OK] depict correctly classified images.
Conclusion
Hence, we have successfully trained a model to classify the gemstone based on the provided image.
Notebook link — https://cainvas.ai-tech.systems/notebooks/details/?path=AmrutaKoshe/gemstone.ipynb
Credit: Amruta Koshe
Also Read: Inventory Classification for Retail Management using Deep Learning