Git Product home page Git Product logo

go-onnxruntime's Introduction

Go Onnxruntime Wrapper

features

  • onnx input's and output's names and shapes
  • onnx inference
  • cuda support in windows and linux

Reference

  1. github.com/nbigaouette/onnxruntime-rs
  2. github.com/yalue/onnxruntime_go

Requirements

Windows

  1. Download onnxruntime from https://github.com/microsoft/onnxruntime/releases

    e.g. download file: onnxruntime-win-x64-gpu-1.16.0.zip

Linux

  1. Download onnxruntime from https://github.com/microsoft/onnxruntime/releases

    e.g. download file: onnxruntime-linux-x64-gpu-1.16.0.tgz

  2. unpack to /usr/local/onnxruntime

    $ tar -zxvf onnxruntime-linux-x64-gpu-1.16.0.tgz
    $ mv onnxruntime-linux-x64-gpu-1.16.0 /usr/local/onnxruntime
  3. set environment

    $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/onnxruntime/lib

Usage

package main

import (
	"log"
	"runtime"

	ort "github.com/yam8511/go-onnxruntime"
)

func main() {
	var onnxruntimeDLL string
	if runtime.GOOS == "windows" {
		onnxruntimeDLL = "path/to/your/onnxruntime.dll"
	}
	sdk, err := ort.New_ORT_SDK(onnxruntimeDLL)
	if err != nil {
		log.Panicln("new onnxruntime sdk error", err)
	}
	defer sdk.Release()

	/*
	 * Load Onnx File
	 */
	session, _ := ort.NewSessionWithONNX(sdk, "path/to/your/model.onnx", true)
	defer session.Release()

	/*
	 * Run session method 1. auto binding onnx input's and output's name and shape
	 */
	{
		inputTensor, _ := ort.NewInputTensor(session, "", []float32{0.1, 0.2, 0.3})
		outputTensor, _ := ort.NewEmptyOutputTensor[float32](session, "")
		_ = session.RunDefault([]ort.AnyTensor{inputTensor}, []ort.AnyTensor{outputTensor})
		_ = outputTensor.GetData()
	}

	/*
	 * Run session method 2. specify tensor's name and shape
	 */
	{
		inputNames := []string{"input"}
		inputTensor, _ := ort.NewTensor(session, ort.NewShape(1, 1, 1), []float32{0.1, 0.2, 0.3})
		outputNames := []string{"output"}
		outputTensor, _ := ort.NewTensor(session, ort.NewShape(1), []float32{})
		_ = session.Run(
			inputNames, []ort.AnyTensor{inputTensor},
			outputNames, []ort.AnyTensor{outputTensor},
		)
		_ = outputTensor.GetData()
	}
}

Example

go-onnxruntime's People

Contributors

yam8511 avatar

Stargazers

 avatar Viggo avatar  avatar  avatar

Watchers

 avatar

Forkers

mr-xiaolei

go-onnxruntime's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.