Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object Detection Demo Jupyter Notebook #2093

Conversation

helena-intel
Copy link

Jupyter Notebook for Async Object Detection. I took the Python demo and made it into a notebook that allows a user to select a model and a video (or upload a video of their own), and set settings for max_num_requests, num_threads and num_streams.

The notebook downloads the model, optionally converts it, does inference, and shows the results. It shows "live" results of a selected model on one video, and on three frames of multiple selected models (to quickly compare results). By default currently only Intel models are used because they do not require using the model converter, so this notebook can be used with the pip version of OpenVINO. I can update this when the model optimizer becomes part of the pip distribution.

The target audience for this notebook includes less experienced developers. The goal of this specific notebook is to demonstrate Object Detection with the Async API. I will also make more introductionary notebooks for using OpenVINO/OMZ in general.

Try without installing

You can try it here: https://mybinder.org/v2/gh/helena-intel/open_model_zoo/object-detection-demo-notebook-binder?filepath=%2Fdemos%2Fobject_detection_demo%2Fjupyter-python%2Fobject_detection_demo.ipynb There is also a version where the code is hidden: https://mybinder.org/v2/gh/helena-intel/open_model_zoo/detection-async-notebook?urlpath=voila%2Frender%2Fobject_detection_demo.ipynb Note that performance may be slow and it may randomly crash.

README/Usage notes

The README instructions need to be updated, but I wanted to wait for the updated pip install method to do that. If you already have OpenVINO installed you can run the notebook from a terminal/cmd.exe where you have set the PATH/LD_LIBRARY_PATH or run setupvars. Then install the requirements in the demos/object_detection_demo/jupyter-python folder and run the notebook, with jupyter notebook or jupyter lab. Note: the notebook calls subprocess to run the model optimizer. If you use a virtualenv, this only works if your environment variables are set globally. If you set them in the shell before running the notebook, subprocess doesn't see them. With the new pip release this should no longer be an issue (otherwise I can change the subprocess call).

Tested on Windows and Linux (Ubuntu 18).

Notes and questions

  • I am relatively new to OpenVINO and OMZ and am not sure if I follow best practices. All feedback is welcome!
  • I would like to include a bit more information on the options for num_threads, num_streams and max_num_requests but I don't know enough about this to write this.
  • I got the intro text from the Python demo, but the target audience for the notebook demo's is a bit different and the intro text could be a bit more catchy. Suggestions are welcome.
  • I put some code in a detection_utils file and some directly in the notebook. I tried to find a balance, so that the important code for this demo is still in the notebook, but that it is not too much code. I am not sure if I found the right balance.
  • I used ipywidgets so that there are dropdowns to select a model/video and buttons to start inferencing. I find it useful for this demo because there are so many models to choose from, and it allows for really quick testing/comparing of detection models. It also makes it possible to make an "app version" (using voila, to quickly show results to others for example). But it does add a bit of new complexity.
  • I made a models-subset.lst with a mostly random selection of models. I found that useful for testing, for example on Binder, and for slow computers/connections. If we want to keep that, I would welcome a selection of models that is less random.
  • I download the models by default to $HOME/open_model_zoo_models and set the cache to $HOME/open_model_zoo_cache. I keep the converted models in the same directory as the downloaded models. I am happy to change these directories to something else, I do not know what is best practice. I would like to have a standard directories for this that always work, so that I can use them for further OMZ notebooks.
  • New Python packages in requirements.txt are voila, ipywidgets and jupyterlab. They are all BSD licensed.

Screenshots of the completed notebook and app-version

omz_object_detection_demo_notebook
omz_object_detection_demo_notebook_voila

@openvino-pushbot
Copy link
Contributor

Can one of the admins verify this patch?

@vladimir-dudnik
Copy link
Contributor

@helena-intel that basically work for me (tested on Windows). Although get some questions:

  • downloading and converting all OMZ models is costly process, it make take hours. It would be much easier if by starting notebook we can pass as a parameter folders for downloaded models and downloader cache, so if customer already downloaded models when was playing with other demos, no need to manually edit notebook. Currently it silently start model downloading to predefined place. (do not know if notebooks provide such capabilities, to start with params).
  • it seems there is no need to install openvino python module, just initialize openvino environment by running setupvars.sh/.bat from openvino installed location, just like it is needed for all other demos. This script will initialize both PYTHONPATH and PATH environment vars for console it was called from.

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@helena-intel
Copy link
Author

  • downloading and converting all OMZ models is costly process, it make take hours. It would be much easier if by starting notebook we can pass as a parameter folders for downloaded models and downloader cache, so if customer already downloaded models when was playing with other demos, no need to manually edit notebook. Currently it silently start model downloading to predefined place. (do not know if notebooks provide such capabilities, to start with params).

