Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
yolo testing material
func eqInDelta(a,b tensor.Tensor, delta float32) bool {
outputValue := outputT[0].Data().([]float32)
expectedValue := expectedOutput.Data().([]float32)
if ! outputT[0].Shape().Eq(expectedOutput.Shape()) {
return false
}
for i := 0; i < len(outputValue); i++ {
if math.Abs(float64(outputValue[i]-expectedValue[i])) > delta {
return false
}
}
return true
}
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/owulveryck/onnx-go"
"github.com/owulveryck/onnx-go/backend/x/gorgonnx"
"github.com/stretchr/testify/assert"
"gorgonia.org/tensor"
)
var (
modelONNX = "../FACES/yolo.onnx"
npyZeroResult = "../FACES/keras/output.npy"
)
func TestYOLO(t *testing.T) {
b, err := ioutil.ReadFile(modelONNX)
check(t, err)
backend := gorgonnx.NewGraph()
model := onnx.NewModel(backend)
err = model.UnmarshalBinary(b)
check(t, err)
inputT := tensor.New(
tensor.WithShape(1, 416, 416, 3),
tensor.Of(tensor.Float32))
err = model.SetInput(0, inputT)
check(t, err)
err = backend.Run()
check(t, err)
outputT, err := model.GetOutputTensors()
check(t, err)
file, err := os.Open(npyZeroResult)
check(t, err)
defer file.Close()
expectedOutput := new(tensor.Dense)
err = expectedOutput.ReadNpy(file)
check(t, err)
assert.Equal(t, expectedOutput.Shape(), outputT[0].Shape())
outputValue := outputT[0].Data().([]float32)
expectedValue := expectedOutput.Data().([]float32)
assert.InDeltaSlice(t, expectedValue, outputValue, 5e-5)
}
func check(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.