Giter VIP home page Giter VIP logo

goonnx's Introduction

go-onnx

Go language bindings for ONNX runtime

About

I'm a fan of Go and have just started digging a bit deeper in to machine learning. I heard about ONNX runtime and I'm a fan of standardization, so it seemed like a good place to start. I realized ONNX runtime didn't have Go language bindings, and I figured, if I can get that going, it'd probably be a great way to get started on my AI/ML journey.

The initial goal was to replicate the functionality of the C example from the ONNX repository, here.

At this point, the implemented functionality achieves the same result as the example noted above and I've, additionally, tested it with ResNet on image classification (example below and in main.go).

The API is incomplete (compared to the functionality available in the C library), at this time. I may try continue to build it out, as time permits, but would gladly accept help if anybody else is interested in this sort of thing.

Using this library

Go-onnx uses cgo and leverages the onnxruntime shared library, so to run your program which leverages go-onnx, you'll need to let cgo know where that library resides on your local system. To do so, in your main.go (or wherever), include something like the following snippet:

/*
#cgo LDFLAGS: -L/path/to/onnx/runtime/lib
 */
import "C"

The directory specified should contain the libonnxruntime.so (named the same). If your ONNX runtime file is named something different, you may need to include the additional flag -l<libname>.

Example

For a new application, first get go-onnx:

go get github.com/dhdanie/goonnx

You'll also need to download the example ResNet model from here.

Then, you should be able to run a basic demo application like the following (see main.go for working demo):

package main

/*
#cgo LDFLAGS: -L/usr/local/lib/onnx -lonnxruntime
*/
import "C"

...

func classifyResNet(rgbVals []float32) [][]float32 {
	defer timeTrack(time.Now(), "classifyResnet")

	logId := "log0001"

	var myCustomLogger ort.CustomLogger = func(severity ort.LoggingLevel, category string, codeLocation string, message string) {
		fmt.Printf("Custom Logger %d/%s/%s - %s\n", severity, category, codeLocation, message)
	}

	env, err := ort.NewEnvironmentWithCustomLogger(ort.LoggingLevelVerbose, logId, myCustomLogger)
	if err != nil {
		errorAndExit(err)
	}
	defer env.ReleaseEnvironment()

	opts := &ort.SessionOptions{
		IntraOpNumThreads:      1,
		GraphOptimizationLevel: ort.GraphOptLevelEnableBasic,
		SessionLogID:           logId,
		LogVerbosityLevel:      0,
	}

	session, err := ort.NewSession(env, "models/resnet152v2.onnx", opts)
	if err != nil {
		errorAndExit(err)
	}
	defer session.ReleaseSession()

	typeInfo, err := session.GetInputTypeInfo(0)
	if err != nil {
		errorAndExit(err)
	}
	tensorInfo, err := typeInfo.ToTensorInfo()
	if err != nil {
		errorAndExit(err)
	}
	memoryInfo, err := ort.NewCPUMemoryInfo(ort.AllocatorTypeArena, ort.MemTypeDefault)
	if err != nil {
		errorAndExit(err)
	}
	defer memoryInfo.ReleaseMemoryInfo()
	value, err := ort.NewTensorWithFloatDataAsValue(memoryInfo, "data", rgbVals, tensorInfo)
	if err != nil {
		errorAndExit(err)
	}
	inputValues := []ort.Value{
		value,
	}
	outs, err := session.Run(&ort.RunOptions{}, inputValues)
	if err != nil {
		errorAndExit(err)
	}
	outputs := make([][]float32, len(outs))
	for i, out := range outs {
		if out.GetName() != "resnetv27_dense0_fwd" {
			continue
		}
		outFloats, err := out.GetTensorMutableFloatData()
		if err != nil {
			errorAndExit(err)
		}
		outputs[i] = make([]float32, len(outFloats))
		for j := range outFloats {
			outputs[i][j] = outFloats[j]
		}
	}

	return outputs
}

License

MIT License

goonnx's People

Contributors

dhdanie avatar

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.