Skip to content

Commit

Permalink
feat(components): Added the component to convert from KerasModelHdf5 …
Browse files Browse the repository at this point in the history
…to TensorflowSavedModel (#4524)

* Components -  Added the component to convert from KerasModelHdf5 to TensorflowSavedModel

* Adding the component to a sample
  • Loading branch information
Ark-kun authored Sep 21, 2020
1 parent 9287287 commit 4830e03
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from kfp.components import create_component_from_func, InputPath, OutputPath

def keras_convert_hdf5_model_to_tf_saved_model(
model_path: InputPath('KerasModelHdf5'),
converted_model_path: OutputPath('TensorflowSavedModel'),
):
'''Converts Keras HDF5 model to Tensorflow SavedModel format.
Args:
model_path: Keras model in HDF5 format.
converted_model_path: Keras model in Tensorflow SavedModel format.
Annotations:
author: Alexey Volkov <alexey.volkov@ark-kun.com>
'''
from pathlib import Path
from tensorflow import keras

model = keras.models.load_model(filepath=model_path)
keras.models.save_model(model=model, filepath=converted_model_path, save_format='tf')


if __name__ == '__main__':
keras_convert_hdf5_model_to_tf_saved_model_op = create_component_from_func(
keras_convert_hdf5_model_to_tf_saved_model,
base_image='tensorflow/tensorflow:2.3.0',
packages_to_install=['h5py==2.10.0'],
output_component_file='component.yaml',
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Keras convert hdf5 model to tf saved model
description: Converts Keras HDF5 model to Tensorflow SavedModel format.
inputs:
- {name: model, type: KerasModelHdf5, description: Keras model in HDF5 format.}
outputs:
- {name: converted_model, type: TensorflowSavedModel, description: Keras model in Tensorflow SavedModel format.}
implementation:
container:
image: tensorflow/tensorflow:2.3.0
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'h5py==2.10.0' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet
--no-warn-script-location 'h5py==2.10.0' --user) && "$0" "$@"
- python3
- -u
- -c
- |
def _make_parent_dirs_and_return_path(file_path: str):
import os
os.makedirs(os.path.dirname(file_path), exist_ok=True)
return file_path
def keras_convert_hdf5_model_to_tf_saved_model(
model_path,
converted_model_path,
):
'''Converts Keras HDF5 model to Tensorflow SavedModel format.
Args:
model_path: Keras model in HDF5 format.
converted_model_path: Keras model in Tensorflow SavedModel format.
Annotations:
author: Alexey Volkov <alexey.volkov@ark-kun.com>
'''
from pathlib import Path
from tensorflow import keras
model = keras.models.load_model(filepath=model_path)
keras.models.save_model(model=model, filepath=converted_model_path, save_format='tf')
import argparse
_parser = argparse.ArgumentParser(prog='Keras convert hdf5 model to tf saved model', description='Converts Keras HDF5 model to Tensorflow SavedModel format.')
_parser.add_argument("--model", dest="model_path", type=str, required=True, default=argparse.SUPPRESS)
_parser.add_argument("--converted-model", dest="converted_model_path", type=_make_parent_dirs_and_return_path, required=True, default=argparse.SUPPRESS)
_parsed_args = vars(_parser.parse_args())
_outputs = keras_convert_hdf5_model_to_tf_saved_model(**_parsed_args)
args:
- --model
- {inputPath: model}
- --converted-model
- {outputPath: converted_model}
9 changes: 7 additions & 2 deletions components/keras/Train_classifier/_samples/sample_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
chicago_taxi_dataset_op = components.load_component_from_url('https://raw.githubusercontent.com/kubeflow/pipelines/e3337b8bdcd63636934954e592d4b32c95b49129/components/datasets/Chicago%20Taxi/component.yaml')
pandas_transform_csv_op = components.load_component_from_url('https://raw.githubusercontent.com/kubeflow/pipelines/6162d55998b176b50267d351241100bb0ee715bc/components/pandas/Transform_DataFrame/in_CSV_format/component.yaml')
keras_train_classifier_from_csv_op = components.load_component_from_url('https://raw.githubusercontent.com/kubeflow/pipelines/f6aabf7f10b1f545f1fd5079aa8071845224f8e7/components/keras/Train_classifier/from_CSV/component.yaml')
keras_convert_hdf5_model_to_tf_saved_model_op = components.load_component_from_url('https://raw.githubusercontent.com/kubeflow/pipelines/51e49282d9511e4b72736c12dc66e37486849c6e/components/_converters/KerasModelHdf5/to_TensorflowSavedModel/component.yaml')


number_of_classes = 2

Expand Down Expand Up @@ -38,14 +40,17 @@ def keras_classifier_pipeline():
transform_code='''df = df["was_tipped"] * 1''',
).output

keras_train_classifier_from_csv_op(
keras_model_in_hdf5 = keras_train_classifier_from_csv_op(
training_features=features_in_csv,
training_labels=labels_in_csv,
network_json=dense_network_with_sigmoid.to_json(),
learning_rate=0.1,
num_epochs=100,
)
).outputs['model']

keras_model_in_tf_format = keras_convert_hdf5_model_to_tf_saved_model_op(
model=keras_model_in_hdf5,
).output

if __name__ == '__main__':
kfp_endpoint = None
Expand Down

0 comments on commit 4830e03

Please sign in to comment.