Remove Onnx Input Initializers Using – PyTorch Tutorial

By | October 8, 2023

It is easy to remove onnx input initializers. In this tutorial, we will introduce you how to do.

Step 1. Download

You can download this script here.

import argparse

import onnx

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", required=True, help="input model")
    parser.add_argument("--output", required=True, help="output model")
    args = parser.parse_args()
    return args

def remove_initializer_from_input():
    args = get_args()

    model = onnx.load(args.input)
    if model.ir_version < 4:
        print("Model with ir_version below 4 requires to include initilizer in graph input")

    inputs = model.graph.input
    name_to_input = {}
    for input in inputs:
        name_to_input[] = input

    for initializer in model.graph.initializer:
        if in name_to_input:
            inputs.remove(name_to_input[]), args.output)

if __name__ == "__main__":

Step 2: Use command below to remove

python --input moel.onnx --output model_output.onnx

Moreover, you also can use os.popen() or os.system() to run this command in your own script.

Understand the Difference Between Python os.popen() and os.system(): A Completed Guide

Remove Onnx Input Initializers Using - PyTorch Tutorial