open_model_zoo icon indicating copy to clipboard operation
open_model_zoo copied to clipboard

Dynamic Open Model Zoo as Python module

Open likholat opened this issue 5 years ago • 28 comments

Current state

Adding to OpenVino

  1. Create a symbolic link:
    ln -s /opt/intel/openvino_2021/deployment_tools/open_model_zoo/libs/python/open_model_zoo.py /opt/intel/openvino_2021/python/python3.X/openvino/open_model_zoo.py
    
  2. Add path to downloader to python path
    export PYTHONPATH=$PYTHONPATH:/opt/intel/openvino/deployment_tools/open_model_zoo/tools/downloader
    
  3. Initialize openvino enviroment variable :
     source /opt/intel/openvino_2021/bin/setupvars.sh
    

Model's downloading

import openvino.open_model_zoo as omz

ssd = omz.Model('mobilenet-ssd')
################|| Downloading models ||################

========== Downloading /user/models/public/mobilenet-ssd/mobilenet-ssd.prototxt
... 100%, 28 KB, 38894 KB/s, 0 seconds passed

========== Downloading /user/models/public/mobilenet-ssd/mobilenet-ssd.caffemodel
... 100%, 22605 KB, 3760 KB/s, 6 seconds passed

################|| Post-processing ||################

Documentation

import openvino.open_model_zoo as omz

ssd = omz.Model('mobilenet-ssd', precision='FP32')

print('Model task type: ' + ssd.task_type)
print(ssd.description)
Model task type: detection​
The "mobilenet-ssd" model is a Single-Shot multibox Detection (SSD) network intended to perform object detection. This model is implemented using the Caffe* framework. For details about this model, check out the repository <https://github.com/chuanqi305/MobileNet-SSD>.​
The model input is a blob that consists of a single image of 1x3x300x300 in BGR order, also like the "densenet-121" model. The BGR mean values need to be subtracted as follows: [127.5, 127.5, 127.5] before passing the image blob into the network. In addition, values must be divided by 0.007843.​
The model output is a typical vector containing the tracked object data, as previously described.​

    License: https://raw.githubusercontent.com/chuanqi305/MobileNet-SSD/master/LICENSE

Usage example

import os
import numpy as np
import cv2 as cv

import openvino.open_model_zoo as omz

from openvino.inference_engine import IECore

ssd = omz.Model('mobilenet-ssd', precision='FP32')
xml_path, bin_path = ssd.xml_path, ssd.bin_path
ie = IECore()
net = ie.read_network(xml_path, bin_path)
exec_net = ie.load_network(net, 'CPU')

image = cv.imread('example.jpeg')
predictions = detect(net, image)

rows = image.shape[0]
cols = image.shape[1]

for detection in predictions.reshape(-1, 7):
    image_id = detection[0]
    if image_id < 0:
        break
 
    if detection[2] > 0.6:
        xmin = int(detection[3] * cols)
        ymin = int(detection[4] * rows)
        xmax = int(detection[5] * cols)
        ymax = int(detection[6] * rows)

        cv.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0))

cv.imshow('out', image)
cv.waitKey()

image

  • Tests To run tests use:
source /opt/intel/openvino_2021/bin/setupvars.sh
python3 /user/open_model_zoo/tools/downloader/open_model_zoo_test.py

likholat avatar Jul 24 '20 16:07 likholat

Can one of the admins verify this patch?

openvino-pushbot avatar Jul 24 '20 16:07 openvino-pushbot

Can one of the admins verify this patch?

openvino-pushbot avatar Jul 24 '20 16:07 openvino-pushbot

Can one of the admins verify this patch?

openvino-pushbot avatar Jul 24 '20 16:07 openvino-pushbot

Can one of the admins verify this patch?

openvino-pushbot avatar Jul 24 '20 16:07 openvino-pushbot

Can one of the admins verify this patch?

openvino-pushbot avatar Jul 24 '20 16:07 openvino-pushbot

@dkurt

likholat avatar Jul 24 '20 16:07 likholat

Hi @likholat,

please make develop as target branch for your request, thanks

eaidova avatar Jul 24 '20 17:07 eaidova

import topologies as omz
face_detection = omz.Topology('face-detection-0102', precision='FP32')
xml_path, bin_path = face_detection.get_ir('FP16')

output:

################|| Downloading models ||################

========== Downloading /models/intel/face-detection-0102/FP32/face-detection-0102.xml
... 100%, 162 KB, 401520 KB/s, 0 seconds passed

========== Downloading /models/intel/face-detection-0102/FP32/face-detection-0102.bin
... 100%, 7098 KB, 4240 KB/s, 1 seconds passed

################|| Post-processing ||################

========== Skipping face-detection-0102 (no conversions defined)

likholat avatar Jul 30 '20 11:07 likholat

@IRDonch, @ngaloppo, Please review this PR. This PR is a continuation of https://github.com/openvinotoolkit/open_model_zoo/pull/304

likholat avatar Aug 03 '20 16:08 likholat

For topology.py, downloader.py and converter.py change imports: import common -> from . import common import downloader -> from . import downloader import converter -> from . import converter

  1. Is there a way to avoid changing these files?
  2. Can the procedure be simplified if we assume that file is named as open_model_zoo.py?

dkurt avatar Aug 18 '20 20:08 dkurt

You seem to be missing a block of hidden comments. Make sure to expand all comments.

IRDonch avatar Aug 25 '20 17:08 IRDonch

@IRDonch, please, tell me, which directory is the best place to put the open_model_zoo.py?

likholat avatar Aug 28 '20 09:08 likholat

@likholat I already made a suggestion above:

I would suggest libs/python/open_model_zoo.py.

IRDonch avatar Aug 28 '20 10:08 IRDonch

@likholat @dkurt @IRDonch May I know why has this PR stalled?

druzhkov-paul avatar Feb 16 '21 10:02 druzhkov-paul

@druzhkov-paul, good question. @likholat, let's revise our latest progress. For Python demos such kind of feature would be very nice.

dkurt avatar Mar 09 '21 09:03 dkurt

@IRDonch @eizamaliev, please review it again

likholat avatar Mar 10 '21 17:03 likholat

Jenkins please retry a build

jkamelin avatar Dec 02 '21 08:12 jkamelin

Jenkins please retry a build

jkamelin avatar Dec 17 '21 14:12 jkamelin

@nignatovsky please look at this, some CI issue

vladimir-dudnik avatar Dec 18 '21 00:12 vladimir-dudnik

Jenkins please retry a build

jkamelin avatar Dec 20 '21 06:12 jkamelin

Jenkins please retry a build

jkamelin avatar Dec 29 '21 11:12 jkamelin

Jenkins please retry a build

jkamelin avatar Jan 10 '22 18:01 jkamelin

Jenkins please retry a build

vladimir-dudnik avatar Jan 27 '22 22:01 vladimir-dudnik

Do you know if there is a way to have openvino.model_zoo.open_model_zoo and openvino.model_zoo.model_api.models both in editable mode at the same time through pip -e or PYTHONPATH?

I believe it doesn't work because there is a demos/common/python/openvino/model_zoo/__init__.py file, which marks openvino.model_zoo as being a regular package. A regular package can only belong to a single project. The simplest way to fix this is to turn openvino.model_zoo into a namespace package by removing that __init__.py file (you might also need to adjust setup.py accordingly).

IRDonch avatar Mar 01 '22 13:03 IRDonch

There are two ways of creating a namespace package. Omitting __init__.py or putting __path__ = __import__('pkgutil').extend_path(__path__, __name__) into it. We are forced to use the second option for openvino namespaces after the main openvino project. To be consistent for model_zoo namespace we should follow the same option. @jkamelin, please, add it

Wovchena avatar Mar 02 '22 10:03 Wovchena

We are forced to use the second option for openvino namespaces after the main openvino project.

FWIW, I don't think you're actually forced to use this option, because native namespace packages are compatible with pkgutil-style ones. pkgutil.extend_path will locate all directories named after the package, regardless of whether they have an __init__.py file or not.

IRDonch avatar Mar 02 '22 11:03 IRDonch

Yes, I realized that after I posted the message. But still, it feels reasonable to go with __path__ = __import__('pkgutil').extend_path(__path__, __name__) to stay aligned with root openvino init

Wovchena avatar Mar 02 '22 11:03 Wovchena

@jkamelin there is merge conflict

vladimir-dudnik avatar Apr 04 '22 08:04 vladimir-dudnik