def test_model_one_hot_encoder_int32(self): model = OneHotEncoder(categories='auto') data = numpy.array([[1, 2, 3], [4, 3, 0], [0, 1, 4], [0, 5, 6]], dtype=numpy.int32) model.fit(data) model_onnx = convert_sklearn( model, "scikit-learn one-hot encoder", [("input", Int32TensorType([None, 3]))], target_opset=TARGET_OPSET) str_model_onnx = str(model_onnx) assert "int64_data" in str_model_onnx self.assertTrue(model_onnx is not None) dump_data_and_model( data, model, model_onnx, basename="SklearnOneHotEncoderInt32-SkipDim1")
def ptype2vttype(it, shape): if it == TensorProto.FLOAT: # pylint: disable=E1101 return FloatTensorType(shape) if it == TensorProto.DOUBLE: # pylint: disable=E1101 return DoubleTensorType(shape) if it == TensorProto.INT64: # pylint: disable=E1101 return Int64TensorType(shape) if it == TensorProto.INT32: # pylint: disable=E1101 return Int32TensorType(shape) if it == TensorProto.BOOL: # pylint: disable=E1101 return BooleanTensorType(shape) if it == TensorProto.STRING: # pylint: disable=E1101 return StringTensorType(shape) raise NotImplementedError( # pragma: no cover "Unrecognized proto type {} with shape {}".format(it, shape))
def test_model_one_hot_encoder_int32_scaler(self): model = make_pipeline(OneHotEncoder(categories='auto', sparse=False), RobustScaler()) data = numpy.array([[1, 2, 3], [4, 3, 0], [0, 1, 4], [0, 5, 6]], dtype=numpy.int32) model.fit(data) model_onnx = convert_sklearn( model, "scikit-learn one-hot encoder", [("input", Int32TensorType([None, 3]))], ) str_model_onnx = str(model_onnx) assert "int64_data" in str_model_onnx self.assertTrue(model_onnx is not None) dump_data_and_model( data, model, model_onnx, basename="SklearnOneHotEncoderInt32Scaler-SkipDim1")
if len(sys.argv) < 5: print("usage: {} <scaler_model> <clf_model> <hmm_model> <output_model>". format(sys.argv[0])) exit(1) scaler_model_path = sys.argv[1] clf_model_path = sys.argv[2] hmm_model_path = sys.argv[3] onnx_model_path = sys.argv[4] scaler_model = pickle.load(open(scaler_model_path, "rb")) clf_model = pickle.load(open(clf_model_path, "rb")) hmm_model = pickle.load(open(hmm_model_path, "rb")) n_features = scaler_model.n_features_in_ skl2onnx_pipeline.register_converter() pipeline = skl2onnx_pipeline.Pipeline(scaler_model, clf_model, hmm_model) onnx_model = convert_sklearn(pipeline, name='pipeline', initial_types=[ ('input', FloatTensorType([None, n_features])) ], final_types=[('states', Int32TensorType([None])), ('probs', FloatTensorType([None]))]) onnx_model = postprocess_onnx_model(onnx_model) onnx.checker.check_model(onnx_model) with open(onnx_model_path, "wb") as f: f.write(onnx_model.SerializeToString())
print("Data wrangling complete!") print("Training model with processed microdata...") # get best classifier and train using all data ml_model = SVC(kernel="linear", C=0.025, probability=True, decision_function_shape='ovo') ml_model.fit(au_micro.loc[:, au_micro.columns != 'occupation'], au_micro.occupation) # export model in ONNX ml_onnx = convert_sklearn(ml_model, initial_types=[('int_input', Int32TensorType([None, 4]))]) with open("onnxModels/refugee_employment.onnx", "wb+") as f: f.write(ml_onnx.SerializeToString()) print("ONNX model exported!") # for work around with ONNX model issues # use a database with all input permutations and corresponding outputs # create a dataframe of all permutations au_inputs = pd.DataFrame(list( itertools.product(*[ au_micro.gender.unique(), au_micro.age_group_10y.unique(), au_micro.english_proficiency.unique(), au_micro.highest_education.unique() ])),
import sys import pickle import onnx from skl2onnx import convert_sklearn from skl2onnx.common.data_types import Int32TensorType import skl2onnx_hmm if len(sys.argv) < 3: print("usage: {} <input_model> <output_model>".format(sys.argv[0])) exit(1) hmmlearn_model_path = sys.argv[1] onnx_model_path = sys.argv[2] skl2onnx_hmm.register_converter() hmm_model = pickle.load(open(hmmlearn_model_path, "rb")) initial_types_hmm = [("input_states", Int32TensorType([None]))] output_types_hmm = [("output_states", None)] onx_hmm = convert_sklearn(hmm_model, name="hmm_model", initial_types=initial_types_hmm, final_types=output_types_hmm) onnx.checker.check_model(onx_hmm) with open(onnx_model_path, "wb") as f_output_model: f_output_model.write(onx_hmm.SerializeToString())