Snake classification report

Snake classification report

Why?

5.4 million people are bitten by snakes every year and 81-138 million people die due to snake bites each year. Preventing snake bites is clearly a major issue, in need of preventative measures to save lives. The project Snaked demonstrates a potential solution to alleviate the problem of struggling to identify the snake which has bitten a person. A proof of concept app is available on the Google Play Store which uses this model.

Challenge

  • Each snake species varies in shape, colour, size, texture and more.
  • Over 3000 snake species have already been discovered worldwide!
  • Different snake species may look nearly identical, however, vary significantly

So it is clearly not an easy job to identify one snake from another, even though it's quintessential that we do.

Frameworks and Methods Utilized

PyTorch is used for ALL deep learning code and Numpy for numerical computations. The code is separated into the main file outlining the chosen algorithms (can be swapped/modified easily) and abstracted code to help train, evaluate and create an executable for any model easily.

There are several novel ideas/techniques used here which help to create neater/more readable pythonic code. The three primary examples of new PyTorch techniques:

  • The Item tuple class which allows modular and further extensible code (when extra data needs to be processed)
  • Use of super-convergence/the one-cycle policy in pure PyTorch instead of a highly abstracted library (i.e. Fast.AI)/bare python/numpy
  • Use of a dictionary to dictate how different data sets should be split up to allow easy modifications of data proportions (i.e. switching between full dataset for training and small batches for ensuring all code runs without errors)

Although several models were trialed out on the dataset, in the end, a MobileNetV2 model provided the best results, whilst also remaining relatively lightweight and so able to run on low-power devices like phones (essential for the app). The final model was trained using LDAM loss instead of cross-entropy loss due to the dataset being imbalanced (some classes having far more samples than others). Note that the codebase has support for classical rebalancing, however, experimental trials show that this method causes overfitting extremely early on. The model manages to achieve around a 70% accuracy and F1 score. More details about the choice of model, how it was improved and lessons learnt from this project can be found on The Data Science Swiss Army Knife blog.

Android Application

The android application created for this project was written with Kotlin, using Fotoapparat (for easy camera support) and PyTorch (for utilizing the chosen PyTorch model) libraries. Due to the lightweight MobileNetV2 model no network is required to connect to a server (which would normally run the computations itself). This is intentionally done to facilitate use within remote locations! Please note that this is only a sample proof of concept app and if you're bitten consult a medical expert immediately.

Sources

All data currently used for the project comes from AIcrowd's Snake Species Identification Challenge. The images and labels have been used, however, geographic locations have been ignored to allow easy usage for any image, even if it hasn't been tagged. The dataset allows 85 species to be labelled. A Jupyter Notebook is also provided which demonstrates how to collect further data using Google Image searches! All statistics used here, or within the repository are from the World Health Organisation (unless otherwise stated) or pertaining specifically to the Snaked source code.

For further information please see the following:

Metrics

Throughout this report, I'll refer to F1 scores as my primary metric. This is because of the class imbalance. Please ensure to note that there is a major difference between the support number and number of present samples for a class. The first refers to the count in the validation dataset, whereas the latter in the training dataset. The number present in the training set will be primarily used to judge the effect of a skewed dataset on model predictions.

Training and Validation Graphs

Train epoch vs accuracy

train_acc.png

Train epoch vs loss

train_loss.png

Validation epoch vs accuracy

validation_acc.png

Validation epoch vs loss

validation_loss.png

Model trained for 51 epochs

Classification Report

