-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): Added the component to convert from KerasModelHdf5 …
…to TensorflowSavedModel (#4524) * Components - Added the component to convert from KerasModelHdf5 to TensorflowSavedModel * Adding the component to a sample
- Loading branch information
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
components/_converters/KerasModelHdf5/to_TensorflowSavedModel/component.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) |
55 changes: 55 additions & 0 deletions
55
components/_converters/KerasModelHdf5/to_TensorflowSavedModel/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters