Giter Club home page Giter Club logo

mlops_videoanomalydetection's Introduction

Video Anomaly Detection - with Azure ML and MLOps

Build Status

The automation of detecting anomalous event sequences in videos is a challenging problem, but also has broad applications across industry verticals.

The approach followed in this repository involves self-supervised training deep neural networks to develop an in-depth understanding of the physical and causal rules in the observed scenes. The model effectively learns to predict future frames in the video in a self-supervised fashion.

The trained model can then be used to detect anomalies in videos. As the model tries to predict each next frame, one can calculate the error in the model's prediction. If the error is large, it is likely that an anomalous even occurred.

The approach can be used both in a supervised and unsupervised fashion, thus enabling the detection of pre-defined anomalies, but also of anomalous events that have never occurred in the past.

Post on LinkedIn (includes video)

Example scenarios

The following snapshots are also links to video demonstrations.

Unsupervised Approach:

Video of unsupervised anomaly detection

Supervised Approach:

Video of unsupervised anomaly detection

Learning Goals

You will learn:

  1. How to adapt an existing neural network architecture to your use-case.
  2. How to prepare video data for deep learning.
  3. How to perform hyperparameter tuning with HyperDrive to improve the performance of your model.
  4. How to deploy a deep neural network as a webservice for video processing.
  5. How to post-process the output of a Keras model for secondary tasks (here, anomaly detection).
  6. How to define a build pipeline for DevOps.

Pre-requisites

Skills

  1. Some familiarity with concepts and frameworks for neural networks:
  2. Knowledge of basic data science and machine learning concepts. Here and here you'll find short introductory material.
  3. Moderate skills in coding with Python and machine learning using Python. A good place to start is here.

Software Dependencies

  • Various python modules. We recommend working with a conda environement (see config/environment.yml and Documentation).
    • We recommend you begin by installing Miniconda.
    • Also consider running the following command to update you conda base image: conda update -n base -c defaults conda
    • Then, you could run: conda env create -f config/environment.yml
  • If you are using a DSVM:

Hardware Dependencies

A computer with a GPU, for example a Linux Azure VM. Compare VM sizes and prices). If you don't know what to choose, we recommend the Standard NC6, the most affordable VM with a GPU.

You could create a VM in the Azure Portal.

Dataset

UCSD Anomaly Detection Dataset

Agenda

The recommended first step is to clone this repository.

Getting Started

  1. Data Preparation - Download and prepare data for training/testing.
  2. Azure ML Configuration - Configure your Azure ML workspace.
  3. AML Pipelines - Automate data preparation, training, and re-training.
  4. Deployment - How to deploy your anomaly detection as a webservice on AKS.

Deep-dive

  1. Model Development - Understand model architecture and training.
  2. Fine Tuning - Perform transfer learning with pretrained model onnew data.
  3. Hyperparameter tuning - Tune hyperparameters with HyperDrive.
  4. Anomaly Detection - Use Model errors for detecting anomalies.

Contribute

We invite contributions to this repository. The preferred method would be to fork this repository and to create a pull request.

References / Resources

Troubleshooting

Missing cuda libraries

We noticed that tensorflow might be looking for the wrong the of cudatoolkit.

You might see the following error:

Could not load dynamic library 'libcudart.so.10.0'; dlerror: libcudart.so.10.0: cannot open shared object file: No such file or directory

We found that it helps to look at which version of these libraries you have installed, and pin the version of cudatoolkit to the version that tensorflow is looking for. E.g. above, TF is looking for 10.0, so we pinned the version of the cudatoolkit to 10.0 in config/environment.yml.

mlops_videoanomalydetection's People

Contributors

andrejpk avatar jpe316 avatar microsoft-github-policy-service[bot] avatar microsoftopensource avatar msftgits 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mlops_videoanomalydetection's Issues

“Keras requires TensorFlow 2.2 or higher”!!

The config.yml pins tensorflow==1.15. However no Keras version is pinned. Thus, after execution of conda env create -f config/environment.yml latest version of Keras (i.e. 2.4.3) gets installed.
While executing train.py locally, it throws Error “Keras requires TensorFlow 2.2 or higher”
I think pinning appropriate version of keras would help. What it is?

