Code Monkey home page Code Monkey logo

zamba's Introduction

Zamba

Docs Status tests codecov

monkey-vid.mp4

Zamba means "forest" in Lingala, a Bantu language spoken throughout the Democratic Republic of the Congo and the Republic of the Congo.

zamba is a tool built in Python that uses machine learning and computer vision to automatically detect and classify animals in camera trap videos. You can use zamba to:

  • Identify which species appear in each video
  • Filter out blank videos
  • Create your own custom models that identify your species in your habitats
  • Estimate the distance between animals in the frame and the camera
  • And more! ๐Ÿ™ˆ ๐Ÿ™‰ ๐Ÿ™Š

The official models in zamba can identify blank videos (where no animal is present) along with 32 species common to Africa and 11 species common to Europe. Users can also finetune models using their own labeled videos to then make predictions for new species and/or new ecologies.

zamba can be used both as a command-line tool and as a Python package. It is also available as a user-friendly website application, Zamba Cloud.

We encourage people to share their custom models trained with Zamba. If you train a model and want to make it available, please add it to the Model Zoo Wiki for others to be able to use!

Visit https://zamba.drivendata.org/docs/ for full documentation and tutorials.

Installing zamba

First, make sure you have the prerequisites installed:

  • Python 3.8 or 3.9
  • FFmpeg > 4.3

Then run:

pip install https://github.com/drivendataorg/zamba/releases/latest/download/zamba.tar.gz

See the Installation page of the documentation for details.

Getting started

Once you have zamba installed, some good starting points are:

Example usage

Once zamba is installed, you can see the basic command options with:

$ zamba --help

 Usage: zamba [OPTIONS] COMMAND [ARGS]...

 Zamba is a tool built in Python to automatically identify the species seen in camera trap
 videos from sites in Africa and Europe. Visit https://zamba.drivendata.org/docs for more
 in-depth documentation.

โ•ญโ”€ Options โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ --version                     Show zamba version and exit.                                โ”‚
โ”‚ --install-completion          Install completion for the current shell.                   โ”‚
โ”‚ --show-completion             Show completion for the current shell, to copy it or        โ”‚
โ”‚                               customize the installation.                                 โ”‚
โ”‚ --help                        Show this message and exit.                                 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
โ•ญโ”€ Commands โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ densepose      Run densepose algorithm on videos.                                         โ”‚
โ”‚ depth          Estimate animal distance at each second in the video.                      โ”‚
โ”‚ predict        Identify species in a video.                                               โ”‚
โ”‚ train          Train a model on your labeled data.                                        โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

zamba can be used "out of the box" to generate predictions or train a model using your own videos. zamba supports the same video formats as FFmpeg, which are listed here. Any videos that fail a set of FFmpeg checks will be skipped during inference or training.

Classifying unlabeled videos

$ zamba predict --data-dir path/to/videos

By default, predictions will be saved to zamba_predictions.csv. Run zamba predict --help to list all possible options to pass to predict.

See the Quickstart page or the user tutorial on classifying videos for more details.

Training a model

$ zamba train --data-dir path/to/videos --labels path_to_labels.csv --save_dir my_trained_model

The newly trained model will be saved to the specified save directory. The folder will contain a model checkpoint as well as training configuration, model hyperparameters, and validation and test metrics. Run zamba train --help to list all possible options to pass to train.

You can use your trained model on new videos by editing the train_configuration.yaml that is generated by zamba. Add a predict_config section to the yaml that points to the checkpoint file that is generated:

...
# generated train_config and video_loader_config
...

predict_config:
  checkpoint: PATH_TO_YOUR_CHECKPOINT_FILE

Now you can pass this configuration to the command line. See the Quickstart page or the user tutorial on training a model for more details.

You can then share your model with others by adding it to the Model Zoo Wiki.

Estimating distance between animals and the camera

$ zamba depth --data-dir path/to/videos

By default, predictions will be saved to depth_predictions.csv. Run zamba depth --help to list all possible options to pass to depth.

See the depth estimation page for more details.

Contributing

We would love your contributions of code fixes, new models, additional training data, docs revisions, and anything else you can bring to the project!

See the docs page on contributing to zamba for details.

zamba's People

Contributors

allendowney avatar caseyfitz avatar dependabot[bot] avatar drivendata avatar ejm714 avatar iamshankhadeep avatar jayqi avatar klwetstone avatar pdima avatar pjbull avatar r-b-g-b avatar tamara-glazer avatar wookietreiber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zamba's Issues

Updates to error handling

We need to update error handling with two items:

