%3CLINGO-SUB%20id%3D%22lingo-sub-1770833%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20score%20ONNX%20models%20in%20Azure%20Data%20Explorer%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1770833%22%20slang%3D%22en-US%22%3E%3CP%3EUpdated%20the%20blog%20with%20a%20simplified%20process%20-%20as%20we%20added%20onnxruntime%20to%20ADX%20Python%20sandbox%20image%2C%20it%20can%20be%20imported%20directly.%20There%20is%20no%20need%20to%20download%2C%20pack%20and%20upload%20it%20to%20a%20blob%20container%20and%20access%20it%20via%20the%20external%20artifacts%20parameter.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1553482%22%20slang%3D%22en-US%22%3EHow%20to%20score%20ONNX%20models%20in%20Azure%20Data%20Explorer%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1553482%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22toc-hId-499716310%22%20id%3D%22toc-hId-499716310%22%3EIntroduction%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUpdated%3A%2012%20Oct%202020%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMachine%20Learning%20is%20widely%20used%20these%20days%20for%20various%20data%20driven%20tasks%20including%20detection%20of%20security%20threats%2C%20monitoring%20IoT%20devices%20for%20predictive%20maintenance%2C%20recommendation%20systems%2C%20financial%20analysis%20and%20many%20other%20domains.%20Most%20ML%20models%20are%20built%20and%20deployed%20in%20two%20steps%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EOffline%20training%3C%2FLI%3E%0A%3CLI%3EReal%20time%20scoring%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EML%20Training%3C%2FSTRONG%3E%20is%20done%20by%20researchers%2Fdata%20scientists.%20They%20fetch%20the%20training%20data%2C%20clean%20it%2C%20engineer%20features%2C%20try%20different%20models%20and%20tune%20parameters%2C%20repeating%20this%20cycle%20to%20improve%20the%20model%E2%80%99s%20quality%20and%20accuracy.%20This%20process%20is%20usually%20done%20using%20data%20science%20tools%20such%20as%20Jupyter%2C%20PyCharm%2C%20VS%20Code%2C%20Matlab%20etc.%20Once%20the%20model%20meets%20the%20required%20quality%20it%20is%20serialized%20and%20saved%20for%20scoring.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EML%20Scoring%3C%2FSTRONG%3E%20is%20the%20process%20of%20applying%20the%20model%20on%20new%20data%20to%20get%20insights%20and%20predictions.%20This%20is%20actually%20the%20business%20goal%20for%20building%20the%20model.%20Scoring%20usually%20needs%20to%20be%20done%20at%20scale%20with%20minimal%20latency%2C%20processing%20large%20sets%20of%20new%20records.%20For%20ADX%20users%2C%20the%20best%20solution%20for%20scoring%20is%20directly%20in%20ADX.%20ADX%20scoring%20is%20done%20on%20its%20compute%20nodes%2C%20in%20distributed%20manner%20near%20the%20data%2C%20thus%20achieving%20the%20best%20performance%20with%20minimal%20latency.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20many%20types%20of%20models%20such%20as%20Bayesian%20models%2C%20decision%20trees%20and%20forests%2C%20regressions%2C%20deep%20neural%20networks%20and%20many%20more.%20These%20models%20can%20be%20built%20by%20various%20frameworks%20and%2For%20packages%20like%20Scikit-learn%2C%20Tensorflow%2C%20CNTK%2C%20Keras%2C%20Caffe2%2C%20PyTorch%20etc.%20(%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FOutline_of_machine_learning%23Machine_learning_tools%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%20is%20a%20nice%20overview%20of%20ML%20algorithms%2C%20tools%20and%20frameworks).%20On%20one%20hand%20this%20variety%20is%20very%20good%20%E2%80%93%20you%20can%20find%20the%20most%20convenient%20algorithm%20and%20framework%20for%20your%20scenario%2C%20but%20on%20the%20other%20hand%20it%20creates%20an%20interoperability%20issue%2C%20as%20usually%20the%20ML%20scoring%20is%20done%20on%20infrastructure%20which%20is%20different%20from%20the%20one%20used%20for%20the%20training.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20resolve%20it%2C%20Microsoft%20and%20Facebook%20introduced%20in%202017%20%3CA%20href%3D%22https%3A%2F%2Fonnx.ai%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EONNX%3C%2FA%3E%2C%20Open%20Neural%20Network%20Exchange%2C%20that%20was%20adopted%20by%20many%20companies%20including%20AWS%2C%20IBM%2C%20Intel%2C%20Baidu%2C%20Mathworks%2C%20NVIDIA%20and%20many%20more.%20ONNX%20is%20a%20system%20for%20representation%20and%20serialization%20of%20ML%20models%20to%20a%20common%20file%20format.%20This%20format%20enables%20smooth%20switching%20among%20ML%20frameworks%20as%20well%20as%20allowing%20hardware%20vendors%20and%20others%20to%20improve%20the%20performance%20of%20deep%20neural%20networks%20for%20multiple%20frameworks%20at%20once%20by%20targeting%20the%20ONNX%20representation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20blog%20we%20explain%20how%20ADX%20can%20consume%20ONNX%20models%2C%20that%20were%20built%20and%20trained%20externally%2C%20for%20near%20real%20time%20scoring%20of%20new%20samples%20that%20are%20ingested%20into%20ADX.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3EHow%20to%20use%20ADX%20for%20scoring%20ONNX%20models%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EADX%20supports%20running%20Python%20code%20embedded%20in%20Kusto%20Query%20Language%20(KQL)%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fpythonplugin%3Fpivots%3Dazuredataexplorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epython()%3C%2FA%3E%20plugin.%26nbsp%3B%20The%20Python%20code%20is%20run%20in%20multiple%20sandboxes%20on%20ADX%20existing%20compute%20nodes.%20The%20Python%20image%20is%20based%20on%20Anaconda%20distribution%20and%20contains%20the%20most%20common%20ML%20frameworks%20including%20Scikit-learn%2C%20TensorFlow%2C%20Keras%20and%20PyTorch.%20To%20score%20ONNX%20models%20in%20ADX%20follow%20these%20steps%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EDevelop%20your%20ML%20model%20using%20your%20favorite%20framework%20and%20tools%3C%2FLI%3E%0A%3CLI%3EConvert%20the%20final%20trained%20model%20to%20ONNX%20format%3C%2FLI%3E%0A%3CLI%3EExport%20the%20ONNX%20model%20to%20a%20table%20on%20ADX%20or%20to%20an%20Azure%20blob%3C%2FLI%3E%0A%3CLI%3EScore%20new%20data%20in%20ADX%20using%20the%20inline%20%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fpythonplugin%3Fpivots%3Dazuredataexplorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epython()%3C%2FA%3E%20plugin%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH1%20id%3D%22toc-hId--1307738153%22%20id%3D%22toc-hId--1307738153%22%3EExample%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20build%20a%20model%20to%20predict%20room%20occupancy%20based%20on%20%3CA%20href%3D%22https%3A%2F%2Farchive.ics.uci.edu%2Fml%2Fdatasets%2FOccupancy%2BDetection%2B%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3EOccupancy%20Detection%3C%2FSTRONG%3E%3C%2FA%3E%20data%2C%20a%20public%20dataset%20from%20UCI%20Repository.%20This%20model%20is%20a%20binary%20classifier%20to%20predict%20occupied%2Fempty%20room%20based%20on%20Temperature%2C%20Humidity%2C%20Light%20and%20CO2%20sensors%20measurements.%20The%20complete%20process%20can%20be%20found%20in%20this%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-kusto-analytics-lib%2Fblob%2Fmaster%2FML%2Fnotebooks%2FONNX-ADX-Occupancy-Prediction-Pub.ipynb%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Jupyter%20notebook%3C%2FA%3E.%20Here%20we%20embed%20few%20snips%20just%20to%20present%20the%20main%20concepts%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--617176679%22%20id%3D%22toc-hId--617176679%22%3EPrerequisite%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EEnable%20Python%20plugin%20on%20your%20ADX%20cluster%20(see%20the%20Onboarding%20section%20of%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fpythonplugin%3Fpivots%3Dazuredataexplorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3Epython()%3C%2FSTRONG%3E%3C%2FA%3E%20plugin%20doc)%3C%2FLI%3E%0A%3CLI%3EWhitelist%20a%20blob%20container%20to%20be%20accessible%20by%20ADX%20Python%20sandbox%20(see%20the%20Appendix%20section%20of%20that%20doc)%3C%2FLI%3E%0A%3CLI%3ECreate%20a%20Python%20environment%20(conda%20or%20virtual%20env)%20that%20reflects%20the%20Python%20sandbox%20image%3C%2FLI%3E%0A%3CLI%3EInstall%20in%20that%20environment%20ONNX%20packages%3A%20onnxruntime%20and%20skl2onnx%20packages%3C%2FLI%3E%0A%3CLI%3EInstall%20in%20that%20environment%20Azure%20Blob%20Storage%20package%3A%20azure-storage-blob%3C%2FLI%3E%0A%3CLI%3EInstall%20KqlMagic%20to%20easily%20connect%20and%20query%20ADX%20cluster%20from%20Jupyter%20notebooks%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId-1870336154%22%20id%3D%22toc-hId-1870336154%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-62881691%22%20id%3D%22toc-hId-62881691%22%3E%3CSPAN%3ERetrieve%20and%20explore%20the%20data%20using%20KqlMagic%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Ereload_ext%20Kqlmagic%0A%25config%20Kqlmagic.auto_dataframe%20%3D%20True%0A%25kql%20kusto%3A%2F%2Fcode%3Bcluster%3D'demo11.westus'%3Bdatabase%3D'ML'%20-try_azcli_login%0A%25kql%20df%20%26lt%3B%26lt%3B%20OccupancyDetection%0Adf%5B-4%3A%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ETimestamp%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%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2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3E20559%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E2015-02-18%2009%3A19%3A00.0000000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E21.000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E28.1000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E409.00%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1864.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E0.004321%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH2%20id%3D%22toc-hId--1744572772%22%20id%3D%22toc-hId--1744572772%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-742940061%22%20id%3D%22toc-hId-742940061%22%3ETrain%20your%20model%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESplit%20the%20data%20to%20features%20(x)%2C%20labels%20(y)%20and%20for%20training%2Ftesting%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Etrain_x%20%3D%20df%5Bdf%5B'Test'%5D%20%3D%3D%20False%5D%5B%5B'Temperature'%2C%20'Humidity'%2C%20'Light'%2C%20'CO2'%2C%20'HumidityRatio'%5D%5D%0Atrain_y%20%3D%20df%5Bdf%5B'Test'%5D%20%3D%3D%20False%5D%5B'Occupancy'%5D%0Atest_x%20%3D%20df%5Bdf%5B'Test'%5D%20%3D%3D%20True%5D%5B%5B'Temperature'%2C%20'Humidity'%2C%20'Light'%2C%20'CO2'%2C%20'HumidityRatio'%5D%5D%0Atest_y%20%3D%20df%5Bdf%5B'Test'%5D%20%3D%3D%20True%5D%5B'Occupancy'%5D%0A%0Aprint(train_x.shape%2C%20train_y.shape%2C%20test_x.shape%2C%20test_y.shape)%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E(8143%2C%205)%20(8143%2C)%20(12417%2C%205)%20(12417%2C)%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ETrain%20few%20classic%20models%20from%20Scikit-learn%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Efrom%20sklearn%20import%20tree%0Afrom%20sklearn%20import%20neighbors%0Afrom%20sklearn%20import%20naive_bayes%0Afrom%20sklearn.linear_model%20import%20LogisticRegression%0Afrom%20sklearn.model_selection%20import%20cross_val_score%0A%0A%23four%20classifier%20types%0Aclf1%20%3D%20tree.DecisionTreeClassifier()%0Aclf2%20%3D%20LogisticRegression(solver%3D'liblinear')%0Aclf3%20%3D%20neighbors.KNeighborsClassifier()%0Aclf4%20%3D%20naive_bayes.GaussianNB()%0A%0Aclf1%20%3D%20clf1.fit(train_x%2C%20train_y)%0Aclf2%20%3D%20clf2.fit(train_x%2C%20train_y)%0Aclf3%20%3D%20clf3.fit(train_x%2C%20train_y)%0Aclf4%20%3D%20clf4.fit(train_x%2C%20train_y)%0A%0A%23%20Accuracy%20on%20Testing%20set%0A%0Afor%20clf%2C%20label%20in%20zip(%5Bclf1%2C%20clf2%2C%20clf3%2C%20clf4%5D%2C%20%5B'Decision%20Tree'%2C%20'Logistic%20Regression'%2C%20'K%20Nearest%20Neighbour'%2C%20'Naive%20Bayes'%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20scores%20%3D%20cross_val_score(clf%2C%20test_x%2C%20test_y%2C%20cv%3D5%2C%20scoring%3D'accuracy')%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Accuracy%3A%20%250.4f%20(%2B%2F-%20%250.4f)%20%5B%25s%5D%22%20%25%20(scores.mean()%2C%20scores.std()%2C%20label))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAccuracy%3A%200.8605%20(%2B%2F-%200.1130)%20%5BDecision%20Tree%5D%3CBR%20%2F%3EAccuracy%3A%200.9887%20(%2B%2F-%200.0071)%20%5BLogistic%20Regression%5D%3CBR%20%2F%3EAccuracy%3A%200.9656%20(%2B%2F-%200.0224)%20%5BK%20Nearest%20Neighbour%5D%3CBR%20%2F%3EAccuracy%3A%200.8893%20(%2B%2F-%200.1265)%20%5BNaive%20Bayes%5D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20logistic%20regression%20model%20is%20the%20best%20one%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1064514402%22%20id%3D%22toc-hId--1064514402%22%3EConvert%20the%20model%20to%20ONNX%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Efrom%20skl2onnx%20import%20convert_sklearn%0Afrom%20skl2onnx.common.data_types%20import%20FloatTensorType%0A%0A%23%20We%20define%20the%20input%20type%20(5%20sensors%20readings)%2C%20convert%20the%20scikit-learn%20model%20to%20ONNX%20and%20serialize%20it%0A%0Ainitial_type%20%3D%20%5B('float_input'%2C%20FloatTensorType(%5BNone%2C%205%5D))%5D%0Aonnx_model%20%3D%20convert_sklearn(clf2%2C%20initial_types%3Dinitial_type)%0Abmodel%20%3D%20onnx_model.SerializeToString()%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1422998431%22%20id%3D%22toc-hId-1422998431%22%3ETest%20ONNX%20Model%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EPredict%20using%20ONNX%20runtime%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20numpy%20as%20np%0Aimport%20onnxruntime%20as%20rt%0A%0Asess%20%3D%20rt.InferenceSession(bmodel)%0Ainput_name%20%3D%20sess.get_inputs()%5B0%5D.name%0Alabel_name%20%3D%20sess.get_outputs()%5B0%5D.name%0Apred_onnx%20%3D%20sess.run(%5Blabel_name%5D%2C%20%7Binput_name%3A%20test_x.values.astype(np.float32)%7D)%5B0%5D%0A%0A%23%20Verify%20ONNX%20and%20Scikit-learn%20predictions%20are%20same%0A%0Apred_clf2%20%3D%20clf2.predict(test_x)%0Adiff_num%20%3D%20(pred_onnx%20!%3D%20pred_clf2).sum()%0A%0Aif%20diff_num%3A%0A%20%20%20%20print(f'Predictions%20difference%20between%20sklearn%20and%20onnxruntime%2C%20total%20%7Bdiff_num%7D%20elements%20differ')%0Aelse%3A%0A%20%20%20%20print('Same%20prediction%20using%20sklearn%20and%20onnxruntime')%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESame%20prediction%20using%20sklearn%20and%20onnxruntime%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--384456032%22%20id%3D%22toc-hId--384456032%22%3EScoring%20in%20ADX%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%202%20options%20for%20retrieving%20the%20model%20for%20scoring%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Eserialize%20the%20model%20to%20a%20string%20to%20be%20stored%20in%20a%20standard%20table%20in%20ADX%3C%2FLI%3E%0A%3CLI%3Ecopy%20the%20model%20to%20a%20blob%20container%20(that%20was%20previously%20whitelisted%20for%20access%20by%20ADX%20Python%20sandbox)%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--518140756%22%20id%3D%22toc-hId--518140756%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1969372077%22%20id%3D%22toc-hId-1969372077%22%3EScoring%20from%20a%20serialized%20model%20which%20is%20stored%20in%20ADX%20table%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESerializing%20the%20model%20and%20store%20it%20in%20ADX%20models%20table%20using%20KqlMagic%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20pandas%20as%20pd%0Aimport%20datetime%0A%0Amodels_tbl%20%3D%20'ML_Models'%0Amodel_name%20%3D%20'ONNX-Occupancy'%0A%0Asmodel%20%3D%20bmodel.hex()%0Anow%20%3D%20datetime.datetime.now()%0Adfm%20%3D%20pd.DataFrame(%7B'name'%3A%5Bmodel_name%5D%2C%20'timestamp'%3A%5Bnow%5D%2C%20'model'%3A%5Bsmodel%5D%7D)%0Adfm%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3Ename%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3Etimestamp%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3Emodel%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3E0%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3EONNX-Occupancy%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E2020-07-28%2017%3A07%3A20.280040%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E08031208736b6c326f6e6e781a05312e362e3122076169...%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eset_query%20%3D%20'''%0A.set-or-append%20%7B0%7D%20%26lt%3B%7C%0Alet%20tbl%20%3D%20dfm%3B%0Atbl%0A'''.format(models_tbl)%0Aprint(set_query)%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E.set-or-append%20ML_Models%20%26lt%3B%7C%3CBR%20%2F%3Elet%20tbl%20%3D%20dfm%3B%3CBR%20%2F%3Etbl%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%25kql%20-query%20set_query%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EExtentId%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EOriginalSize%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EExtentSize%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ECompressedSize%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EIndexSize%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ERowCount%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3E0%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3Ebfc9acc2-3d79-4e64-9a79-d2681547e43d%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1430.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1490.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1040.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E450.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3EScoring%20from%20serialized%20model%20which%20is%20stored%20in%20ADX%20table%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20NOTE%3A%0A%23%0A%23%20We%20run%20ADX%20scoring%20query%20here%20using%20KqlMagic%20by%20embedding%20the%20query%20from%20Kusto%20Explorer%0A%23%20with%20r'''Kusto%20Explorer%20query'''%3A%0A%0Ascoring_from_table_query%20%3D%20r'''%0Alet%20predict_onnx_fl%3D(samples%3A(*)%2C%20models_tbl%3A(name%3Astring%2C%20timestamp%3Adatetime%2C%20model%3Astring)%2C%20model_name%3Astring%2C%20features_cols%3Adynamic%2C%20pred_col%3Astring)%0A%7B%0A%20%20%20%20let%20model_str%20%3D%20toscalar(models_tbl%20%7C%20where%20name%20%3D%3D%20model_name%20%7C%20top%201%20by%20timestamp%20desc%20%7C%20project%20model)%3B%0A%20%20%20%20let%20kwargs%20%3D%20pack('smodel'%2C%20model_str%2C%20'features_cols'%2C%20features_cols%2C%20'pred_col'%2C%20pred_col)%3B%0A%20%20%20%20let%20code%20%3D%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'import%20pickle%5Cn'%0A%20%20%20%20'import%20binascii%5Cn'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'smodel%20%3D%20kargs%5B%22smodel%22%5D%5Cn'%0A%20%20%20%20'features_cols%20%3D%20kargs%5B%22features_cols%22%5D%5Cn'%0A%20%20%20%20'pred_col%20%3D%20kargs%5B%22pred_col%22%5D%5Cn'%0A%20%20%20%20'bmodel%20%3D%20binascii.unhexlify(smodel)%5Cn'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'features_cols%20%3D%20kargs%5B%22features_cols%22%5D%5Cn'%0A%20%20%20%20'pred_col%20%3D%20kargs%5B%22pred_col%22%5D%5Cn'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'import%20onnxruntime%20as%20rt%5Cn'%0A%20%20%20%20'sess%20%3D%20rt.InferenceSession(bmodel)%5Cn'%0A%20%20%20%20'input_name%20%3D%20sess.get_inputs()%5B0%5D.name%5Cn'%0A%20%20%20%20'label_name%20%3D%20sess.get_outputs()%5B0%5D.name%5Cn'%0A%20%20%20%20'df1%20%3D%20df%5Bfeatures_cols%5D%5Cn'%0A%20%20%20%20'predictions%20%3D%20sess.run(%5Blabel_name%5D%2C%20%7Binput_name%3A%20df1.values.astype(np.float32)%7D)%5B0%5D%5Cn'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'result%20%3D%20df%5Cn'%0A%20%20%20%20'result%5Bpred_col%5D%20%3D%20pd.DataFrame(predictions%2C%20columns%3D%5Bpred_col%5D)'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20%3B%0A%20%20%20%20samples%20%7C%20evaluate%20python(typeof(*)%2C%20code%2C%20kwargs)%0A%7D%3B%0AOccupancyDetection%20%0A%7C%20where%20Test%20%3D%3D%201%0A%7C%20extend%20pred_Occupancy%3Dbool(0)%0A%7C%20invoke%20predict_onnx_fl(ML_Models%2C%20'ONNX-Occupancy'%2C%20pack_array('Temperature'%2C%20'Humidity'%2C%20'Light'%2C%20'CO2'%2C%20'HumidityRatio')%2C%20'pred_Occupancy')%0A'''%0A%0A%25kql%20pred_df%20%26lt%3B%26lt%3B%20-query%20scoring_from_table_query%0A%0Apred_df%5B-4%3A%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ETimestamp%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ETemperature%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EHumidity%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ELight%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ECO2%3C%2FSTRONG%3E%3C%2FP%3E%0A%3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E%3CSTRONG%3E12416%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E2015-02-18%2009%3A19%3A00%2B00%3A00%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E21.000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E28.1000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E409.00%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1864.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E0.004321%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eprint('Confusion%20Matrix')%0Apred_df.groupby(%5B'Occupancy'%2C%20'pred_Occupancy'%5D).size()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConfusion%20Matrix%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOccupancy%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20pred_Occupancy%3C%2FP%3E%0A%3CP%3EFalse%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20False%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%209284%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20True%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20112%3C%2FP%3E%0A%3CP%3ETrue%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20False%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%2015%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20True%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%203006%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20further%20details%20have%20a%20look%20at%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Ffunctions-library%2Fpredict-onnx-fl%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Epredict_onnx_fl()%3C%2FA%3E%20in%20ADX%20functions%20library%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-161917614%22%20id%3D%22toc-hId-161917614%22%3EScoring%20from%20model%20which%20is%20stored%20in%20blob%20storage%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECopy%20the%20model%20to%20blob%3C%2FP%3E%0A%3CP%3ENote%20again%20that%20the%20blob%20container%20should%20be%20whitelisted%20to%20be%20accessible%20by%20ADX%20Python%20sandbox%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Efrom%20azure.storage.blob%20import%20BlobClient%0A%0Aconn_str%20%3D%20%22BlobEndpoint%3Dhttps%3A%2F%2Fartifcatswestus.blob.core.windows.net%2Fkusto%3BSharedAccessSignature%3D%3F****%20YOUR%20SAS%20KEY%20****%22%0Ablob_client%20%3D%20BlobClient.from_connection_string(conn_str%2C%20container_name%3D%22ONNX%22%2C%20blob_name%3D%22room_occupancy.onnx%22)%0A%0Ares%20%3D%20blob_client.upload_blob(bmodel%2C%20overwrite%3DTrue)%3C%2FCODE%3E%3C%2FPRE%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20Notes%3A%0A%23%0A%23%20Replace%20the%20string%20%22****%20YOUR%20SAS%20KEY%20****%22%20below%20with%20the%20real%20SAS%0A%23%20We%20run%20ADX%20scoring%20query%20here%20using%20KqlMagic%20by%20embedding%20the%20query%20from%20Kusto%20Explorer%0A%23%20with%20r'''Kusto%20Explorer%20query'''%3A%0A%0Ascoring_from_blob_query%20%3D%20r'''%0Alet%20predict_onnx_blob_fl%3D(samples%3A(*)%2C%20model_sas%3Astring%2C%20features_cols%3Adynamic%2C%20pred_col%3Astring)%0A%7B%0A%20%20%20%20let%20kwargs%20%3D%20pack('features_cols'%2C%20features_cols%2C%20'pred_col'%2C%20pred_col)%3B%0A%20%20%20%20let%20code%20%3D%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'features_cols%20%3D%20kargs%5B%22features_cols%22%5D%5Cn'%0A%20%20%20%20'pred_col%20%3D%20kargs%5B%22pred_col%22%5D%5Cn'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'import%20onnxruntime%20as%20rt%5Cn'%0A%20%20%20%20'sess%20%3D%20rt.InferenceSession(r%22C%3A%5C%5CTemp%5C%5Cmodel.onnx%22)%5Cn'%0A%20%20%20%20'input_name%20%3D%20sess.get_inputs()%5B0%5D.name%5Cn'%0A%20%20%20%20'label_name%20%3D%20sess.get_outputs()%5B0%5D.name%5Cn'%0A%20%20%20%20'df1%20%3D%20df%5Bfeatures_cols%5D%5Cn'%0A%20%20%20%20'predictions%20%3D%20sess.run(%5Blabel_name%5D%2C%20%7Binput_name%3A%20df1.values.astype(np.float32)%7D)%5B0%5D%5Cn'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20'result%20%3D%20df%5Cn'%0A%20%20%20%20'result%5Bpred_col%5D%20%3D%20pd.DataFrame(predictions%2C%20columns%3D%5Bpred_col%5D)'%0A%20%20%20%20'%5Cn'%0A%20%20%20%20%3B%0A%20%20%20%20samples%20%7C%20evaluate%20python(typeof(*)%2C%20code%2C%20kwargs%2C%0A%20%20%20%20%20%20%20%20external_artifacts%3Dpack('model.onnx'%2C%20model_sas))%0A%7D%3B%0AOccupancyDetection%20%0A%7C%20where%20Test%20%3D%3D%201%0A%7C%20extend%20pred_Occupancy%3Dbool(0)%0A%7C%20invoke%20predict_onnx_blob_fl('https%3A%2F%2Fartifcatswestus.blob.core.windows.net%2Fkusto%2FONNX%2Froom_occupancy.onnx%3F****%20YOUR%20SAS%20KEY%20****'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pack_array('Temperature'%2C%20'Humidity'%2C%20'Light'%2C%20'CO2'%2C%20'HumidityRatio')%2C%20'pred_Occupancy')%0A'''%0A%0A%25kql%20pred_df%20%26lt%3B%26lt%3B%20-query%20scoring_from_blob_query%0A%0Apred_df%5B-4%3A%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ETimestamp%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ETemperature%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EHumidity%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ELight%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ECO2%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EHumidityRatio%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3EOccupancy%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CSTRONG%3ETest%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E%3CS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C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E1864.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3E0.004321%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%0A%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eprint('Confusion%20Matrix')%0Apred_df.groupby(%5B'Occupancy'%2C%20'pred_Occupancy'%5D).size()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConfusion%20Matrix%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOccupancy%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20pred_Occupancy%3C%2FP%3E%0A%3CP%3EFalse%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20False%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%209284%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20True%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20112%3C%2FP%3E%0A%3CP%3ETrue%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20False%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%2015%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20True%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%203006%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1645536849%22%20id%3D%22toc-hId--1645536849%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-841975984%22%20id%3D%22toc-hId-841975984%22%3ESummary%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20tutorial%20we%20showed%20how%20to%20train%20a%20model%20in%20Scikit-learn%2C%20convert%20it%20to%20ONNX%20format%20and%20export%20it%20to%20ADX%20for%20scoring.%20This%20workflow%20is%20convenient%20as%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ETraining%20can%20be%20done%20on%20any%20hardware%20platform%2C%20using%20any%20framework%20supporting%20ONNX%3C%2FLI%3E%0A%3CLI%3EScoring%20is%20done%20in%20ADX%20near%20the%20data%2C%20on%20the%20existing%20compute%20nodes%2C%20enabling%20near%20real%20time%20processing%20of%20big%20amounts%20of%20new%20data.%20There%20is%20no%20need%20to%20export%20the%20data%20to%20external%20scoring%20service%20and%20import%20back%20the%20results.%20Consequently%2C%20scoring%20architecture%20is%20simpler%20and%20performance%20is%20much%20faster%20and%20scalable%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1553482%22%20slang%3D%22en-US%22%3E%3CP%3EAre%20you%20training%20ML%20models%20using%20different%20frameworks%20and%20want%20to%20score%20streaming%20data%20in%20ADX%3F%20Just%20convert%20the%20model%20to%20ONNX%20format%20and%20export%20it%20to%20ADX.%20You%20can%20now%20score%20new%20data%20directly%20in%20ADX%2C%26nbsp%3B%20near%20the%20data%2C%20using%20ADX%20existing%20compute%20nodes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ADX-ONNX-runtime.png%22%20style%3D%22width%3A%20540px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F208850i396C21725FFD9678%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22ADX-ONNX-runtime.png%22%20alt%3D%22ADX-ONNX-runtime.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1553482%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eadx%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Data%20Explorer%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ekql%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EKusto%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMachine%20Learning%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eml%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eonnx%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Escore%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Etraining%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Introduction

 

