Gemstone Classification using Deep Learning

Photo by Ivan Mesaros on Dribbble

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

https://gist.github.com/AmrutaKoshe/e567ac49ea41768b3ce25808912531c2

  • Download the data and unzip it to access the images and labels from your notebook.

https://gist.github.com/AmrutaKoshe/037c74fd18f4b7720dc348d80517639d

  • There are 40 total categories of gemstones in our dataset. Map them and print them accordingly.

https://gist.github.com/AmrutaKoshe/dae206639a48ed59cc07eb82aa28e4b0

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 –

https://gist.github.com/AmrutaKoshe/bdf092b823186da9b1f9ca32af977908

  • 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

https://gist.github.com/AmrutaKoshe/443c5a20500846628024cf3b5b02279d

  • 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.

https://gist.github.com/AmrutaKoshe/b827efc03861f22376f7de1dc56f5008

Training a sequential model

https://gist.github.com/AmrutaKoshe/85254b0cc22b4879cef536a3653361d1

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

https://gist.github.com/AmrutaKoshe/2701c47ea05c0a0664c06fe8f397e812

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