def test_save_model(self): this = os.path.dirname(__file__) onnx_file = os.path.join(this, "models", "coreml_OneHotEncoder_BikeSharing.onnx") new_onnx_file = os.path.join(this, "models", "coreml_OneHotEncoder_BikeSharing2.onnx") onnx_model = load_model(onnx_file) save_model(onnx_model, new_onnx_file) self.assertTrue(os.path.exists(new_onnx_file))
def save(self): # Onnx Save (can't save a list of model for now) onx = convert_sklearn(self.pipe, 'Pipe', [('input', StringTensorType([1, 1]))]) save_model(onx, "Model.onnx") print ("Model saved")
def convert_model_from_float_to_float16(model_path): # from onnxmltools.utils.float16_converter import convert_float_to_float16 from onnxmltools.utils import load_model, save_model from float16 import convert_float_to_float16 onnx_model = load_model(model_path) new_onnx_model = convert_float_to_float16(onnx_model) save_model(new_onnx_model, 'new_fp16_model.onnx') return os.path.join(os.getcwd(), "new_fp16_model.onnx")
def export(self, output_dir): onnx_model = convert_sklearn(self.pipe, initial_types=[("input", FloatTensorType([1, 1]))]) save_model(onnx_model, path.join(output_dir, f'{self.name()}.onnx'))
X_test.head() #%% # Make a CSV backup of our test data without column names or indexes test_data = X_test.copy() test_data['target'] = y_test test_data.to_csv("test_data.csv", header=False, index=False) #%% test_data.head() #%% model = xgbr() model.fit(X_train, y_train, eval_set=[(X_test, y_test)]) #%% conv_model = convert_xgboost(model, initial_types=[('float_input', FloatTensorType(shape=[1, 4]))]) assert(conv_model is not None) save_model(conv_model, 'model.onnx') #%%
`keras <https://keras.io/>`_, `tensorflow <https://www.tensorflow.org/>`_, `onnxmltools <https://pypi.org/project/onnxmltools/>`_ but then only *onnxruntime* is required to compute the predictions. """ import os if not os.path.exists('dense121.onnx'): from keras.applications.densenet import DenseNet121 model = DenseNet121(include_top=True, weights='imagenet') from onnxmltools import convert_keras onx = convert_keras(model, 'dense121.onnx') from onnxmltools.utils import save_model save_model(onx, "dense121.onnx") ################################## # Let's load an image (source: wikipedia). from keras.preprocessing.image import array_to_img, img_to_array, load_img img = load_img('Sannosawa1.jpg') ximg = img_to_array(img) import matplotlib.pyplot as plt plt.imshow(ximg / 255) plt.axis('off') ############################################# # Let's load the model with onnxruntime. import onnxruntime as rt
# Conversion to ONNX format # +++++++++++++++++++++++++ # # We use module # `onnxmltools <https://github.com/onnx/onnxmltools>`_ # to convert the model into ONNX format. from onnxmltools import convert_sklearn from onnxmltools.utils import save_model from onnxmltools.convert.common.data_types import FloatTensorType, Int64TensorType, DictionaryType, SequenceType # initial_type = [('float_input', DictionaryType(Int64TensorType([1]), FloatTensorType([])))] initial_type = [('float_input', DictionaryType(Int64TensorType([1]), FloatTensorType([])))] onx = convert_sklearn(pipe, initial_types=initial_type) save_model(onx, "pipeline_vectorize.onnx") ################################## # We load the model with ONNX Runtime and look at # its input and output. import onnxruntime as rt sess = rt.InferenceSession("pipeline_vectorize.onnx") import numpy inp, out = sess.get_inputs()[0], sess.get_outputs()[0] print("input name='{}' and shape={} and type={}".format( inp.name, inp.shape, inp.type)) print("output name='{}' and shape={} and type={}".format( out.name, out.shape, out.type)) ##################################