Updated: 12 Oct 2020

 

Machine Learning is widely used these days for various data driven tasks including detection of security threats, monitoring IoT devices for predictive maintenance, recommendation systems, financial analysis and many other domains. Most ML models are built and deployed in two steps:

  • Offline training
  • Real time scoring

ML Training is done by researchers/data scientists. They fetch the training data, clean it, engineer features, try different models and tune parameters, repeating this cycle to improve the model’s quality and accuracy. This process is usually done using data science tools such as Jupyter, PyCharm, VS Code, Matlab etc. Once the model meets the required quality it is serialized and saved for scoring.

ML Scoring is the process of applying the model on new data to get insights and predictions. This is actually the business goal for building the model. Scoring usually needs to be done at scale with minimal latency, processing large sets of new records. For ADX users, the best solution for scoring is directly in ADX. ADX scoring is done on its compute nodes, in distributed manner near the data, thus achieving the best performance with minimal latency.

 

There are many types of models such as Bayesian models, decision trees and forests, regressions, deep neural networks and many more. These models can be built by various frameworks and/or packages like Scikit-learn, Tensorflow, CNTK, Keras, Caffe2, PyTorch etc. (here is a nice overview of ML algorithms, tools and frameworks). On one hand this variety is very good – you can find the most convenient algorithm and framework for your scenario, but on the other hand it creates an interoperability issue, as usually the ML scoring is done on infrastructure which is different from the one used for the training.

 

To resolve it, Microsoft and Facebook introduced in 2017 ONNX, Open Neural Network Exchange, that was adopted by many companies including AWS, IBM, Intel, Baidu, Mathworks, NVIDIA and many more. ONNX is a system for representation and serialization of ML models to a common file format. This format enables smooth switching among ML frameworks as well as allowing hardware vendors and others to improve the performance of deep neural networks for multiple frameworks at once by targeting the ONNX representation.

 

In this blog we explain how ADX can consume ONNX models, that were built and trained externally, for near real time scoring of new samples that are ingested into ADX.

 

How to use ADX for scoring ONNX models

 

ADX supports running Python code embedded in Kusto Query Language (KQL) using the python() plugin.  The Python code is run in multiple sandboxes on ADX existing compute nodes. The Python image is based on Anaconda distribution and contains the most common ML frameworks including Scikit-learn, TensorFlow, Keras and PyTorch. To score ONNX models in ADX follow these steps:

  1. Develop your ML model using your favorite framework and tools
  2. Convert the final trained model to ONNX format
  3. Export the ONNX model to a table on ADX or to an Azure blob
  4. Score new data in ADX using the inline  python() plugin

Example

 

We build a model to predict room occupancy based on Occupancy Detection data, a public dataset from UCI Repository. This model is a binary classifier to predict occupied/empty room based on Temperature, Humidity, Light and CO2 sensors measurements. The complete process can be found in this Jupyter notebook. Here we embed few snips just to present the main concepts

 