If you change the base_model_dir and omz_cache_dir in the Settings cell (at the top) it will not redownload everything and just use the models that are there. But as far as I know there is no way to set this from the command line when you start Jupyter. I plan to reuse this directory across all demo's (and am happy to change to other directory names) so then a user only needs to download once. I already made a subset of models to download by default. I can make that a lot smaller. I can also highlight the setting in the README and make it a highlighted alert box in the notebook itself.

  • it seems there is no need to install openvino python module, just initialize openvino environment by running setupvars.sh/.bat from openvino installed location, just like it is needed for all other demos. This script will initialize both PYTHONPATH and PATH environment vars for console it was called from.

Correct. The instructions were for people who are completely new to this and do not have OpenVINO installed yet. I will update the instructions, also to account for the new pip method. I can add an "if you have already installed OpenVINO" part to the instructions (where I can also point to the directory settings). I would like to still keep this method to install everything including openvino with pip install -r requirements.txt though (and not requiring the full install of OpenVINO and running setupvars). That makes it much easier to get started with this, also during live demos/webinars.

@vladimir-dudnik
Copy link
Contributor

@helena-intel please note pre-commit style check failed:
13:49:31 demos/object_detection_demo/jupyter-python/README.md:32: trailing whitespace.
13:49:31 +2. Install Python
13:49:31 demos/object_detection_demo/jupyter-python/README.md:43: trailing whitespace.
13:49:31 + - NOTE: the PATH is remembered as long as you do not close the terminal application. If you open a new terminal and want to run the notebooks again, you have to set the PATH again.
13:49:31 demos/object_detection_demo/jupyter-python/README.md:48: trailing whitespace.
13:49:31 +
13:49:31 demos/object_detection_demo/jupyter-python/README.md:70: trailing whitespace.
13:49:31 +steps under Configure the Model Optimizer.
13:49:31 demos/object_detection_demo/jupyter-python/README.md:71: new blank line at EOF.
13:49:31 demos/object_detection_demo/jupyter-python/detection_utils.py:55: trailing whitespace.
13:49:31 +
13:49:31 demos/object_detection_demo/jupyter-python/detection_utils.py:56: trailing whitespace.
13:49:31 +
13:49:32 running yamllint...
13:49:37 running flake8...
13:49:45 ./demos/object_detection_demo/jupyter-python/detection_utils.py:13:1: F401 'monitors' imported but unused
13:49:45 demos/object_detection_demo/jupyter-python/detection_utils.py:117: last line doesn't end with a newline character

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@helena-intel
Copy link
Author

@helena-intel please note pre-commit style check failed:
13:49:31 demos/object_detection_demo/jupyter-python/README.md:32: trailing whitespace.

Apologies, I fixed that, and found the check-basics.py script to prevent this in the future.

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

1 similar comment
@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@helena-intel
Copy link
Author

I added a NotebookDemo to the demo tests and a test case for this object detection notebook. The test converts the notebook to a Python script and runs it with default arguments. For now, the test case only tests that the notebook runs without exceptions (so it catches import errors etc.) not if there is actually useful output. I will update the test to a more useful one if this is the right approach.

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

@helena-intel we need to update CI requirements in order to make this work.

cc @nignatovsky @IRDonch

@helena-intel helena-intel force-pushed the object-detection-demo-notebook branch from c9793fb to 0a28004 Compare March 2, 2021 12:12
@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

@nignatovsky please check your modification of requirements for CI. On Linux it gives an error:
ERROR: Could not find a version that satisfies the requirement pywin32==300

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

demos/tests/cases.py Outdated Show resolved Hide resolved
demos/tests/cases.py Outdated Show resolved Hide resolved
Fix to make jupyter work in CI

Co-authored-by: Nikita Ignatovsky <73649020+nignatovsky@users.noreply.github.com>
@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

demos/tests/cases.py Outdated Show resolved Hide resolved
demos/tests/cases.py Outdated Show resolved Hide resolved
demos/tests/cases.py Outdated Show resolved Hide resolved
demos/tests/cases.py Outdated Show resolved Hide resolved
Comment on lines 100 to 101
# Change the working directory to the directory that contains the notebook
changedir = f"import os\nos.chdir(r'{os.path.dirname(python_file)}')\n"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? The script ought to work from any directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the notebook is run, the current directory is the notebook directory, and imports from that directory work. This makes the test script do the same thing.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the notebook is run, the current directory is the notebook directory

Not necessarily, the user could run it from any directory.

and imports from that directory work

Is it really required for the current directory to be the notebook directory for the imports to work?

demos/tests/cases.py Outdated Show resolved Hide resolved
demos/tests/cases.py Outdated Show resolved Hide resolved
…ning, updated installation instructions, updated error message in notebook
@helena-intel
Copy link
Author

@IRDonch thanks for your review and suggestions! I have implemented most of your suggestions now, and marked those conversations as resolved.

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

