TheCacophonyProject / classifier-pipeline

Exports tracked animals through thermal vision.
GNU General Public License v3.0
21 stars 14 forks source link
cptv-files machine-learning server

Overview

These scripts handle the data pre-processing, training, and execution of a Convolutional Neural Network based classifier for thermal vision.

The output is a TensorFlow model that can identify thermal video clips of animals

Scripts

build.py

Creates training, validation and testing datasets from database of clips & tracks. Datasets contain frames or segments ( e.g. 45 frames).

Frames (important frames) are calculated by choosing frames with a mass ( the count of pixels that have been deemed an object by track extraction) between the lower and upper quartiles of a tracks mass distribution.

Segments are calculated by choosing segment duration consecutive frames whose mass is above a certain amount

Datasets are split by camera and location ( to try and remove any bias that may occur from using a camera in multiple sets).

Some labels have low amounts of data so they are split by clip_id i.e. wallaby and penguin

train.py

Trains a neural net using a provided test / train / validation dataset.

extract.py

Extract tracking information for a specified CPTV file

classify.py

Uses a pre-trained model to identifying and classifying any animals in supplied CPTV file, CPTV file must have associated metadata generated from extract. Classifier will produce a JSON output either to terminal or txt file.

thumbnail algorithm

A single region to be used as the thumbnail will be chosen per recording and saved in the JSON output

If a recording has tracks

If there are no tracks for a recording

Choose the region with greatest mass if any regions exist (these are points of interest that never eventuated into tracks)

Otherwise take the frame with the highest mean pixel value and find the highest mean pixel 64 x64 region

modelevaluate.py

Evaluates the performance of a model

Setup

Install the following prerequisites (tested with Ubuntu 18.0 and Python Python 3.6.9) apt-get install -y tzdata git python3 python3-dev python3-venv libcairo2-dev build-essential libgirepository1.0-dev libjpeg-dev python-cairo libhdf5-dev

  1. Create a virtual environment in python3 and install the necessary prerequisites
    pip install -r requirements.txt

  2. Copy the classifier_Template.yaml to classifier.yaml and then edit this file with your own settings. You will need to set up the paths for it work on your system. (Note: Currently these settings only apply to classify.py and extract.py)

  3. Optionally install GPU support for tensorflow (note this requires additional setup)
    pip install tensorflow-gpu

  4. MPEG4 output requires FFMPEG to be installed which can be found here On linux apt-get install ffmpeg. On windows the installation path will need to be added to the system path.

  5. Create a classifier configuration

Copy classifier_TEMPLATE.yaml to classifier.yaml. Edit.

Usage

Downloading the Dataset

CPTV files can be downloaded using the cptv-downloader tool.

Training a Model

First download the CPTV files by running

python cptv-download.py <dir> <user> <password>

Now we can build the data set. Move to the src directory.

python build.py <dir> --ext ".cptv"

And train the model

python train.py <build name>

This will build a model under the default parameters which reflect the production model

Preparing model for use

Once you have trained a model use tfliteconverter script to export the model for inference.

python3 tfliteconverter.py -f <path to store model> -e -m <model_path> -w

This will export the model to the supplied path

Tar this folder

tar czf <datetime and model type>.tar -C <frozen model path> .

Make a release https://github.com/TheCacophonyProject/AI-Model/releases and attach the tar file as an asset.

If this is a server side prediction model use the prefix server- in your release name. If this is a pi model use the prefix pi- in your release name

Database format

Load.py will create a hdf5 file (dataset.hdf5) The format of this is described here: https://docs.google.com/document/d/1iPsp-LWRva8YTQHwXRq8R1vjJKT58wRNCn_8bi6BoW8/

Classifying animals within a CPTV File

A pre-trained model can be used to classify objects within a CPTV video python extract.py [cptv filename] python classify.py [cptv filename]

This will generate a text file listing the animals identified, and create an MPEG preview file.

Classification and Training Images

Single frame models use 48 x 48 frames to classify/train

picture alt

Multi frame models use:

picture alt

Release and Update

Releasing prcoessing changes

  1. Create a release on GitHub (https://github.com/TheCacophonyProject/classifier-pipeline)

  2. SSH into processing server

  3. By default processing will use the latest pipeline release. If you have changed these settings, make sure that in the config file ( Default location is /etc/cacophony/processing.yaml ) the key classify_image references the release version you just made

Release pi tracking code

  1. In order to built a release for pi update the version in pyproject.toml

  2. Then you need to merge the changes into the pi-classifier branch. This will automatically create a release here https://pypi.org/project/classifier-pipeline/

  3. This can then be installed on via pip

Testing Classification and Tracking

Generating Tests

python generatetests.py out_dir Username Password <list of recording ids separated by a space>

e.g.

python generatetests.py test_clips Derek password123 12554 122232

Running Tests

Tracking results

Results for tests/tracking-tests.yml on tracking algorithms are located here https://drive.google.com/drive/u/1/folders/1uGU9FhKaypadUVcIvItBZuZebZa_Z7MG

IR Videos

TF Lite Conversion

Neural Compute Conversion

PYPI

Lila Dataset

  1. Download the data set Lila Dataset Download

  2. Download the suggested split Dataset split

  3. Unzip the contents of both files

  4. Clone this repository

  5. Make a python3 virtual environment if youd like and tnstall requirements pip install -r requirements.txt

  6. go into source dir cd src

  7. Build the dataset into tf records python3 build.py --split-file <PATH TO DATASET SPLIT> `

  8. Train the model python3 train.py <training name>