Prerequisite

 

  • Enable Python plugin on your ADX cluster (see the Onboarding section of the python() plugin doc)
  • Whitelist a blob container to be accessible by ADX Python sandbox (see the Appendix section of that doc)
  • Create a Python environment (conda or virtual env) that reflects the Python sandbox image
  • Install in that environment ONNX packages: onnxruntime and skl2onnx packages
  • Install in that environment Azure Blob Storage package: azure-storage-blob
  • Install KqlMagic to easily connect and query ADX cluster from Jupyter notebooks

 

Retrieve and explore the data using KqlMagic

 

 

 

reload_ext Kqlmagic
%config Kqlmagic.auto_dataframe = True
%kql kusto://code;cluster='demo11.westus';database='ML' -try_azcli_login
%kql df << OccupancyDetection
df[-4:]

 

 

 

 

Timestamp

Temperature

Humidity

Light

CO2

HumidityRatio

Occupancy

Test

20556

2015-02-18 09:16:00.0000000

20.865

27.7450

423.50

1514.5

0.004230

True

True

20557

2015-02-18 09:16:00.0000000

20.890

27.7450

423.50

1521.5

0.004237

True

True

20558

2015-02-18 09:17:00.0000000

20.890

28.0225

418.75

1632.0

0.004279

True

True

20559

2015-02-18 09:19:00.0000000

21.000

28.1000

409.00

1864.0

0.004321

True

True

 

Train your model

 

Split the data to features (x), labels (y) and for training/testing:

 

 

 

train_x = df[df['Test'] == False][['Temperature', 'Humidity', 'Light', 'CO2', 'HumidityRatio']]
train_y = df[df['Test'] == False]['Occupancy']
test_x = df[df['Test'] == True][['Temperature', 'Humidity', 'Light', 'CO2', 'HumidityRatio']]
test_y = df[df['Test'] == True]['Occupancy']

print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)

 

 

 

(8143, 5) (8143,) (12417, 5) (12417,)

 

Train few classic models from Scikit-learn:

 

 

 

from sklearn import tree
from sklearn import neighbors
from sklearn import naive_bayes
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

#four classifier types
clf1 = tree.DecisionTreeClassifier()
clf2 = LogisticRegression(solver='liblinear')
clf3 = neighbors.KNeighborsClassifier()
clf4 = naive_bayes.GaussianNB()

clf1 = clf1.fit(train_x, train_y)
clf2 = clf2.fit(train_x, train_y)
clf3 = clf3.fit(train_x, train_y)
clf4 = clf4.fit(train_x, train_y)

# Accuracy on Testing set

