8 steps to build a world class image classifier.
Recently I was having chat with a colleague of mine who is studying Geological Engineering. He had seen me use Dlib to perform facial recognition tasks, and with a sense of awe after seeing the program learn to distinguish between myself and him given just a single picture, he asked….
Can it distinguish between minerals and rocks?
Well, I think it can…
This got my mind racing and probably yours too, thinking of the many ways in which AI specifically deep learning can help Geologists and Geophysicists in their day to day tasks.
To test the waters, we’re going address the initial question of distinguishing between minerals.
We are going to build an image classifier that distinguishes between quartz and amazonite
Well it turns out you are not the only one who couldn’t identify an amazonite, my colleague couldn’t and surprisingly neither does medium’s editor as it thinks amazonite is not a word.
Let’s see if our model can do better
To get enough images to train the model, I used a script suggested by Nikhil B in his post. I downloaded 200 images of each mineral, which was reduced to 138 after removing the corrupted and inappropriate ones. The images were split into 118 training images and 20 validation images for both classes.
Step 1: Set precompute = True
Setting precompute=True saves the activations of the last layers of the neural network. These activations are referred to as precomputed activatations. This generally makes it faster to train your model.
Step 2: Use
lr_find() to find highest learning rate where loss is still clearly improving
Based on the plot, I chose a learning rate of 0.01.
This approach to finding learning rates is based on the Cyclical Learning Rates for Training Neural Networks paper.
Step 3: Train last layer from precomputed activations for 1–2 epochs
This gave us an accuracy of 82.5%.
Step 4: Train last layer with data augmentation (i.e. precompute=False) for 2–3 epochs with cycle_len=1
With data augmentation, we randomly apply changes to the images in ways that shouldn’t impact their interpretation, such as flipping, zooming and rotating. This effectively creates more data for our model to train on.
cycle_len=1: enables stochastic gradient descent with restarts which ensures that we arrive at a global minimum.
We now have an accuracy of 95%…. that clearly helped!
Step 5: Unfreeze all layers
Because we are using a pretrained network, unfreezing the layers allows us to fine tune the weights of the earlier layers.
Step 6: Set earlier layers to 3x-10x lower learning rate than next higher layer
The learning rate for the earlier layers is smaller than the learning rate for the later layers because we don’t want to destroy the pretrained weights. We are only fine tuning.
Step 7: Use
Using learning rate finder again showed that a learning rate of 0.001 was going to be better going forward.
And at last…
Step 8: Train full network with cycle_mult=2 until over-fitting
cycle_mult=2: doubles the length of the cycle after each cycle.
I trained the model for 30 epochs, with the results of the last 8 epochs being shown here.
97.5% accuracy… Hurray !!!
Wait, what does that actually mean?
Let’s plot a confusion matrix to find out.
It turns out our model only makes one wrong classification.
This I think is a very great feat considering that our dataset is very small by all deep learning standards.
Back to our question: Can it distinguish between minerals and rocks?
It surely can… and I think you do agree with me on this. This approach can be used to analyze photographs and quickly and accurately recognize rock types .
- Can it be used to interpret seismic data.
- Can it recognize processes that were responsible for creation of rock formations.
- Can it be used for interpreting remote sensing images (RSIs).
Given the extreme effectiveness of the tools of deep learning that we have at our disposal today, computers can definitely be used for these tasks and many more. But it would require collaboration between deep learning practitioners and domain experts in the geo-sciences to see these ideas come to fruition.
If you enjoyed this post (actually this is my first), please recommend and share.
Source: Deep Learning on Medium