(1) Catch and skip videos that error so that a single problem does not tank the entire job.
(2) Save errors in a machine readable format (file, message, and traceback) so that it is consumable by zamba cloud.

Don't fail entire process on invalid videos

Currently, our video loading has a number of limitations:

  • Does not support passing a list of videos rather than an entire directory
  • Assumes everything in a folder is a video
  • Does not check file is a video before trying to load it

If a video or file is invalid, the entire process throws an error and ends. This is probably not desirable in the case where someone is batch processing a large number of videos.

Model: Refactoring where possible.

For example, it seems that second_stage.py and second_stage_nn.py share some function signatures such as loading data. Would it be possible to refactor here, and/or any other places?

download weights if not available

  • Upload zipped weights to PUBLIC s3 bucket - include mirror links for europe and asia
  • If not exists weight_dir: ask to download, download
    -- warn about size!
    -- unzip to weights dir
    -- goes in init of CnnEnsemble
    -- use requests, os.environ.get for weights url, default to ours
    -- zipfile, in standard lib

end to end with raw vids

  • no hard-coded data paths!
  • pull some, known classes, from s3
  • add test on single nano vid (to ship with package)

Unblock dev by using cifar classifier as toy model

The Problem

Now that we have simple functionality, we need to hone in on specific features. To do this efficiently, we need

  • a lightweight model pipeline
  • a pipeline similar enough to the primatrix classifier so that core functionality can be developed
  • a pipeline simple enough so that development can proceed at a reasonable pace

A Solution

Building a convolutional network to classify the cifar10 data fits the above criteria. The cifar 10 problem is to classify 32x32 pixel RBG images into one of 10 classes. It is a dataset often used in academics, but I think can be very useful for application development as well, at least in our case.

The idea is to use cifar10 as the context, and a simple convolutional net as the "default model" (eventually dmytro's) that we can use to develop the predict, train, and tune functionality. Since the inputs (images) and outputs (row of probs for one image) are similar, minimizing the code's bias towards cifar should be less complicated than if we keep using random number data and non-predictors as a development model.

The goal is to frame as many pri-martrix problems as we can as cifar problems. My claim is that there are a non-trivial amount of pri-matrix problems that allow this framing. For example,

  1. Handling real input directories, pre-processing their contents into temporary storage, and sending them into a model for prediction.
  2. Handling output that is similarly structured to the chimps output, i.e., csv files indexed by a filename with class probabilities as columns.
  3. Training, tuning, saving, and loading functionality for classifiers, not just tensorflow graphs (although that was a good start).
  4. Development of any** additional performance metrics**, such as loss over time, validation scoring, etc.

As the tensorflow advanced tutorial on cifar says,

The reason CIFAR-10 was selected was that it is complex enough to exercise much of TensorFlow's ability to scale to large models. At the same time, the model is small enough to train fast, which is ideal for trying out new ideas and experimenting with new techniques.

In short, this approach seems to me a good solution to asynchronously working on the codebase with dmytro before his models are ready for integration.

Advantages

  • The above-referenced tutorial comes with a lot of code that can be edited and used to speed up development of our project, such as network architecture, training criteria, data-fetching scripts, pre-processing tasks, and various other "advanced" features that we can consciously decide to develop or not as we work on the tool.
  • The fact that inputs to the dmytro model are images and these data are images will allow us to develop a lot of the preprocessing pipeline (except the parts that convert the videos into images).
  • We can experiment with some of the ensembling that we'll eventually need to handle, such as using tensorflow and xgboost to generate predictions.

Disadvantages

  • Will inevitably have to deal with some idiosyncrasies of the cifar10 data, but given its simplicity and the fact that we have lots of starter code for manipulating it, this shouldn't set us back too much.

Timeline

Since the code skeleton is now fairly operational, I think it's reasonable to shoot for integrating this by the end of the week. End of first rev at the latest.

Consider implementing MegaDetector for images

i.e. if you have images, weโ€™ll run MegaDetector instead and give you results
could also consider other ways to further implement MegaDetector for videos as part of Zamba backend

improve tests

Convert testing configs to @pytest.fixtures. Remove the temporary tf testing of the workflow.

Implement ModelManager object to mediate loading, configuration, and logic of model calls.

Make cli a thin wrapper around model object. cli should be focused on user io only. Use ModelManager object to mediate loading, configuration, and logic of model calls.

Eg,

class Model(object):
    def __init__(self):
        pass




class ModelManager(object):
    def __init__(self, modelpath, tempdir=None, predict_thresh=None):
        self.model = load_model(modelpath)
        self.predict_thresh = predict_thresh

    def predict(self, datapath, outputpath):
        data = load_data(datapath)

        if self.predict_thresh:
            preds = self.model.predict(data) >= self.predict_thresh
        else:
            preds = self.model.predict(data)

        preds.to_csv(outputpath)

    def train(self):
        pass

    def tune(self):
        pass

Write getting started

  • for weights in PUBLIC s3 bucket - include mirror links for europe and asia

  • ffmpeg: include overview of commands, 15 sec resize example

Tests Failing on master branch

I setup an automated test run with Codeship so that we have continuous integration. Tests on master are currently failing.

(@caseyfitz I know there's WIP to update those, so this is just to track that work.)

Use config.py imports instead of Path objects where possible

The file zamba.models.cnnensemble.src.config.py sets a lot of paths. Yet many files such as zamba.models.cnnensemble_model.py import Path and config but use Path objects local to the file instead of config.

Use config where possible throughout the cnnensemble model.

Update docs for sphinx

Do this after initial ModelManager is merged. Doc everything that exists so far.

This includes updating README.md post djamba.

Add Zamba Cloud to Zamba docs

https://zamba.drivendata.org/docs/

Options for Running zamba could include Zamba Cloud web application (available for conservation researchers upon request) with a link to zambacloud.com

@pjbull I had a note on this from when this has come up a couple times in the past. I think it's worth making explicit but don't feel strongly. Assigning to you for now, feel free to reassign/remove

Model: Remove unused code

We're not looking for full coverage, but there is a lot of commented out code throughout the src directory

If we are processing one video that ffmpeg cannot load, we fail with cryptic error.

We should have a better message for users rather than failing in this case:

(1) Ask zamba to run on 1 file that is not a valid video
(2) zamba will say "skipping

Logs look like:

2019-01-11 02:05:00,757 - root - INFO - making predictions17
Predicting on 1 L1 models:   0%|          | 0/1 [00:00<?, ?it/s]18
Processing 1 videos:   0%|          | 0/1 [00:00<?, ?it/s]๏ฟฝ[A19
Processing 1 videos: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1/1 [00:07<00:00,  7.82s/it]๏ฟฝ[A20๏ฟฝ[A
Predicting on 1 L1 models: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1/1 [00:38<00:00, 38.37s/it]21
Skipping file that is not a valid video:  /paperspace/videos/123d4d52-858c-49b2-82d1-ae8d490df66f_12090006.AVI
Traceback (most recent call last):
  File "script.py", line 30, in <module>
    manager.predict(str(download_dir), save=True, pred_path=output_path)
  File "/src/zamba/zamba/models/manager.py", line 95, in predict
    preds = self.model.predict(data)
  File "/src/zamba/zamba/models/cnnensemble_model.py", line 67, in predict
    l2_results = second_stage.predict(l1_results, profile=self.profile)
  File "/src/zamba/zamba/models/cnnensemble/src/second_stage.py", line 273, in predict
    xgboost_predictions = xgboost_model.predict(l1_model_results)
  File "/src/zamba/zamba/models/cnnensemble/src/second_stage.py", line 141, in predict
    return self._predict(X)
  File "/src/zamba/zamba/models/cnnensemble/src/second_stage.py", line 201, in _predict
    return self.model.predict_proba(X)
  File "/usr/local/lib/python3.6/dist-packages/xgboost/sklearn.py", line 575, in predict_proba
    ntree_limit=ntree_limit)
  File "/usr/local/lib/python3.6/dist-packages/xgboost/core.py", line 1050, in predict
    self._validate_features(data)
  File "/usr/local/lib/python3.6/dist-packages/xgboost/core.py", line 1308, in _validate_features
    data.feature_names))
ValueError: feature_names mismatch: ['f0', 'f1', 'f2 
...

re-train the first level models on the full dataset

In the original Cnnensemble implementation ensemble of L1 models have been used trained on 4 folds.

It provides very limited if any accuracy gain while 4x slower, it's better to re-train the models on the whole dataset, including test data.

Model: Replace shell calls with python wrappers.

We will be developing a simple command line tool entirely in python, so being able to call training, testing, predicting, and submission processes from within Python (rather than through bash scripts) makes that more reliable.

Name the project so we can replace `cmd` throughout

As mentioned in the README.md, cmd is a placeholder.

Usage at the command line will look like

cmd predict --datapath /path/to/my/new/cameratrap-footage

People will install the tool from PyPi using

pip install cmd

The issue will be resolved when we replace cmd with something... better ๐Ÿ˜‰

Suggestions so far (rest in comments):

  • cam2chimp
  • snapchimp
  • SpeciesSpotter
  • camtrap.ai
  • chimply

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.