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
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
Trains a neural net using a provided test / train / validation dataset.
Extract tracking information for a specified CPTV file
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.
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
An overriding tag based on the number of occurrences of an animal is chosen, with any animal being favoured over false-positive e.g. rat, possum, possum, false-positive, false-positive, false-positive.... Will be a possum tag
All tracks will be scored on prediction confidence, standard deviation of mass and mass.
Choose highest scoring track and take the region with the 75th percentile mass as the thumbnail region
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
Evaluates the performance of a model
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
Create a virtual environment in python3 and install the necessary prerequisites
pip install -r requirements.txt
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)
Optionally install GPU support for tensorflow (note this requires additional setup)
pip install tensorflow-gpu
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.
Create a classifier configuration
Copy classifier_TEMPLATE.yaml
to classifier.yaml
. Edit.
CPTV files can be downloaded using the cptv-downloader tool.
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
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-
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/
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.
Single frame models use 48 x 48 frames to classify/train
Multi frame models use:
Create a release on GitHub (https://github.com/TheCacophonyProject/classifier-pipeline)
SSH into processing server
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
In order to built a release for pi update the version in pyproject.toml
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/
This can then be installed on via pip
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
python trackingtest.py -t tests/tracking-tests.yml --user <User> --password <password>
Results for tests/tracking-tests.yml on tracking algorithms are located here https://drive.google.com/drive/u/1/folders/1uGU9FhKaypadUVcIvItBZuZebZa_Z7MG
python3 tfviewer.py <config.base_data_folder>/<config.tracks_folder>/training-data/<set to see train/vaidation/train>/*.tfrecord --overlay classification --image-key image/thermalencoded
python3 tfliteconvert.py -m <path to saved model> -w <weights to use> -c
Will save to python3 tfliteconvert.py -m <path to tf lite model>
-r`python3 tfliteconvert.py -m <path to saved model> -w <weights to use> -f
https://www.intel.com/content/www/us/en/developer/articles/guide/get-started-with-neural-compute-stick.html
python3 ~/intel/openvino_<VERSION>/deployment_tools/model_optimizer/mo_tf.py --saved_model_dir <Path to dir with weights saved to saved_model.pb> --input_shape [1,<width>,<height>,3]
pip install classifier-pipeline
.
This will install the executable pi_classify
which can be used to connect to leptond or an ir cameraDownload the data set Lila Dataset Download
Download the suggested split Dataset split
Unzip the contents of both files
Clone this repository
Make a python3 virtual environment if youd like and tnstall requirements pip install -r requirements.txt
go into source dir cd src
Build the dataset into tf records
python3 build.py --split-file <PATH TO DATASET SPLIT>
Train the model
python3 train.py <training name>