name: Convert to tensorflow saved model from onnx model
metadata:
  annotations: 
    author: Alexey Volkov <alexey.volkov@ark-kun.com>
    canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/_converters/OnnxModel/to_TensorflowSavedModel/component.yaml'
inputs:
- {name: model, type: OnnxModel}
outputs:
- {name: converted_model, type: TensorflowModel}
implementation:
  container:
    image: tensorflow/tensorflow:2.4.1
    command:
    - sh
    - -c
    - (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
      'onnx-tf==1.7.0' 'onnx==1.8.0' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m
      pip install --quiet --no-warn-script-location 'onnx-tf==1.7.0' 'onnx==1.8.0'
      --user) && "$0" "$@"
    - sh
    - -ec
    - |
      program_path=$(mktemp)
      printf "%s" "$0" > "$program_path"
      python3 -u "$program_path" "$@"
    - |
      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 convert_to_tensorflow_saved_model_from_onnx_model(
          model_path,
          converted_model_path,
      ):
          import onnx
          import onnx_tf

          onnx_model = onnx.load(model_path)
          tf_rep = onnx_tf.backend.prepare(onnx_model)
          tf_rep.export_graph(converted_model_path)

      import argparse
      _parser = argparse.ArgumentParser(prog='Convert to tensorflow saved model from onnx model', description='')
      _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 = convert_to_tensorflow_saved_model_from_onnx_model(**_parsed_args)
    args:
    - --model
    - {inputPath: model}
    - --converted-model
    - {outputPath: converted_model}