trying to use the project

dear all,

I was trying your project but I have found some issues, I was following your steps and this is what I have found:

  1. I downloaded the UCS dataset and executed the data_prepraration.py file (note that in your documents the name of the file is shorter. It seems to be executed ok, but the files are not in the right folder, see next section.
    2 as said the documents created in the point 1 are placed in a different place (it puts it into a folder, I had to move them to the root of data folder.
    in addition there is a typo in the file prednet.py, it puts .BACKEND and it should be lowercase
    and at the end I got an error that I put below:

/keras_preprocessing/image/iterator.py line 127 in _get_batches_of_transformed_samples raise NotImplementedError

keras is version 2.3.1

I hope that you can help me to fix this issue and continue with the testing.

regards.

bug in train.py

dear all,

I have found a bug in the code, in train.py, in line 294, you call the function run without taking into account if the training is done in remote

_# after training is complete, we log the final loss on the validation set
run.log('final_val_loss', history.history['val_loss'][-1])_

it should be

_# after training is complete, we log the final loss on the validation set
if remote_execution:
      run.log('final_val_loss', history.history['val_loss'][-1])_

trying to build provided Dockerfile results with a failure

C:\Users\gubert\Repos\MLOps_VideoAnomalyDetection\config>docker build -f Dockerfile -t wopauli_1.8-gpu:1 .
Sending build context to Docker daemon  8.704kB
Step 1/2 : FROM mcr.microsoft.com/azureml/base-gpu:intelmpi2018.3-cuda9.0-cudnn7-ubuntu16.04
intelmpi2018.3-cuda9.0-cudnn7-ubuntu16.04: Pulling from azureml/base-gpu
7b722c1070cd: Pull complete                                                                                                                                                5fbf74db61f1: Pull complete                                                                                                                                                ed41cb72e5c9: Pull complete                                                                                                                                                7ea47a67709e: Pull complete                                                                                                                                                35400734fa04: Pull complete                                                                                                                                                195acf8a5739: Pull complete                                                                                                                                                127028f911f6: Pull complete                                                                                                                                                84588368cc86: Pull complete                                                                                                                                                decbf3005a1c: Pull complete                                                                                                                                                249412ff35c9: Pull complete                                                                                                                                                3f601dfda46c: Pull complete                                                                                                                                                d481228abde9: Pull complete                                                                                                                                                38567447e6f3: Pull complete                                                                                                                                                1c5715cbc27e: Pull complete                                                                                                                                                9fdb00ca4b90: Pull complete                                                                                                                                                Digest: sha256:fd6c26ca1c5e8aefce47850ebaaea8ae58f2b1516b4530a75fff9d48ffd3a2bb
Status: Downloaded newer image for mcr.microsoft.com/azureml/base-gpu:intelmpi2018.3-cuda9.0-cudnn7-ubuntu16.04
 ---> c0ba45f719a0
Step 2/2 : RUN ldconfig /usr/local/cuda/lib64/stubs &&     conda install -y python=3.6.2 && conda clean -ay &&     pip install --no-cache-dir azureml-defaults &&     pip install --no-cache-dir tensorflow==1.8.0 tensorflow-gpu==1.8.0 keras==2.0.8 matplotlib==3.0.3 seaborn==0.9.0 requests==2.21.0 bs4==0.0.1 imageio==2.5.0 sklearn pandas==0.24.2 numpy==1.16.2 hickle==3.4.3 &&     pip install --no-cache-dir horovod==0.13.5 &&     ldconfig
 ---> Running in 70f2deb6967a
Solving environment: ...working... done


==> WARNING: A newer version of conda exists. <==
  current version: 4.5.11
  latest version: 4.7.5

Please update conda by running

    $ conda update -n base -c defaults conda


ca-certificates-2019 | 133 KB    | ########## | 100%
zlib-1.2.11          | 120 KB    | ########## | 100%
libedit-3.1          | 171 KB    | ########## | 100%
openssl-1.0.2s       | 3.1 MB    | ########## | 100%
setuptools-41.0.1    | 656 KB    | ########## | 100%
sqlite-3.23.1        | 1.5 MB    | ########## | 100%
wheel-0.33.4         | 40 KB     | ########## | 100%
certifi-2019.6.16    | 154 KB    | ########## | 100%
pip-19.1.1           | 1.9 MB    | ########## | 100%
_libgcc_mutex-0.1    | 3 KB      | ########## | 100%
readline-7.0         | 1.1 MB    | ########## | 100%
ncurses-6.0          | 920 KB    | ########## | 100%
python-3.6.2         | 27.0 MB   | ########## | 100%
libgcc-ng-9.1.0      | 8.1 MB    | ########## | 100%
## Package Plan ##

  environment location: /opt/miniconda

  added / updated specs:
    - python=3.6.2


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2019.5.15  |                0         133 KB
    zlib-1.2.11                |       h7b6447c_3         120 KB
    libedit-3.1                |       heed3624_0         171 KB
    openssl-1.0.2s             |       h7b6447c_0         3.1 MB
    setuptools-41.0.1          |           py36_0         656 KB
    sqlite-3.23.1              |       he433501_0         1.5 MB
    wheel-0.33.4               |           py36_0          40 KB
    certifi-2019.6.16          |           py36_0         154 KB
    pip-19.1.1                 |           py36_0         1.9 MB
    _libgcc_mutex-0.1          |             main           3 KB
    readline-7.0               |       ha6073c6_4         1.1 MB
    ncurses-6.0                |       h9df7e31_2         920 KB
    python-3.6.2               |      hca45abc_19        27.0 MB
    libgcc-ng-9.1.0            |       hdf63c60_0         8.1 MB
    ------------------------------------------------------------
                                           Total:        44.9 MB

The following NEW packages will be INSTALLED:

    _libgcc_mutex:   0.1-main

The following packages will be UPDATED:

    ca-certificates: 2018.03.07-0            --> 2019.5.15-0
    certifi:         2018.8.24-py37_1        --> 2019.6.16-py36_0
    libgcc-ng:       8.2.0-hdf63c60_1        --> 9.1.0-hdf63c60_0
    openssl:         1.0.2p-h14c3975_0       --> 1.0.2s-h7b6447c_0
    pip:             10.0.1-py37_0           --> 19.1.1-py36_0
    setuptools:      40.2.0-py37_0           --> 41.0.1-py36_0
    wheel:           0.31.1-py37_0           --> 0.33.4-py36_0
    zlib:            1.2.11-ha838bed_2       --> 1.2.11-h7b6447c_3

The following packages will be DOWNGRADED:

    libedit:         3.1.20170329-h6b74fdf_2 --> 3.1-heed3624_0
    ncurses:         6.1-hf484d3e_0          --> 6.0-h9df7e31_2
    python:          3.7.0-hc3d631a_0        --> 3.6.2-hca45abc_19
    readline:        7.0-h7b6447c_5          --> 7.0-ha6073c6_4
    sqlite:          3.24.0-h84994c4_0       --> 3.23.1-he433501_0


Downloading and Extracting Packages
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done

Traceback (most recent call last):
  File "/opt/miniconda/bin/conda", line 7, in <module>
    from conda.cli import main
ModuleNotFoundError: No module named 'conda'
The command '/bin/sh -c ldconfig /usr/local/cuda/lib64/stubs &&     conda install -y python=3.6.2 && conda clean -ay &&     pip install --no-cache-dir azureml-defaults &&     pip install --no-cache-dir tensorflow==1.8.0 tensorflow-gpu==1.8.0 keras==2.0.8 matplotlib==3.0.3 seaborn==0.9.0 requests==2.21.0 bs4==0.0.1 imageio==2.5.0 sklearn pandas==0.24.2 numpy==1.16.2 hickle==3.4.3 &&     pip install --no-cache-dir horovod==0.13.5 &&     ldconfig' returned a non-zero code: 1

Datastore must be of type AzureBlobDatastore

default datastore may be Azure Files so it breaks on "Schedule.create" which expects AzureBlobDatastore:

schedule = Schedule.create(workspace=ws, name=pipeline_name + "_sch",
                           pipeline_id=published_pipeline.id, 
                           experiment_name='prednet_master',
                           datastore=datastore,
                           wait_for_provisioning=True,
                           description="Datastore scheduler for Pipeline" + pipeline_name,
                           path_on_datastore='prednet/data/video',
                           polling_interval=1
                           )

error:

Uploaded 1 files
Traceback (most recent call last):
  File "pipelines_master.py", line 145, in <module>
    polling_interval=1
  File "/home/vsts/.local/lib/python3.6/site-packages/azureml/pipeline/core/schedule.py", line 363, in create
    raise ValueError('Datastore must be of type AzureBlobDatastore')
ValueError: Datastore must be of type AzureBlobDatastore
##[error]Script failed with error: Error: /bin/bash failed with return code: 1

The mysterious aml_config directory

Hello, I'm missing where the aml_config directory comes from. When I run create_docker_image.py, an error is thrown because it doesn't exist.

I notice that files it's supposed to contain such as model.json are written to the remote outputs directory when training finishes, but at no point is it downloaded or created locally as part of the process up to this point.

Could you inform me where this is supposed to be coming from?

Error on running create_docker_image.py:

(prednet) MLOps_VideoAnomalyDetection git:master ❯ cd deployment                                                                                                                                                      
(prednet) deployment git:master ❯ ls                                                                                                                                                                                  
azure-pipelines-deploy.yml  create_docker_image.py  deploy.py  environment.yml  inferenceConfig.json  myenv.yml  score.py  test_data  test_local_docker.py  test_webservice.py
(prednet) deployment git:master ❯ py3 create_docker_image.py                                                                                                                                                          
ERROR - Dependency aml_config/model.json doesn't exist. 

Traceback (most recent call last):
  File "create_docker_image.py", line 44, in <module>
    dependencies=['../models/prednet/prednet.py', 'aml_config/model.json']
  File "/home/masoncusack/miniconda3/envs/prednet/lib/python3.6/site-packages/azureml/core/image/container.py", line 159, in image_configuration
    base_image, base_image_registry, cuda_version=cuda_version)
  File "/home/masoncusack/miniconda3/envs/prednet/lib/python3.6/site-packages/azureml/core/image/container.py", line 320, in __init__
    self.validate_configuration()
  File "/home/masoncusack/miniconda3/envs/prednet/lib/python3.6/site-packages/azureml/core/image/container.py", line 488, in validate_configuration
    validate_path_exists_or_throw(dependency, "Dependency")
  File "/home/masoncusack/miniconda3/envs/prednet/lib/python3.6/site-packages/azureml/_model_management/_util.py", line 877, in validate_path_exists_or_throw
    logger=module_logger)
