class_docstr: ```
  Abstract class to preprocess data
  ```
class_docstr_tok: ["``","`","Abstract","class","to","preprocess","data","``","`"]
docstr: ```
  Export model to onnx
  Args:
    fpath(str): String representing full path to model file where ONNX model will be saved.
                Example: '/tmp/my_model.onnx'
    quantize(str): If True, will create a total of three model files will be created using transformers.convert_graph_to_onnx:
                   1) ONNX model  (created directly using keras2onnx
                   2) an optimized ONNX model (created by transformers library)
                   3) a quantized version of optimized ONNX model (created by transformers library)
                   All files will be created in the parent folder of fpath:
                   Example:
                     If fpath='/tmp/model.onnx', then both /tmp/model-optimized.onnx and
                     /tmp/model-optimized-quantized.onnx will also be created.
    verbose(bool): verbosity
  Returns:
    str: string representing fpath.  If quantize=True, returned fpath will be different than supplied fpath
  ```
docstr_tok: ["``","`","Export","model","to","onnx","Args",":","fpath","(","str",")",":","String","representing","full","path","to","model","file","where","ONNX","model","will","be","saved",".","Example",":","'\/tmp\/my_model.onnx'","quantize","(","str",")",":","If","True",",","will","create","a","total","of","three","model","files","will","be","created","using","transformers.convert_graph_to_onnx",":","1",")","ONNX","model","(","created","directly","using","keras2onnx","2",")","an","optimized","ONNX","model","(","created","by","transformers","library",")","3",")","a","quantized","version","of","optimized","ONNX","model","(","created","by","transformers","library",")","All","files","will","be","created","in","the","parent","folder","of","fpath",":","Example",":","If","fpath='\/tmp\/model.onnx","'",",","then","both","\/tmp\/model-optimized.onnx","and","\/tmp\/model-optimized-quantized.onnx","will","also","be","created",".","verbose","(","bool",")",":","verbosity","Returns",":","str",":","string","representing","fpath",".","If","quantize=True",",","returned","fpath","will","be","different","than","supplied","fpath","``","`"]
code: def export_model_to_onnx(
      self, fpath, quantize=False, target_opset=None, verbose=1
  ):
      """
      ```
      Export model to onnx
      Args:
        fpath(str): String representing full path to model file where ONNX model will be saved.
                    Example: '/tmp/my_model.onnx'
        quantize(str): If True, will create a total of three model files will be created using transformers.convert_graph_to_onnx:
                       1) ONNX model  (created directly using keras2onnx
                       2) an optimized ONNX model (created by transformers library)
                       3) a quantized version of optimized ONNX model (created by transformers library)
                       All files will be created in the parent folder of fpath:
                       Example:
                         If fpath='/tmp/model.onnx', then both /tmp/model-optimized.onnx and
                         /tmp/model-optimized-quantized.onnx will also be created.
        verbose(bool): verbosity
      Returns:
        str: string representing fpath.  If quantize=True, returned fpath will be different than supplied fpath
      ```
      """
      try:
          import onnx
          import onnxruntime
      except ImportError:
          raise Exception(
              "This method requires ONNX libraries to be installed: "
              + "pip install -q --upgrade onnxruntime==1.10.0 onnx sympy tf2onnx"
          )
      from pathlib import Path
  
      if type(self.preproc).__name__ == "BERTPreprocessor":
          raise Exception(
              'currently_unsupported:  BERT models created with text_classifier("bert",...) are not supported (i.e., keras_bert models). '
              + "Only BERT models created with Transformer(...) are supported."
          )
  
      if verbose:
          print(
              "converting to ONNX format by way of TFLite ... this may take a few moments..."
          )
      if U.is_huggingface(model=self.model):
          tokenizer = self.preproc.get_tokenizer()
          maxlen = self.preproc.maxlen
          input_dict = tokenizer(
              "Name",
              return_tensors="tf",
              padding="max_length",
              max_length=maxlen,
          )
  
          if version.parse(tf.__version__) < version.parse("2.2"):
              raise Exception(
                  "export_model_to_tflite requires tensorflow>=2.2"
              )
              # self.model._set_inputs(input_spec, training=False) # for tf < 2.2
          self.model._saved_model_inputs_spec = None  # for tf > 2.2
          self.model._set_save_spec(input_dict)  # for tf > 2.2
          self.model._get_save_spec()
  
      # onnx_model = keras2onnx.convert_keras(self.model, self.model.name, target_opset=target_opset)
      # keras2onnx.save_model(onnx_model, fpath)
      tflite_model_path = self.export_model_to_tflite(
          fpath + "-TFLITE_TMP", verbose=verbose
      )
  
      import subprocess
  
      if verbose:
          print("converting to ONNX using tf2onnx...")
      proc = subprocess.run(
          f"python -m tf2onnx.convert --tflite {tflite_model_path} --output {fpath}".split(),
          stdout=subprocess.PIPE,
          stderr=subprocess.PIPE,
      )
      if verbose:
          print(proc.returncode)
          print(proc.stdout.decode("ascii"))
          print(proc.stderr.decode("ascii"))
      return_fpath = fpath
  
      if quantize:
          from transformers.convert_graph_to_onnx import (
              optimize,
              quantize,
          )
  
          # opt_path = optimize(Path(fpath))
  
          if U.is_huggingface(model=self.model) and type(
              self.model
          ).__name__ in [
              "TFDistilBertForSequenceClassification",
              "TFBertForSequenceClassification",
          ]:
              try:
                  from onnxruntime.transformers import optimizer
                  from onnxruntime.transformers.onnx_model_bert import (
                      BertOptimizationOptions,
                  )
  
                  # disable embedding layer norm optimization for better model size reduction
                  opt_options = BertOptimizationOptions("bert")
                  opt_options.enable_embed_layer_norm = False
                  opt_model = optimizer.optimize_model(
                      fpath,
                      "bert",  # bert_keras causes error with transformers
                      num_heads=12,
                      hidden_size=768,
                      optimization_options=opt_options,
                  )
                  opt_model.save_model_to_file(fpath)
              except:
                  warnings.warn(
                      "Could not run BERT-specific optimizations"
                  )
                  pass
          quantize_path = quantize(Path(fpath))
          return_fpath = quantize_path.as_posix()
      if verbose:
          print("done.")
      return return_fpath
  
file_path: ktrain/ktrain/predictor.py
imports: from .None import utils
  from .imports import *
label_desc: Use image node_id 9 for calling the Predictor obj's underlying member method code with example usage: obj.export_model_to_onnx(fpath, quantize, target_opset, verbose) and returns: return_fpath