@helena-intel please take a look at pre-commit failures:

12:55:42 Fixed arguments: /home/jenkins/workspace/omz/precommit/demos_ubuntu18/venv/bin/python /home/jenkins/workspace/omz/precommit/demos_ubuntu18/src/demos/object_detection_demo/jupyter_python/object_detection_demo_test.py
12:55:42
12:55:42 Test case #0/CPU: -i /mnt/datasets/Image_Retrieval/d0c460d0-4d75-4315-98a8-a0116d3dfb81.dav -m /tmp/tmpl63ylz3v/models/public/yolo-v3-tiny-tf/FP16/yolo-v3-tiny-tf.xml
12:55:42
12:55:42 /home/jenkins/workspace/omz/precommit/demos_ubuntu18/venv/bin/python: can't open file '/home/jenkins/workspace/omz/precommit/demos_ubuntu18/src/demos/object_detection_demo/jupyter_python/object_detection_demo_test.py': [Errno 2] No such file or directory

@helena-intel
Copy link
Author

@helena-intel please take a look at pre-commit failures:
[...]
12:55:42 /home/jenkins/workspace/omz/precommit/demos_ubuntu18/venv/bin/python: can't open file '/home/jenkins/workspace/omz/precommit/demos_ubuntu18/src/demos/object_detection_demo/jupyter_python/object_detection_demo_test.py': [Errno 2] No such file or directory

@vladimir-dudnik After reading the change requests, and changing the command to run jupyter, I had hoped that the PATH and PYTHONPATH variables were unnecessary now, so I removed them. The test did pass on my local machine. I'll add the PATH variable back. That should hopefully fix this. Is there a way for me to check the CI pipeline without making a commit and update the PR?

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

@helena-intel without making commit you can only test this locally, by making the same environment and call run_tests.py

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

2 similar comments
@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

@vladimir-dudnik
Copy link
Contributor

@helena-intel still can't find script file

17:00:20 /home/jenkins/workspace/omz/precommit/demos_ubuntu18/venv/bin/python: can't open file '/home/jenkins/workspace/omz/precommit/demos_ubuntu18/src/demos/object_detection_demo/jupyter_python/object_detection_demo_test.py': [Errno 2] No such file or directory

@helena-intel
Copy link
Author

@vladimir-dudnik I think the recent commit doesn't fix it. Working on it now.

demos/tests/cases.py Outdated Show resolved Hide resolved
Comment on lines 100 to 101
# Change the working directory to the directory that contains the notebook
changedir = f"import os\nos.chdir(r'{os.path.dirname(python_file)}')\n"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the notebook is run, the current directory is the notebook directory

Not necessarily, the user could run it from any directory.

and imports from that directory work

Is it really required for the current directory to be the notebook directory for the imports to work?

demos/tests/cases.py Outdated Show resolved Hide resolved
@@ -8,3 +8,6 @@ tokenizers
tensorboard
tensorboardX
tqdm
jupyterlab
ipywidgets
opencv-python-headless
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove OpenCV from here, as well. Also, keep the list in alphabetic order.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to regenerate ci/requirements-demos.txt from the new version of this file.

# Set User-Agent to Mozilla because some websites block requests with User-Agent Python
request = urllib.request.Request(path, headers={"User-Agent": "Mozilla/5.0"})
response = urllib.request.urlopen(request)
data = response.read()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems dangerous to load the whole file into memory at once. It could be multiple gigabytes in size. I would suggest to download it in chunks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid the notebook will have more problems if people start using multi-gigabyte video's... (The instructions say to use small video's too). I'll change it though, it is good to err on the safe side.

Co-authored-by: Roman Donchenko <roman.donchenko@intel.com>
Wrap reading file object with `with` statement
Update and sort demo requirements
Use fnmatch instead of re
Use pathlib for paths
Select IR models based on dldt framework
Delete demo_env from tests (should no longer be necessary)
Reformat template for adding test code to script in cases.py/NotebookDemo
Test uses FP32 models instead of FP16
Skip model downloading in test
…tel/open_model_zoo into object-detection-demo-notebook
@vladimir-dudnik
Copy link
Contributor

Jenkins please retry a build

# Add `test_code` to the test script and make it accept command line arguments
content = f"""
import os
os.chdir(r'{os.path.dirname(python_file)}')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
os.chdir(r'{os.path.dirname(python_file)}')
os.chdir({os.path.dirname(python_file)!r})

Let Python escape any special characters for you. :-)

# Change settings for test script
base_model_dir=dirname(dirname(dirname(dirname(args.m))))

PRECISION = 'FP32'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this ought to be part of the test code.

@vladimir-dudnik
Copy link
Contributor

we will close this PR in favour of new repo especially created to develop and distribute OpenVINO jupyter notebook demos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants