Skip to content

Commit

Permalink
feat: mnist example (onnx version 1.3) can successfully run with Gorg…
Browse files Browse the repository at this point in the history
…onia

* chore: ignore binary

* feat: create the skeleton of a utility to run a model from the zoo

* chore: prepare the implementation of the auto-padding

* feat: the utility is working

* feat: add the same_upper auto-paddding

* chore: new informations

* chore: add some doc
  • Loading branch information
owulveryck authored Apr 25, 2019
1 parent 49423a4 commit f37e05b
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
doc/doc
example/gorgonia/numpy
example/gorgonia/gorgonia
examples/model_zoo_executor/model_zoo_executor
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ This is a new version of the API.
The tweaked version of Gorgonia have been removed. It is now compatible with the master branch of Gorgonia.
Some operators are not yet available though.

Meanwhile, you can use the old version for a demo by fetching a pre-release version of checking out the old version `01b2e2b`
A utility has been added in order to run models from the zoo.
check the `examples` subdirectory.
```


Expand Down Expand Up @@ -97,6 +98,8 @@ func Example_gorgonia() {
}
```

A basic implementation can be found in the `examples` subdirectory.

## Internal

### ONNX protobuf definition
Expand Down
3 changes: 2 additions & 1 deletion RELNOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ This is a new version of the API.
The tweaked version of Gorgonia have been removed. It is now compatible with the master branch of Gorgonia.
Some operators are not yet available though.

Meanwhile, you can use the old version for a demo by fetching a pre-release version of checking out the old version `01b2e2b`
A utility has been added in order to run models from the zoo.
check the `examples` subdirectory.
48 changes: 44 additions & 4 deletions backend/x/gorgonnx/conv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gorgonnx

import (
"math"

"github.com/owulveryck/onnx-go"
"gorgonia.org/gorgonia"
"gorgonia.org/tensor"
Expand All @@ -16,6 +18,7 @@ func init() {
// https://godoc.org/gorgonia.org/gorgonia#Conv2d
// test with go test -run=TestONNX/Conv
type conv struct {
autopad string
pad []int
stride []int
dilation []int
Expand All @@ -29,6 +32,40 @@ func (c *conv) apply(g *Graph, n *Node) error {
if err != nil {
return err
}
// autopadding needs to be applied now because it needs to be aware of the shape of the nodes
switch c.autopad {
case "NOTSET":
case "":
case "SAME_UPPER":
outputHeight := int(
math.Ceil(
float64(children[0].gorgoniaNode.Shape()[2]) /
float64(c.stride[0])))
outputWidth := int(
math.Ceil(
float64(children[0].gorgoniaNode.Shape()[3]) /
float64(c.stride[1])))
c.pad[0] = int(
math.Max(
float64((outputHeight-1)*c.stride[0]+
c.kernelShape[0]-
children[0].gorgoniaNode.Shape()[2]),
float64(0))) /
2
c.pad[1] = int(
math.Max(
float64((outputWidth-1)*c.stride[1]+
c.kernelShape[1]-
children[0].gorgoniaNode.Shape()[3]),
float64(0))) /
2

default:
return &onnx.ErrNotImplemented{
Operator: "Conv",
Message: "auto_pad " + c.autopad + " not implemented",
}
}
n.gorgoniaNode, err = gorgonia.Conv2d(
children[0].gorgoniaNode,
children[1].gorgoniaNode,
Expand All @@ -41,10 +78,13 @@ func (c *conv) apply(g *Graph, n *Node) error {

func (c *conv) init(o onnx.Operation) error {
autoPad, ok := o.Attributes["auto_pad"]
if ok && autoPad.(string) != "NOTSET" {
return &onnx.ErrNotImplemented{
Operator: "Conv",
Message: "auto_pad " + autoPad.(string) + " not implemented",
if ok {
switch autoPad.(string) {
case "NOTSET":
case "VALID":
c.pad = []int{0, 0}
default:
c.autopad = autoPad.(string)
}
}
kernelShape, ok := o.Attributes["kernel_shape"]
Expand Down
16 changes: 16 additions & 0 deletions examples/model_zoo_executor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# About

This is a simple utility that runs a model from the model zoo thanks to the Gorgonia backend

## Example

Download a pre-trained [model from the zoo](https://github.com/onnx/models) (for now, only [MNIST](https://github.com/onnx/models/tree/master/mnist) is known to work)

then smply run:

`go run main.go -model /tmp/mnist/model.onnx -input /tmp/mnist/test_data_set_0/input_0.pb -output /tmp/mnist/test_data_set_0/output_0.pb`

The utility evaluates the model and check if the computed output is equal to the expected output (within a delta of 5e-3).
If the result is ok, it displays the result:

`[975.67035 -618.7244 6574.5684 668.0278 -917.27057 -1671.6357 -1952.7606 -61.54949 -777.17645 -1439.5311]`
80 changes: 80 additions & 0 deletions examples/model_zoo_executor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"

"github.com/owulveryck/onnx-go"
"github.com/owulveryck/onnx-go/backend/x/gorgonnx"
"github.com/stretchr/testify/assert"
)

func main() {
model := flag.String("model", "model.onnx", "path to the model file")
input := flag.String("input", "test_data_set_0/input_0.pb", "path to the input file")
output := flag.String("output", "test_data_set_0/output_0.pb", "path to the output file")
h := flag.Bool("h", false, "help")
flag.Parse()
if *h {
flag.Usage()
os.Exit(0)
}
for _, f := range []string{*model, *input, *output} {
if _, err := os.Stat(f); err != nil && os.IsNotExist(err) {
log.Fatalf("%v does not exist", f)
}
}
// Create a backend receiver
backend := gorgonnx.NewGraph()
// Create a model and set the execution backend
m := onnx.NewModel(backend)

// read the onnx model
b, err := ioutil.ReadFile(*model)
if err != nil {
log.Fatal(err)
}
// Decode it into the model
err = m.UnmarshalBinary(b)
if err != nil {
log.Fatal(err)
}
// Set the first input, the number depends of the model
// TODO
b, err = ioutil.ReadFile(*input)
if err != nil {
log.Fatal(err)
}
inputT, err := onnx.NewTensor(b)
if err != nil {
log.Fatal(err)
}
m.SetInput(0, inputT)
err = backend.Run()
if err != nil {
log.Fatal(err)
}
b, err = ioutil.ReadFile(*output)
if err != nil {
log.Fatal(err)
}
outputT, err := onnx.NewTensor(b)
if err != nil {
log.Fatal(err)
}
computedOutputT, err := m.GetOutputTensors()
if err != nil {
log.Fatal(err)
}
assert.InDeltaSlice(&testingT{}, outputT.Data(), computedOutputT[0].Data(), 5e-3, "the two tensors should be equal.")
fmt.Println(computedOutputT[0].Data())
}

type testingT struct{}

func (t *testingT) Errorf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}

0 comments on commit f37e05b

Please sign in to comment.