speciesprecisionrecallf1-scoresupport
agkistrodon-contortrix0.84523809523809520.87654320987654320.860606060606060681.0
agkistrodon-piscivorus0.6769230769230770.60273972602739720.637681159420289973.0
ahaetulla-prasina0.80.66666666666666660.72727272727272726.0
arizona-elegans0.6315789473684210.57142857142857140.621.0
boa-imperator0.60.52941176470588240.562517.0
bothriechis-schlegelii0.88888888888888880.76190476190476190.820512820512820521.0
bothrops-asper0.8750.53846153846153840.666666666666666713.0
carphophis-amoenus0.65517241379310340.65517241379310340.655172413793103429.0
charina-bottae0.89473684210526320.73913043478260860.809523809523809523.0
coluber-constrictor0.56896551724137930.5593220338983050.564102564102564159.0
contia-tenuis0.520.61904761904761910.565217391304347821.0
coronella-austriaca0.33333333333333330.10.1538461538461538310.0
crotalus-adamanteus0.61538461538461540.72727272727272730.666666666666666711.0
crotalus-atrox0.77472527472527470.83928571428571430.8057142857142857168.0
crotalus-cerastes0.78571428571428570.84615384615384610.814814814814814813.0
crotalus-horridus0.77419354838709680.81355932203389840.793388429752066259.0
crotalus-molossus0.63636363636363640.70.666666666666666610.0
crotalus-oreganus0.53333333333333330.66666666666666660.592592592592592612.0
crotalus-ornatus1.00.81818181818181820.911.0
crotalus-pyrrhus0.85714285714285710.54545454545454540.666666666666666522.0
crotalus-ruber0.78947368421052630.71428571428571430.750000000000000121.0
crotalus-scutulatus0.86206896551724130.75757575757575760.806451612903225833.0
crotalus-viridis0.53846153846153840.63636363636363640.583333333333333422.0
diadophis-punctatus0.82666666666666670.79487179487179480.810457516339869378.0
epicrates-cenchria1.00.50.66666666666666662.0
haldea-striatula0.54545454545454540.470588235294117640.505263157894736751.0
heterodon-nasicus0.70.53846153846153840.60869565217391313.0
heterodon-platirhinos0.70588235294117650.750.727272727272727248.0
hierophis-viridiflavus0.50.43750.466666666666666716.0
hypsiglena-jani0.52380952380952380.61111111111111120.564102564102564218.0
lampropeltis-californiae0.87012987012987010.81707317073170730.842767295597484282.0
lampropeltis-getula0.83333333333333340.6250.714285714285714316.0
lampropeltis-holbrooki0.69230769230769230.64285714285714290.666666666666666614.0
lampropeltis-triangulum0.81666666666666670.83050847457627120.823529411764705859.0
lichanura-trivirgata0.93333333333333330.82352941176470580.874999999999999917.0
masticophis-flagellum0.52941176470588240.64285714285714290.580645161290322642.0
micrurus-tener1.00.950.974358974358974320.0
morelia-spilota0.40.22222222222222220.28571428571428579.0
naja-naja0.83333333333333340.71428571428571430.76923076923076927.0
natrix-maura0.250.1250.166666666666666668.0
natrix-natrix0.78571428571428570.40740740740740740.536585365853658527.0
natrix-tessellata0.50.363636363636363650.421052631578947411.0
nerodia-cyclopion0.58333333333333340.46666666666666670.518518518518518615.0
nerodia-erythrogaster0.531250.43589743589743590.4788732394366197578.0
nerodia-fasciata0.61904761904761910.433333333333333350.509803921568627430.0
nerodia-rhombifer0.72131147540983610.65671641791044780.687567.0
nerodia-sipedon0.50847457627118640.53097345132743370.5194805194805195113.0
nerodia-taxispilota0.84615384615384610.550.666666666666666720.0
opheodrys-aestivus0.90425531914893620.94444444444444440.923913043478260990.0
opheodrys-vernalis0.82352941176470580.66666666666666660.736842105263157721.0
pantherophis-alleghaniensis0.365853658536585360.40.3821656050955413775.0
pantherophis-emoryi0.57142857142857140.66666666666666660.615384615384615336.0
pantherophis-guttatus0.91836734693877550.80357142857142860.857142857142857156.0
pantherophis-obsoletus0.53953488372093030.60416666666666660.5700245700245701192.0
pantherophis-spiloides0.434782608695652160.250.317460317460317540.0
pantherophis-vulpinus0.59574468085106380.84848484848484850.733.0
phyllorhynchus-decurtatus0.69565217391304350.94117647058823530.799999999999999917.0
pituophis-catenifer0.70731707317073170.70161290322580650.7044534412955465124.0
pseudechis-porphyriacus0.66666666666666660.28571428571428570.47.0
python-bivittatus0.8750.77777777777777780.8235294117647069.0
python-regius0.00.00.03.0
regina-septemvittata0.560.66666666666666660.608695652173913121.0
rena-dulcis0.66666666666666660.80.727272727272727210.0
rhinocheilus-lecontei0.82926829268292680.850.839506172839506140.0
sistrurus-catenatus1.00.80.8888888888888895.0
sistrurus-miliarius1.00.66666666666666660.86.0
sonora-semiannulata0.33333333333333330.40.36363636363636365.0
storeria-dekayi0.760.84653465346534650.8009367681498829202.0
storeria-occipitomaculata0.6315789473684210.64864864864864870.6437.0
tantilla-gracilis0.54545454545454540.60.571428571428571310.0
thamnophis-cyrtopsis0.57142857142857140.44444444444444440.59.0
thamnophis-elegans0.470588235294117640.29629629629629630.363636363636363627.0
thamnophis-hammondii0.55555555555555560.357142857142857150.4347826086956521614.0
thamnophis-marcianus0.87878787878787880.82857142857142860.852941176470588335.0
thamnophis-ordinoides0.3750.166666666666666660.2307692307692307818.0
thamnophis-proximus0.75438596491228070.74137931034482760.747826086956521958.0
thamnophis-radix0.81818181818181820.473684210526315760.638.0
thamnophis-sirtalis0.71670702179176760.8969696969696970.7967698519515478330.0
tropidoclonion-lineatum0.77777777777777780.63636363636363640.700000000000000111.0
vermicella-annulata0.00.00.03.0
vipera-aspis0.40.57142857142857140.470588235294117647.0
vipera-berus0.750.750.7512.0
virginia-valeriae0.20.142857142857142850.166666666666666667.0
xenodon-rabdocephalus1.01.01.08.0
zamenis-longissimus0.40.33333333333333330.363636363636363612.0
accuracy0.69306625577812020.69306625577812020.69306625577812020.6930662557781202
macro avg0.66594305972724020.60509484603857940.62476194460390163245.0
weighted avg0.69256349797344350.69306625577812020.68668740838650393245.0

