oramasearch / onnx-go

onnx-go gives the ability to import a pre-trained neural network within Go without being linked to a framework or library.
https://blog.owulveryck.info/2019/04/03/from-a-project-to-a-product-the-state-of-onnx-go.html
MIT License
717 stars 71 forks source link

panic: index out of range #208

Open yg7001 opened 1 year ago

yg7001 commented 1 year ago
func getONNXInputSize(onnxFilePath string) {
    backend:=simple.NewSimpleGraph()
    //backend:=gorgonnx.NewGraph()
    model  := onnx.NewModel(backend)    
    // read the onnx model
    b, err := utils.LoadFileBytes(onnxFilePath)
    if err!=nil{
        fmt.Println("read file error ",onnxFilePath,err)
        panic(err)
    }
    if len(b)==0{
        fmt.Println("onnf file error with lenght 0")
    }
    // Decode it into the model
    err = model.UnmarshalBinary(b)   // panic goes here!!!!!!!!!!!!!!!!!!!!!!!!
    if err!=nil{
        fmt.Println("parse onnx failure",err)
        panic(err)
    }
    inputs := model.GetInputTensors()
    if len(inputs) == 0 {
        fmt.Println("No inputs found in the ONNX model")
    }
    input := inputs[0]

    dims := input.Dims()
    fmt.Println("Input image dimensions:", dims)
}

output: panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]: gorgonia.org/tensor.StdEng.makeArray({{}}, 0xc00015adb8, {{0x831c20?, 0x721f60?}}, 0x0) D:/golangdev/work/pkg/mod/gorgonia.org/tensor@v0.9.3/defaultengine.go:22 +0xec gorgonia.org/tensor.(Dense).makeArray(0xc00015ad80, 0x0) D:/golangdev/work/pkg/mod/gorgonia.org/tensor@v0.9.3/dense.go:87 +0xe2 gorgonia.org/tensor.(Dense).fix(0xc00015ad80) D:/golangdev/work/pkg/mod/gorgonia.org/tensor@v0.9.3/dense.go:292 +0x1c5 gorgonia.org/tensor.New({0xc001cb4920, 0x3, 0x1?}) D:/golangdev/work/pkg/mod/gorgonia.org/tensor@v0.9.3/tensor.go:93 +0xb8 github.com/owulveryck/onnx-go/internal/onnx/ir.(TensorProto).Tensor(0xc000141e60) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/internal/onnx/ir/tensor.go:51 +0x5e5 github.com/owulveryck/onnx-go.toOperationAttribute(0xc0000c6100) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/attributes.go:30 +0x109 github.com/owulveryck/onnx-go.toOperationAttributes({0xc00000a710, 0x1, 0xc0000afbf0?}) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/attributes.go:10 +0x6c github.com/owulveryck/onnx-go.(Model).applyModelProtoGraphNodeOperations(0xc00010bce0, 0xc000100500?) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/decoder.go:235 +0x10d github.com/owulveryck/onnx-go.(Model).applyModelProtoGraph(0xc00010bce0, 0xc000132210) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/decoder.go:149 +0x299 github.com/owulveryck/onnx-go.(Model).decodeProto(0xc000198000?, 0x1b0853b?) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/decoder.go:112 +0x105 github.com/owulveryck/onnx-go.(*Model).UnmarshalBinary(0x7a3222?, {0xc000198000, 0x1b0853b, 0x1b0853c}) D:/golangdev/work/pkg/mod/github.com/owulveryck/onnx-go@v0.5.0/decoder.go:38 +0x75 main.getONNXInputSize({0x7a3222, 0x24}) D:/GolangDev/learnings/testGoCV/yolov3.go:256 +0x277 main.testYolo() D:/GolangDev/learnings/testGoCV/yolov3.go:27 +0x3a main.main() D:/GolangDev/learnings/testGoCV/main.go:27 +0x17 exit status 2

annatelegina commented 1 year ago

Hello! I have the same error, have you found the solution? I can not identify where the problem is.

BerndCzech commented 1 year ago

I am running into the same issue - I guess 209 is just a copy of it.

BerndCzech commented 1 year ago

I found 184 also ran into the issue. As this is long ago with no solution i guess there is no known fix to it.

BerndCzech commented 1 year ago

It might help to know that s is of length 0 in /home/bernd/go/pkg/mod/gorgonia.org/tensor@v0.9.3/defaultengine.go:16:

func (e StdEng) makeArray(arr *array, t Dtype, size int) {
    memsize := calcMemSize(t, size)
    s := make([]byte, memsize)
    arr.t = t
    arr.L = size
    arr.C = size
    arr.Ptr = unsafe.Pointer(&s[0])
    arr.fix()
}

So this is why s[0] panics.

The depending project knows of the issue from tests but seems to regard this as an artifact. Thus I presume onnx-go should avoid calling their api with size = 0.