Insect Classifier Model

Nicholas Ternouth
unpack
Published in
4 min readAug 3, 2021

Objective

The objective of this project was to create an AI that had the ability to recognise different insects, being bees, ants, butterflies, mosquitos, damselflies, dragonflies and termites. I chose this objective because some people might have difficulties telling the differences between certain insects, such as, ands and termites as well as dragonflies and damselflies.

How I made the model

The model took a while to learn but was getting better over time. The accuracy of the model would increase by small increments, under 1% at a time, eventually culminating in an accuracy above 90%.

To gather the images I used the duck duck go image scraper so that none of the images gathered were personalised to me. As a result of this I had images that came up that were not what I was searching for, so I used one of the image scraper’s other functions to look at the images and delete the ones I was looking for and would have lead to the model being less accurate than it currently is.

the image scraper’s cleaner function — usually you would have images in place above the delete buttons

I used fastai to build this model, which peaked at an accuracy just above 92%. I achieved this by giving the model about 100 images of each insect, then using the fastai learner function to teach the ai.

fastai learner function

I then used the learner function again to generate a confusion matrix so I could see what errors the model was making.

confusion matrix for the model

The model made errors in places i expected it to with the confusion between damselflies and dragonflies, however it made fewer errors than I expected it to have with ants and termites.

to further increase the model’s accuracy i used the data augment function that changed the images in minor ways, such as making them darker or lighter or stretching them in certain ways.

Due to the cleaning being manual there were some images that I had missed resulting in them being used to teach the ai, in turn leading it to be less accurate. Luckily fastai has another function that allows you to view the images that have the highest error rate when guessing what was in the image which helped me find the images that i had missed previously.

here you can see the top losses as well as an image that I missed when cleaning the input data

After finding this out, I then went into the file containing the input data and deleted it which then increased the accuracy of my model by a small amount.

Results of the model

Using this function again, i could then see what it found the hardest to identify:

With this you can see that the AI had issues when looking at the difference between dragonflies and damselflies which I had expected from the beginning. In the left most image you can see that the model had an issue with recognising the mosquito in the image and got it confused with a damselfly, which I think was caused by the model learning about the types of grass and reeds that were in many of the images with damselflies due to their habitat, which then caused it to think that this image was of a damselfly rather than a mosquito.

You can also use a similar function to test the model:

you can see here that the model can differentiate between dragonflies and damselflies as well as ants and termites which was what I wanted it to be able to do the most.

Conclusion

In conclusion, I think that my project was a success, however, I think there are ways that it can still be improved. It can be improved by giving it more information to work with in the form of more images of each of the insects and trying to keep them all to a similar number as currently there are far fewer images of dragonflies than all of the other insects meaning it will inventively be less accurate when it comes to dragonflies.

--

--