Skip to content

Latest commit

 

History

History
 
 

onnx

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

JavaCPP Presets for ONNX

Introduction

This directory contains the JavaCPP Presets module for:

Please refer to the parent README.md file for more detailed information about the JavaCPP Presets.

Documentation

Java API documentation is available here:

Sample Usage

Here is a simple example of ONNX ported to Java from this C++ source file and for this data:

We can use Maven 3 to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the pom.xml and LoadModel.java source files below, simply execute on the command line:

 $ mvn compile exec:java

The pom.xml build file

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bytedeco.onnx</groupId>
    <artifactId>loadmodel</artifactId>
    <version>1.5.2</version>
    <properties>
        <exec.mainClass>LoadModel</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>onnx-platform</artifactId>
            <version>1.6.0-1.5.2</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>.</sourceDirectory>
    </build>
</project>

The LoadModel.java source file

import java.nio.file.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.onnx.*;
import static org.bytedeco.onnx.global.onnx.*;

public class LoadModel {
    public static void main(String[] args) throws Exception {
        OpSchemaVector allSchemas = OpSchemaRegistry.get_all_schemas();
        System.out.println(allSchemas.size());

        byte[] bytes = Files.readAllBytes(Paths.get("examples/resources/single_relu.onnx"));

        ModelProto model = new ModelProto();
        ParseProtoFromBytes(model, new BytePointer(bytes), bytes.length);

        check_model(model);

        InferShapes(model);

        StringVector passes = new StringVector("eliminate_nop_transpose", "eliminate_nop_pad", "fuse_consecutive_transposes", "fuse_transpose_into_gemm");
        Optimize(model, passes);

        check_model(model);

        ConvertVersion(model, 8);

        System.out.println(model.graph().input_size());
    }
}