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

Add preliminary support of OpenVINO as Keras 3 backend #19727

Open
wants to merge 56 commits into
base: master
Choose a base branch
from

Conversation

rkazants
Copy link

@rkazants rkazants commented May 17, 2024

Details: Support OpenVINO as Keras 3 backend. This is inference-only backend. In order to switch on this, define environment variable as follows: os.environ["KERAS_BACKEND"] = "openvino" or use set_backend.

Here is an example how it works:
Install OpenVINO pip install openvino -U

import os
os.environ["KERAS_BACKEND"] = "openvino"
import numpy as np
import keras

# build a simple network
x1 = keras.Input(shape=(2,), dtype='float32')
x2 = keras.Input(shape=(2,), dtype='float32')
add = keras.layers.Add()([x1, x2])
sigmoid = keras.activations.sigmoid(add)
model = keras.Model(inputs={'x1': x1, 'x2': x2}, outputs={'sigmoid': sigmoid})
model.summary()

# infer using OpenVINO
out = model.predict({'x1': np.array([[1.0, 2.0]], dtype=np.float32),
                     'x2': np.array([[3.0, 4.0]], dtype=np.float32)})
print('openvino out = ', out)

Added support for operations that allow to infer mobilenet_v3_small and bert_base_en_uncased from Keras Hub.

Example 1:

# from here https://keras.io/api/keras_nlp/models/bert/bert_text_classifier/#from_preset-method
import os

os.environ["KERAS_BACKEND"] = "openvino"
import numpy as np
import keras
#import keras_hub

features = {
    "token_ids": np.ones(shape=(2, 12), dtype="int32"),
    "segment_ids": np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0]] * 2),
    "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
}

# Pretrained classifier without preprocessing.
#classifier = keras_hub.models.BertTextClassifier.from_preset(
#    "bert_base_en_uncased",
#    num_classes=4,
#    preprocessor=None,
#)
#keras.saving.save_model(classifier, "bert_base_en_uncased.keras")
classifier = keras.saving.load_model("bert_base_en_uncased.keras")
#output = classifier.predict(x=features, batch_size=2)
output = classifier.predict(features)

Example 2:

# from here https://keras.io/api/keras_cv/models/backbones/mobilenet_v3/
import os

os.environ["KERAS_BACKEND"] = "openvino"
import numpy as np
import keras
import keras_cv

rng = np.random.default_rng(23345243)

#input_data = np.random.rand(8, 224, 224, 3)
input_data = rng.uniform(0.0, 1.0, [8, 224, 224, 3]).astype(np.float32)
model = keras.saving.load_model("mobilenet_v3_small.keras")
#model = keras_cv.models.MobileNetV3Backbone.from_preset("mobilenet_v3_small",)

output = model.predict(input_data)

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Copy link

google-cla bot commented May 17, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

keras/src/ops/function.py Outdated Show resolved Hide resolved
keras/src/layers/layer.py Outdated Show resolved Hide resolved
keras/src/backend/openvino/linalg.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented May 20, 2024

Codecov Report

Attention: Patch coverage is 53.52994% with 520 lines in your changes missing coverage. Please review.

Project coverage is 81.57%. Comparing base (e0f61ee) to head (ffc4e22).
Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/openvino/numpy.py 53.08% 185 Missing and 13 partials ⚠️
keras/src/backend/openvino/core.py 52.94% 102 Missing and 10 partials ⚠️
keras/src/backend/openvino/nn.py 55.66% 92 Missing and 2 partials ⚠️
keras/src/backend/openvino/trainer.py 25.00% 87 Missing ⚠️
keras/src/backend/openvino/random.py 64.28% 14 Missing and 1 partial ⚠️
keras/src/backend/openvino/math.py 75.00% 7 Missing ⚠️
keras/src/utils/backend_utils.py 25.00% 3 Missing ⚠️
keras/src/backend/__init__.py 75.00% 0 Missing and 1 partial ⚠️
keras/src/backend/openvino/rnn.py 85.71% 1 Missing ⚠️
keras/src/layers/layer.py 50.00% 0 Missing and 1 partial ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19727      +/-   ##
==========================================
- Coverage   82.20%   81.57%   -0.63%     
==========================================
  Files         515      526      +11     
  Lines       48085    49284    +1199     
  Branches     7526     7636     +110     
==========================================
+ Hits        39527    40205     +678     
- Misses       6740     7235     +495     
- Partials     1818     1844      +26     
Flag Coverage Δ
keras 81.42% <53.52%> (-0.63%) ⬇️
keras-jax 63.65% <0.08%> (-1.52%) ⬇️
keras-numpy 58.76% <0.17%> (-1.39%) ⬇️
keras-openvino 25.60% <53.35%> (?)
keras-tensorflow 64.59% <0.08%> (-1.54%) ⬇️
keras-torch 63.58% <0.08%> (-1.51%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gbaned
Copy link
Collaborator

gbaned commented Jun 14, 2024

Hi @rkazants Can you please resolve the conflicts? Thank you!

@rkazants
Copy link
Author

Hi @rkazants Can you please resolve the conflicts? Thank you!

I am working on this PR. I am trying to resolve comments.

Best regards,
Roman

@gbaned
Copy link
Collaborator

gbaned commented Jul 12, 2024

Hi @rkazants Any update on this PR? Please. Thank you!

Copy link

This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Jul 27, 2024
@rkazants
Copy link
Author

I am just back from vacation. I plan to continue from the next week.

Copy link

This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@gbaned
Copy link
Collaborator

gbaned commented Sep 9, 2024

Hi @rkazants Any update on this PR? Please. Thank you!

@rkazants
Copy link
Author

rkazants commented Sep 9, 2024

Hi @rkazants Any update on this PR? Please. Thank you!

Hi @gbaned, please anticipate the update early this week. Sorry for the delay.

Thanks,
Roman

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
@rkazants
Copy link
Author

@fchollet, please take a look at this PR.

Thanks,
Roman

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Assigned Reviewer
Development

Successfully merging this pull request may close these issues.

5 participants