for clf, label in zip([clf1, clf2, clf3, clf4], ['Decision Tree', 'Logistic Regression', 'K Nearest Neighbour', 'Naive Bayes']):
            scores = cross_val_score(clf, test_x, test_y, cv=5, scoring='accuracy')
            print("Accuracy: %0.4f (+/- %0.4f) [%s]" % (scores.mean(), scores.std(), label))

 

 

 

Accuracy: 0.8605 (+/- 0.1130) [Decision Tree]
Accuracy: 0.9887 (+/- 0.0071) [Logistic Regression]
Accuracy: 0.9656 (+/- 0.0224) [K Nearest Neighbour]
Accuracy: 0.8893 (+/- 0.1265) [Naive Bayes]

 

The logistic regression model is the best one 

 

Convert the model to ONNX

 

 

 

from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

# We define the input type (5 sensors readings), convert the scikit-learn model to ONNX and serialize it

initial_type = [('float_input', FloatTensorType([None, 5]))]
onnx_model = convert_sklearn(clf2, initial_types=initial_type)
bmodel = onnx_model.SerializeToString()

 

 

 

Test ONNX Model

Predict using ONNX runtime

 

 

 

import numpy as np
import onnxruntime as rt

sess = rt.InferenceSession(bmodel)
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
pred_onnx = sess.run([label_name], {input_name: test_x.values.astype(np.float32)})[0]

# Verify ONNX and Scikit-learn predictions are same

pred_clf2 = clf2.predict(test_x)
diff_num = (pred_onnx != pred_clf2).sum()

if diff_num:
    print(f'Predictions difference between sklearn and onnxruntime, total {diff_num} elements differ')
else:
    print('Same prediction using sklearn and onnxruntime')

 

 

 

Same prediction using sklearn and onnxruntime

 

Scoring in ADX

 

There are 2 options for retrieving the model for scoring:

  • serialize the model to a string to be stored in a standard table in ADX
  • copy the model to a blob container (that was previously whitelisted for access by ADX Python sandbox)

 

Scoring from a serialized model which is stored in ADX table

 

Serializing the model and store it in ADX models table using KqlMagic

 

 

 

import pandas as pd
import datetime

models_tbl = 'ML_Models'
model_name = 'ONNX-Occupancy'

smodel = bmodel.hex()
now = datetime.datetime.now()
dfm = pd.DataFrame({'name':[model_name], 'timestamp':[now], 'model':[smodel]})
dfm

 

 

 

 

name

timestamp

model

0

ONNX-Occupancy

2020-07-28 17:07:20.280040

08031208736b6c326f6e6e781a05312e362e3122076169...

 

 

 

set_query = '''
.set-or-append {0} <|
let tbl = dfm;
tbl
'''.format(models_tbl)
print(set_query)

 

 

 

.set-or-append ML_Models <|
let tbl = dfm;
tbl

 

 

 

%kql -query set_query

 

 

 

 

ExtentId

OriginalSize

ExtentSize

CompressedSize

IndexSize

RowCount

0

bfc9acc2-3d79-4e64-9a79-d2681547e43d

1430.0

1490.0

1040.0

450.0

1

Scoring from serialized model which is stored in ADX table

 

 

# NOTE:
#
# We run ADX scoring query here using KqlMagic by embedding the query from Kusto Explorer
# with r'''Kusto Explorer query''':

scoring_from_table_query = r'''
let predict_onnx_fl=(samples:(*), models_tbl:(name:string, timestamp:datetime, model:string), model_name:string, features_cols:dynamic, pred_col:string)
{
    let model_str = toscalar(models_tbl | where name == model_name | top 1 by timestamp desc | project model);
    let kwargs = pack('smodel', model_str, 'features_cols', features_cols, 'pred_col', pred_col);
    let code =
    '\n'
    'import pickle\n'
    'import binascii\n'
    '\n'
    'smodel = kargs["smodel"]\n'
    'features_cols = kargs["features_cols"]\n'
    'pred_col = kargs["pred_col"]\n'
    'bmodel = binascii.unhexlify(smodel)\n'
    '\n'
    'features_cols = kargs["features_cols"]\n'
    'pred_col = kargs["pred_col"]\n'
    '\n'
    'import onnxruntime as rt\n'
    'sess = rt.InferenceSession(bmodel)\n'
    'input_name = sess.get_inputs()[0].name\n'
    'label_name = sess.get_outputs()[0].name\n'
    'df1 = df[features_cols]\n'
    'predictions = sess.run([label_name], {input_name: df1.values.astype(np.float32)})[0]\n'
    '\n'
    'result = df\n'
    'result[pred_col] = pd.DataFrame(predictions, columns=[pred_col])'
    '\n'
    ;
    samples | evaluate python(typeof(*), code, kwargs)
};
OccupancyDetection 
| where Test == 1
| extend pred_Occupancy=bool(0)
| invoke predict_onnx_fl(ML_Models, 'ONNX-Occupancy', pack_array('Temperature', 'Humidity', 'Light', 'CO2', 'HumidityRatio'), 'pred_Occupancy')
'''