azureml.exceptions._azureml_exception.WebserviceException: WebserviceException:
        Message: Dependency aml_config/model.json doesn't exist. 
        InnerException None
        ErrorResponse 
{
    "error": {
        "message": "Dependency aml_config/model.json doesn't exist. "
    }
}

got an unexpected keyword argument 'hyperdrive_run_config'

hyperdrive_run_config was changed to hyperdrive_config, currently this line breaks inside pipeline_create.py

hd_step = HyperDriveStep(
        name="train_w_hyperdrive",
        hyperdrive_run_config=hdc,
        estimator_entry_script_arguments=[
            '--data-folder', preprocessed_data, 
            '--remote_execution',
            '--dataset', dataset
            ],
        inputs=[preprocessed_data],
        metrics_output = data_metrics,
        allow_reuse=True
    )
    hd_step.run_after(data_prep)

here's the error I get :

Exception has occurred: TypeError
__init__() got an unexpected keyword argument 'hyperdrive_run_config'
  File "C:\Users\gubert\Repos\MLOps_VideoAnomalyDetection\pipelines_create.py", line 203, in build_pipeline
    allow_reuse=True
  File "C:\Users\gubert\Repos\MLOps_VideoAnomalyDetection\pipelines_create.py", line 295, in <module>
    build_pipeline(dataset, ws, config)

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.