Python API
Installation of the Python package
The Python package is stored as a whl file in the folder <TwinCatInstallDir>\Functions\TF38xx-Machine-Learning\Utilities\ModelManagerAPI\PythonPackage.
To install the package, use pip install
<TwinCatInstallDir>\Functions\TF38xx-Machine-Learning\Utilities\ModelManagerAPI\PythonPackage\<whl-file-name>. The folder may contain different versions of the package (only if you have installed a new setup on top of an old TwinCAT Machine Learning Setup). Make sure you always use the current version.
pip install "C:\TwinCAT\Functions\TF38xx-Machine-Learning\Utilities\ModelManagerAPI\PythonPackage\beckhoff_toolbox-3.1.230205-py3-none-any.whl"
Use of the Beckhoff toolbox
For a description of the individual points such as Custom Attributes and Model Description, see: Conversion from ONNX to XML and BML.
The following source code is also available as a py file. See: _Beckhoff_ONNX_Samples.zip in the PythonAPI_mllib directory.
The package is loaded with
import beckhoff.toolbox as tb
Conversion of an ONNX file to XML
tb.onnximport("../decision tree/decisiontree-regressor.onnx", "../decision tree/decisiontree-regressor.xml")
tb.onnximport("../decision tree/decisiontree-regressor.onnx", "../decision tree/decisiontree-regressor.bml")
Display of model information
tb.info("../decision tree/decisiontree-regressor.onnx")
tb.info("../decision tree/decisiontree-regressor.xml")
Add Custom Attributes
new_ca = { 'nID' : -34234, 'bTested' : True, 'fNum' : 324.3E-12, 'AnotherTreeItem' : { 'fPi' : 3.13412, 'bFalseFlag' : False }}
tb.modify_ca("../decision tree/decisiontree-regressor.xml","../decision tree/decisiontree-regressor-custom.xml", new_ca)
Add Model Description (name, version, author, etc. of a model)
model_description = {
"new_version" : "2.3.1.0",
"new_name" : "CurrentPreControlAxis42",
"new_desc":"This is the most awesome model to control Axis42",
"new_author":"Max",
"new_tags":"awesome, ingenious, astounding",
}
tb.modify_md("../decision tree/decisiontree-regressor-custom.xml", "../decision tree/decisiontree-regressor-md2.xml",
**model_description)
Add Input and Output Transformations
Output Transformations only for selected models Whereas an input transformation is possible for all AI models, output transformations can only be used for models of the regression type. |
# add input / output scalings (input-output-transformations)
tb.modify_iot("../decision tree/decisiontree-regressor-md.xml", "../decision tree/decisiontree-regressor-iot.xml",
tb.iot_scaled_offset([1.2,3.4,1.0,1.1,1.0,1.0,1.0,1.0,1.0,1.0],[1.2,3.4,1.0,1.1,1.0,1.0,1.0,1.0,1.0,1.0]),
None) # no output transformation
The function iot_scaled_offset
expects a list of offsets and scalings. A value must be specified for each input or output.
def iot_scaled_offset(offsets : list, scalings : list):
The number of inputs in the example above is 10, therefore a list of 10 elements is transferred. No output transformation is available for decision trees, therefore a None is transferred here.
Generate multi-engines
Merge several models into one XML file.
# # Create a Multi-Engine
# merge two XML files - output file is Merged.xml
tb.merge(['KerasMLPExample_sin.xml','KerasMLPExample_cos.xml'],'Merged.xml')
# merge two specific engines from two XML files
tb.merge([tb.input_engine('Merged.xml','mlp_fp32_engine::merge1'),
tb.input_engine('KerasMLPExample_cos.xml','mlp_fp32_engine')],
'DoubleMerged.xml')
# merge two specific engines from two XML files and provide a reference name for both engines in the target file
tb.merge([tb.input_engine('KerasMLPExample_sin.xml','mlp_fp32_engine', 'sine'),
tb.input_engine('KerasMLPExample_cos.xml','mlp_fp32_engine', 'cosine')],
'MergedRef.xml')
# extract a specific engine from a Multi-Engine file
tb.extract(tb.input_reference('MergedRef.xml', 'sine'),'Extract.xml')
Test-Predict for the ML Runtime in Python
Generate an XML and subsequently predict-call the ML Runtime. The ML Runtime is compiled as a DLL and is called from Python as a user-mode process. No TwinCAT Runtime is required.
tb.onnximport("../decision tree/decisiontree- regressor.onnx", "../decision tree/decisiontree- regressor.xml")
inp_file_xml = "../decision tree/decisiontree-regressor.xml"
# define input and output format and input values (10 in, 1 out)
prediction = [{'input_type': 'fp64', 'output_type': 'fp64', 'input': [-1.0,-1.0,1.0,1.4,-1.0,-1.0,1.0,1.4,4.2,4.2]}]
# call predict method of Python-ML-Runtime
out = tb.predict(inp_file_xml,prediction)
print(out)
Further sample of a Classifier.
tb.onnximport("../decision tree/decisiontree-classifier.onnx", "../decision tree/decisiontree-classifier.xml")
inp_file_xml = "../decision tree/decisiontree-classifier.xml"
# define input and output format and input values (4 in, 1 out)
prediction = [{'input_type': 'fp64', 'output_type': 'int32', 'input': [-1.0,-1.0,1.0,1.4]}]
# call predict method of Python-ML-Runtime
out = tb.predict(inp_file_xml,prediction)
print(out)