Skip to content

alexvmt/TeraiNet

Repository files navigation

TeraiNet

This repository contains scripts and notebooks to build a species classification model focussed on the Terai region in Nepal, namely TeraiNet. It can classify 10 different classes (including tigers) in camera trap images, using ML (MegaDetector and EfficientNetV2M), open data (LILA BC), open source tools (MEWC) and free compute resources (Google Colab and Kaggle).

TeraiNet is supposed to be an open source blueprint to enable others to easily and flexibly build their own species classifiers for their own specific use cases. All you need is basically a Google and a Kaggle account, and an Internet connection. In a nutshell, you select the species you're interested in and run the rest of the pipeline with some (hopefully) minor tweaks here and there.

Feel free to reach out if you have feedback/ideas for new use cases or would like to contribute/collaborate. Join AI for Conservation Slack and WILDLABS if you're interested in using technology for conservation.

tiger

Credentials: LILA BC, MegaDetector, own illustration.

Motivation and relevance

  • tigers are an endangered species, NGOs like the Nepal Tiger Trust and WWF Nepal protect them
  • there is no open and easy way for ecologists/NGOs to classify their camera trap images with regard to tigers
  • ML and open data/tools can help reduce the amount of manual labor when sifting through large amounts of camera trap images, looking for the needle in the haystack
  • there appears to be no such species classification model focussed on the Terai region in Nepal yet
  • goal: train a species classifier focussing on the most relevant species in the Bengal tiger's ecosystem in Nepal, namely the Terai region, and make it openly available through AddaxAI (formerly known as EcoAssist)

Data preparation

To start, either open the notebooks (step 1-3) in order from here or first clone the repo in your Google Drive. For step 4, just open the notebook in Kaggle directly by clicking the respective button below.

1. Select data sources

2. Sample images

Open In Colab

  • Get image URLs from LILA BC
  • Sample desired amount of images per class
  • Create train test split if applicable

Classes

  1. Tiger
  2. Leopard
  3. Asian black bear, American black bear (not enough images of Asian black bear alone)
  4. Other carnivores, including dhole, black-backed jackal, gray fox, leopard cat, mainland leopard cat, marbled cat, Asian golden cat (including substitutes, i.e., black-backed jackal and gray fox)
  5. Deer
  6. Wild boar
  7. African buffalo, Cape buffalo (substitute for gaur)
  8. White rhinoceros (substitute for Indian rhino)
  9. Asian elephant, African bush elephant (not enough images of Asian elephant alone)
  10. Bird

Note: Due to a lack of images for certain species some fairly heavy compromises had to be taken. It remains to be seen how well a model trained with such compromises generalizes to the target region.

3. Download images

  • LILA BC Open In Colab
  • amur tiger re-identification challenge Open In Colab

Note: Since free Colab and Drive have limited storage capacities, one might have to download images for one species at a time (and free up space before proceeding to the next).

Note: I found the image downloading to be much faster in Colab and Drive compared to Kaggle.

4. Preprocess images

Open In Kaggle

  1. Run MegaDetector on all images
  2. Snip images following mewc-snip
  3. Copy snipped images to Kaggle Output

Note: Images must have been previously downloaded to Drive/local machine via Colab and then uploaded to Kaggle as a dataset (zipped folder).

Note: I found access to free GPUs much better and transparent in Kaggle compared to Colab.

Model training and evaluation

To train and evaluate a model, open the notebook in Kaggle directly by clicking the respective button below.

Open In Kaggle

  1. Use Keras Image Models
  2. Follow mewc-flow and mewc-train
  3. Log experiments using Weights & Biases

I selected a pre-trained EfficientNetV2M with 54M parameters because it constitutes a good compromise between predictive performance, training time and model size. The model has been trained for 35 epochs (with early stopping) with about 2,000 images per class (250 images per class in the validation set). The model has been evaluated on a separate test set with 250 images per class.

Note: There are only ~300 tiger images on LILA BC. I didn't use them in training and validation but instead put all of them in test2 to examine how the model would potentially generalize to tiger camera trap images from another source than the tiger training images (like it would be the case with the Nepal Tiger Trust using the model on their own images through AddaxAI, for example).

Note: As an extension to the underlying paper, to reduce label ambiguity in the data, images were removed if containing more than one animal detection. This was done to address cases where the image-level label did not reliably correspond to the cropped animal region, such as images containing multiple species (e.g., a bird sitting on an elephant) or incorrect crop assignments caused by generic animal detection. Although this filtering reduced the overall amount of data, it substantially improved label consistency and reduced structured supervision noise. The resulting model shows a clear performance increase, particularly for previously confusion-prone classes such as bird, rhino, elephant, and leopard, indicating that improved data quality outweighed the loss in dataset size. The main exception was the deer class, which showed a slight decline in recall and F1-score, likely because the filtering step disproportionately removed valid multi-individual or herd images that are common for deer in camera trap datasets.

Test set performance overview

  • Accuracy: 0.935
  • Precision: 0.936
  • Recall: 0.935
  • F1-Score: 0.935

Test set performance per class

Class Precision Recall F1-Score Support
tiger 1.000 0.992 0.996 250
leopard 0.955 0.932 0.943 250
black_bear 0.956 0.956 0.956 250
other_carnivores 0.894 0.940 0.916 250
deer 0.935 0.864 0.898 250
wild_boar 0.987 0.900 0.941 250
buffalo 0.910 0.968 0.938 250
rhino 0.894 0.912 0.903 250
elephant 0.908 0.952 0.930 250
bird 0.925 0.936 0.930 250
micro avg 0.935 0.935 0.935 2500
macro avg 0.936 0.935 0.935 2500
weighted avg 0.936 0.935 0.935 2500

Note: Test set performance for the tiger class is extremely high because the respective images are unrealistically good. The performance on the tiger images in test2 is more realistic (test2 accuracy: 0.895).

Test set confusion matrix

confusion_matrix

Note: The previously present confusion between certain classes is largely gone - at the slight expense of the performance on the deer class though.

Deployment

  1. Publish model on HuggingFace
  2. Deploy model to AddaxAI

There's also an inference example here to use the model directly.

Cite

Paper

@article{Merdian-Tarko2025,
  title = {TeraiNet: An open-source blueprint for the fast and flexible development of local species classification models},
  author = {Alexander V. Merdian-Tarko},
  workshop = {5th International Workshop on Camera Traps, AI, and Ecology},
  location = {Seatle, WA, U.S. & remote},
  year = {2025}
}

About

TeraiNet is a species classification model focused on the Terai region in Nepal to support Bengal tiger conservation

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors