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 support for np array-based tensors for Keras, Pytorch and TF flavors #3808

Merged
merged 20 commits into from Dec 22, 2020

Conversation

wentinghu
Copy link
Contributor

@wentinghu wentinghu commented Dec 10, 2020

Signed-off-by: Wendy Hu wendy.hu@databricks.com

How is this patch tested?

  • Added PyFunc tests
  • Added tests for each affected flavor

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

Added support for np array-based input tensors for Keras. PyTorch and TF flavors

  • pyfunc keras models now accept pd.DataFrames, dicts, lists, and np.ndarrays
  • pyfunc pytorch models now accept pd.DataFrames and np.ndarrays
  • TF (v1 and v2) pytorch models now accept pd.DataFrames and dicts

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/projects: MLproject format, project running backends
  • area/scoring: Local serving, model deployment tools, spark UDFs
  • area/server-infra: MLflow server, JavaScript dev server
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, JavaScript, plotting
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
mlflow/keras.py Outdated
if self._graph is not None:
with self._graph.as_default():
with self._sess.as_default():
predicted = pd.DataFrame(self.keras_model.predict(dataframe.values))
predicted = pd.DataFrame(self.keras_model.predict(input_data))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we only want to return DataFrame if the input was DataFrame. If the was one of the numpy array based data types we should return the result as is.

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@github-actions github-actions bot added the area/models MLmodel format, model serialization/deserialization, flavors label Dec 11, 2020
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
mlflow/pyfunc/__init__.py Outdated Show resolved Hide resolved
mlflow/pyfunc/__init__.py Outdated Show resolved Hide resolved
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@wentinghu wentinghu marked this pull request as ready for review December 14, 2020 15:56
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@wentinghu wentinghu changed the title Add support for np array-based tensors for Keras flavor Add support for np array-based tensors for Keras and Pytorch flavors Dec 14, 2020
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@wentinghu wentinghu changed the title Add support for np array-based tensors for Keras and Pytorch flavors Add support for np array-based tensors for Keras, Pytorch and TF flavors Dec 14, 2020
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Copy link
Contributor

@tomasatdatabricks tomasatdatabricks left a comment

Choose a reason for hiding this comment

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

Looks great, I left some comments.

mlflow/pytorch/__init__.py Show resolved Hide resolved
mlflow/tensorflow.py Outdated Show resolved Hide resolved
if isinstance(data, dict):
feed_dict = {k: tensorflow.constant(v) for k, v in data.items()}
elif isinstance(data, pandas.DataFrame):
for df_col_name in list(data):
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably simplify now that know data is DataFrame to:feed_dict = {k: tensorflow.constant(v.values) for k, v in list(data)}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I think the purpose of this implementation is explained in the comments below. We need to also check if data[df_col_name] is also a dataframe (there are multiple columns with the same name) and convert that to a np array instead. If there's only one column, data[df_col_name] will have a pandas.core.series.Series type, which is fine to pass into tensorflow.constant I guess

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, interesting, I did not know! Thanks for clarifying this.
I guess this is another way of passing tensors in a Dataframe.

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
…type is not supported

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Copy link
Contributor

@tomasatdatabricks tomasatdatabricks left a comment

Choose a reason for hiding this comment

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

Looks great! I think we should add one minor test case, but otherwise lgtm.

if isinstance(data, dict):
feed_dict = {k: tensorflow.constant(v) for k, v in data.items()}
elif isinstance(data, pandas.DataFrame):
for df_col_name in list(data):
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, interesting, I did not know! Thanks for clarifying this.
I guess this is another way of passing tensors in a Dataframe.

@@ -267,6 +267,49 @@ def predict(pdf):
res = pyfunc_model.predict(pdf)
assert res.dtypes.to_dict() == expected_types

# 8. np.ndarrays can be converted to dataframe but have no columns
with pytest.raises(MlflowException) as ex:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also test that his works if the schema has no column names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think this case is tested here under test_schema_enforcement_no_col_names

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
This reverts commit 2b825e9.

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@github-actions github-actions bot added the area/scoring MLflow Model server, model deployment tools, Spark UDFs label Dec 22, 2020
@wentinghu wentinghu merged commit 209e8ad into mlflow:master Dec 22, 2020
lorenzwalthert pushed a commit to lorenzwalthert/mlflow that referenced this pull request Dec 23, 2020
…ors (mlflow#3808)

* Add support for np array-based tensors for Keras flavor

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for np array inputs for pytorch pyfunc model

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for dicts for tf pyfunc models

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@github-actions github-actions bot added the rn/feature Mention under Features in Changelogs. label Dec 24, 2020
lorenzwalthert pushed a commit to lorenzwalthert/mlflow that referenced this pull request Jan 15, 2021
…ors (mlflow#3808)

* Add support for np array-based tensors for Keras flavor

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for np array inputs for pytorch pyfunc model

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for dicts for tf pyfunc models

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
@wentinghu wentinghu deleted the keras-tensor-inputs branch February 4, 2021 18:38
harupy pushed a commit to chauhang/mlflow that referenced this pull request Apr 8, 2021
…ors (mlflow#3808)

* Add support for np array-based tensors for Keras flavor

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for np array inputs for pytorch pyfunc model

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for dicts for tf pyfunc models

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
harupy pushed a commit to wamartin-aml/mlflow that referenced this pull request Jun 7, 2021
…ors (mlflow#3808)

* Add support for np array-based tensors for Keras flavor

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for np array inputs for pytorch pyfunc model

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>

* Add support for dicts for tf pyfunc models

Signed-off-by: Wendy Hu <wendy.hu@databricks.com>
Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/models MLmodel format, model serialization/deserialization, flavors area/scoring MLflow Model server, model deployment tools, Spark UDFs rn/feature Mention under Features in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants