Skip to main content

Tools for classifying camera trap images

Project description

animl-py

AniML comprises a variety of machine learning tools for analyzing ecological data. This Python package includes a set of functions to classify subjects within camera trap field data and can handle both images and videos. This package is also available in R: animl

Table of Contents

  1. Installation
  2. Usage
  3. Models

Installation Instructions

It is recommended that you set up a conda environment for using animl. See Dependencies below for more detail. You will have to activate the conda environment first each time you want to run AniML from a new terminal.

From GitHub

git clone https://github.com/conservationtechlab/animl-py.git
cd animl-py
pip install -e .

From PyPi

pip install animl

Dependencies

We recommend running AniML on GPU-enabled hardware. **If using an NVIDIA GPU, ensure driviers, cuda-toolkit and cudnn are installed.

Python >= 3.9

Onnx
Animl currently depends on onnxruntime-gpu >= 1.23.2.

Python Package Dependencies

  • numpy>=2.0.2
  • onnxruntime-gpu>=1.23.2
  • pandas>=2.2.2
  • pillow>=11.0.0
  • opencv-python>=4.12.0.88
  • tqdm>=4.66.5

Verify Install

We recommend you download the examples folder within this repository. Download and unarchive the zip folder. Then with the conda environment active:

python -m animl /path/to/example/folder

This should create an Animl-Directory subfolder within the example folder.

Or, if using your own data/models, animl can be given the paths to those files: Download and unarchive the zip folder. Then with the conda environment active:

python -m animl /example/folder --detector /path/to/megadetector --classifier /path/to/classifier --classlist /path/to/classlist.txt

You can use animl in this fashion on any image directory.

Finally you can use the animl.yml config file to specify parameters:

python -m animl /path/to/animl.yml

Usage

Inference

The functionality of animl can be parcelated into its individual functions to suit your data and scripting needs. The sandbox.ipynb notebook has all of these steps available for further exploration.

  1. It is recommended that you use the animl working directory for storing intermediate steps.
import animl
workingdir = animl.WorkingDirectory('/path/to/save/data')
  1. Build the file manifest of your given directory. This will find both images and videos.
files = animl.build_file_manifest('/path/to/images', out_file=workingdir.filemanifest, exif=True)
  1. If there are videos, extract individual frames for processing. Select either the number of frames or fps using the argumments. The other option can be set to None or removed.
allframes = animl.extract_frames(files, out_dir=workingdir.vidfdir, out_file=workingdir.imageframes,
                                 parallel=True, frames=3, fps=None)
  1. Pass all images into MegaDetector. We recommend MDv5a. The function parse_MD will convert the json to a pandas DataFrame and merge detections with the original file manifest, if provided.
detector = animl.load_detector('/path/to/mdmodel.pt', model_type="MDV5", device='cuda:0')
mdresults = animl.detect(detector, allframes, resize_width=animl.MEGADETECTORv5_SIZE, resize_height=animl.MEGADETECTORv5_SIZE, 
                         letterbox=True, file_col="frame", checkpoint_path=working_dir.mdraw, quiet=True)
detections = animl.parse_detections(mdresults, manifest=all_frames, out_file=workingdir.detections)
  1. For speed and efficiency, extract the empty/human/vehicle detections before classification.
animals = animl.get_animals(detections)
empty = animl.get_empty(detections)
  1. Classify using the appropriate species model. Merge the output with the rest of the detections if desired.
class_list = animl.load_class_list('/path/to/classlist.txt')
classifier = animl.load_classifier('/path/to/model', len(class_list), device='cuda:0')
raw_predictions = animl.classify(classifier, animals, resize_width=480, resize_height=480, 
                                 file_col="frame", batch_size=4, out_file=working_dir.predictions)
  1. Apply labels from class list with or without utilizing timestamp-based sequences.
manifest = animl.single_classification(animals, empty, raw_predictions, class_list['class'])

or

manifest = animl.sequence_classification(animals, empty, 
                                         raw_predictions,
                                         class_list['class'],
                                         station_col='station',
                                         empty_class="",
                                         sort_columns=None,
                                         file_col="filepath",
                                         maxdiff=60)
  1. (OPTIONAL) Save the Pandas DataFrame's required columns to csv and then use it to create json for TimeLapse compatibility
csv_loc = animl.export_timelapse(animals, empty, imagedir, only_animal = True)
animl.export_megadetector(csv_loc, imagedir + "final_result.json")
  1. (OPTIONAL) Create symlinks within a given directory for file browser access.
manifest = animl.export_folders(manifest, out_dir=working_dir.linkdir, out_file=working_dir.results)

Training

Training workflows are still under development. Please submit Issues as you come upon them.

  1. Assuming a file manifest of training data with species labels, first split the data into training, validation and test splits. This function splits each label proportionally by the given percentages, by default 0.7 training, 0.2 validation, 0.1 Test.
train, val, test, stats = animl.train_val_test(manifest, out_dir='path/to/save/data/', label_col="species",
                                               percentage=(0.7, 0.2, 0.1), seed=None)
  1. Set up training configuration file. Specify the paths to the data splits from the previous step. See config README

  2. (Optional) Update train.py to include MLOPS connection.

  3. Using the config file, begin training

python -m animl.train --config /path/to/config.yaml

Every 10 epochs (or define custom 'checkpoint_frequency'), the model will be checkpointed to the 'experiment_folder' parameter in the config file, and will contain performance metrics for selection.

  1. Testing of a model checkpoint can be done with the "test.py" module. Add an 'active_model' parameter to the config file that contains the path of the checkpoint to test. This will produce a confusion matrix of the test dataset as well as a csv containing predicted and ground truth labels for each image.
python -m animl.test --config /path/to/config.yaml

Models

The Conservation Technology Lab has several models available for use. You can use the download function within animl or access them here:

animl.download_model(animl.CLASSIFIER['SDZWA_Andes_v1'],  out_dir: str = 'models/')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

animl_lite-3.1.0.tar.gz (38.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

animl_lite-3.1.0-py3-none-any.whl (42.1 kB view details)

Uploaded Python 3

File details

Details for the file animl_lite-3.1.0.tar.gz.

File metadata

  • Download URL: animl_lite-3.1.0.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for animl_lite-3.1.0.tar.gz
Algorithm Hash digest
SHA256 49fc65d759f3271e8bc691e72595ccbcd6b23f448792f81cf692fc22efda8e3b
MD5 4447a94175b0159c5bb6e266f0969413
BLAKE2b-256 1e98f3cfe4553d039616c6178db3bd58c8b4382517eae06bfe5681488e34a45c

See more details on using hashes here.

File details

Details for the file animl_lite-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: animl_lite-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for animl_lite-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ccdfba3bffe18c6f6135f03f6262b4f1fa06b9d27e24e84c3e96251a90aa18a
MD5 9eab82b78f5d62d9914555f6f7820a59
BLAKE2b-256 e48b7553cb428a4cb8b49a3ec4bb475bde406fee5a69c54ee82e2418869504b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page