How to adjust a Neural Networks metadata

From Tygron Preview Support Wiki
Jump to navigation Jump to search

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 

Add a metadata key (without duplication):

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
import onnx
onnx_model = onnx.load("path/to/onnx/file")
addMeta(onnxModel, "key", "value")

Save the onnx model to file:

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

Producer, version and description are set using these functions

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

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

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