How to adjust a Neural Networks metadata: Difference between revisions

From Tygron Preview Support Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
Required install:
Metadata of a Neural Network stored in the [[ONNX]] file format can be adjusted using the python code found below.
 
==Prerequisites==
An installed [https://en.wikipedia.org/wiki/Python_(programming_language) python] environment is required to execute the code.
Additionally, install the onnx library using [https://en.wikipedia.org/wiki/Pip_%28package_manager%29 PIP]:
<pre>pip install onnx </pre>
<pre>pip install onnx </pre>


 
To add or edit a metadata key (without duplication), you can use the following code:
Add a metadata key (without duplication):
<pre>
<pre>
def addMeta(onnx_model, key, value):
def addMeta(onnx_model, key, value):
Line 17: Line 20:
</pre>
</pre>


To adjust an existing ONNX file, you first have to load the model. Next, pass the model to the method, with additional key and value arguments.
<pre>
<pre>
import onnx
import onnx
Line 22: Line 26:
addMeta(onnxModel, "key", "value")
addMeta(onnxModel, "key", "value")
</pre>
</pre>
Save the onnx model to file:  
 
Lastly, save the onnx model again to a (new) file:  
<pre>
<pre>
with open(onnxFilePath, "wb") as f:
with open(onnxFilePath, "wb") as f:
     f.write(onnx_model.SerializeToString())
     f.write(onnx_model.SerializeToString())
</pre>
</pre>
Producer, version and description are set using these functions
 
Producer, version and description are set using these functions, as these are not stored in the meta data.
<pre>
<pre>
def setProducer(onnxModel, producer):
def setProducer(onnxModel, producer):
Line 38: Line 44:
     onnxModel.doc_string = description
     onnxModel.doc_string = description
</pre>
</pre>
{{article end
|seealso=*[[Neural Network]]
*[[ONNX]]
}}
{{InferenceOverlay nav}}

Latest revision as of 15:03, 19 December 2024

Metadata of a Neural Network stored in the ONNX file format can be adjusted using the python code found below.

Prerequisites

An installed python environment is required to execute the code. Additionally, install the onnx library using PIP:

pip install onnx 

To add or edit a metadata key (without duplication), you can use the following code:

def addMeta(onnx_model, key, value):
    
    for entry in onnx_model.metadata_props:
        if entry.key == key:
            entry.value = value
            return

    meta = onnx_model.metadata_props.add()
    meta.key = key
    meta.value = value

To adjust an existing ONNX file, you first have to load the model. Next, pass the model to the method, with additional key and value arguments.

import onnx
onnx_model = onnx.load("path/to/onnx/file")
addMeta(onnxModel, "key", "value")

Lastly, save the onnx model again to a (new) file:

with open(onnxFilePath, "wb") as f:
    f.write(onnx_model.SerializeToString())

Producer, version and description are set using these functions, as these are not stored in the meta data.

def setProducer(onnxModel, producer):
    onnxModel.producer_name = producer

def setModelVersion(onnxModel, modelVersion):
    onnxModel.model_version = modelVersion

def setDocString(onnxModel, description):
    onnxModel.doc_string = description