Despite a skewed dataset, the majority of snakes had a similar precision and recall score.

Number of Samples vs F1 score

Samples vs F1.png The above graph of the number of samples the neural network was trained over and F1 score per class indicates two significant points:

  • Having more samples of a class will increase the probability of a model robustly identifying the snake species correctly
  • However, this does not mean that an inability to collect a large number of images for all classes will create a major imbalance in a model's predictions

The latter takeaway shows that LDAM loss has been largely successful! On a side note though, when a class has few samples, the F1 score may not be completely representative of how the model will generalise. This is primarily because, in a small number of images, only a small number of conditions can be shown and tested. Yet, in reality, an image can be taken in any environment and transformed in a very large number of ways.

Confusion Matrix

Confusion Matrix.png

The test dataset had too few images to clearly judge anything from the confusion matrix. This is due to a combination of having 85 classes, and classes with few samples being quite dark.

Improvements

If this model was going to be retrained, a good idea would be to use a 90-5-5, or better a 80-10-10 split. This would ensure that there would be enough data in the testing dataset to judge where exactly the model's confusion stems from (using a confusion matrix).

Combining the current classification model with an additional preprocessing pipeline which includes segmentation may also allow far higher F1 scores. This is because segmentation is able to remove extraneous noise around a snake (subtle environmental cues which prevent the model from generalising). Additionally, this would completely avoid the chance of a snake being cropped out of an image (currently unlikely but still possible for small snakes).