%kql pred_df << -query scoring_from_table_query

pred_df[-4:]

 

 

 

 

Timestamp

Temperature

Humidity

Light

CO2

HumidityRatio

Occupancy

Test

pred_Occupancy

12413

2015-02-18 09:16:00+00:00

20.865

27.7450

423.50

1514.5

0.004230

True

True

True

12414

2015-02-18 09:16:00+00:00

20.890

27.7450

423.50

1521.5

0.004237

True

True

True

12415

2015-02-18 09:17:00+00:00

20.890

28.0225

418.75

1632.0

0.004279

True

True

True

12416

2015-02-18 09:19:00+00:00

21.000

28.1000

409.00

1864.0

0.004321

True

True

True

 

 

 

print('Confusion Matrix')
pred_df.groupby(['Occupancy', 'pred_Occupancy']).size()

 

 

 

Confusion Matrix

 

Occupancy         pred_Occupancy

False                    False                                  9284

                            True                                   112

True                     False                                  15

                            True                                   3006

 

For further details have a look at predict_onnx_fl() in ADX functions library 

 

Scoring from model which is stored in blob storage

 

Copy the model to blob

Note again that the blob container should be whitelisted to be accessible by ADX Python sandbox

 

 

 

from azure.storage.blob import BlobClient

conn_str = "BlobEndpoint=https://artifcatswestus.blob.core.windows.net/kusto;SharedAccessSignature=?**** YOUR SAS KEY ****"
blob_client = BlobClient.from_connection_string(conn_str, container_name="ONNX", blob_name="room_occupancy.onnx")

res = blob_client.upload_blob(bmodel, overwrite=True)
# Notes:
#
# Replace the string "**** YOUR SAS KEY ****" below with the real SAS
# We run ADX scoring query here using KqlMagic by embedding the query from Kusto Explorer
# with r'''Kusto Explorer query''':

scoring_from_blob_query = r'''
let predict_onnx_blob_fl=(samples:(*), model_sas:string, features_cols:dynamic, pred_col:string)
{
    let kwargs = pack('features_cols', features_cols, 'pred_col', pred_col);
    let code =
    '\n'
    'features_cols = kargs["features_cols"]\n'
    'pred_col = kargs["pred_col"]\n'
    '\n'
    'import onnxruntime as rt\n'
    'sess = rt.InferenceSession(r"C:\\Temp\\model.onnx")\n'
    'input_name = sess.get_inputs()[0].name\n'
    'label_name = sess.get_outputs()[0].name\n'
    'df1 = df[features_cols]\n'
    'predictions = sess.run([label_name], {input_name: df1.values.astype(np.float32)})[0]\n'
    '\n'
    'result = df\n'
    'result[pred_col] = pd.DataFrame(predictions, columns=[pred_col])'
    '\n'
    ;
    samples | evaluate python(typeof(*), code, kwargs,
        external_artifacts=pack('model.onnx', model_sas))
};
OccupancyDetection 
| where Test == 1
| extend pred_Occupancy=bool(0)
| invoke predict_onnx_blob_fl('https://artifcatswestus.blob.core.windows.net/kusto/ONNX/room_occupancy.onnx?**** YOUR SAS KEY ****',
                     pack_array('Temperature', 'Humidity', 'Light', 'CO2', 'HumidityRatio'), 'pred_Occupancy')
'''

%kql pred_df << -query scoring_from_blob_query

pred_df[-4:]

 

 

 

 

Timestamp

Temperature

Humidity

Light

CO2

HumidityRatio

Occupancy

Test

pred_Occupancy

12413

2015-02-18 09:16:00+00:00

20.865

27.7450

423.50

1514.5

0.004230

True

True

True

12414

2015-02-18 09:16:00+00:00

20.890

27.7450

423.50

1521.5

0.004237

True

True

True

12415

2015-02-18 09:17:00+00:00

20.890

28.0225

418.75

1632.0

0.004279

True

True

True

12416

2015-02-18 09:19:00+00:00

21.000

28.1000

409.00

1864.0

0.004321

True

True

True

 

 

 

print('Confusion Matrix')
pred_df.groupby(['Occupancy', 'pred_Occupancy']).size()

 

 

 

Confusion Matrix

 

Occupancy         pred_Occupancy

False                    False                                  9284

                            True                                   112

True                     False                                  15

                            True                                   3006

 

Summary

 

In this tutorial we showed how to train a model in Scikit-learn, convert it to ONNX format and export it to ADX for scoring. This workflow is convenient as

  • Training can be done on any hardware platform, using any framework supporting ONNX
  • Scoring is done in ADX near the data, on the existing compute nodes, enabling near real time processing of big amounts of new data. There is no need to export the data to external scoring service and import back the results. Consequently, scoring architecture is simpler and performance is much faster and scalable
1 Comment
Microsoft

Updated the blog with a simplified process - as we added onnxruntime to ADX Python sandbox image, it can be imported directly. There is no need to download, pack and upload it to a blob container and access it via the external artifacts parameter.