# onnxruntime **Repository Path**: wy136/onnxruntime ## Basic Information - **Project Name**: onnxruntime - **Description**: golang onnxruntime - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-02-16 - **Last Updated**: 2024-02-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README `onnxruntime` Wrapper for Go =========================================== 关于 ----- 该库提供golang 运行机器学习模型,由于是通过调用C 和 C++ API来运行,因此需要支持 cgo。 如果要使用 CUDA,则需要使用支持 CUDA 的 onnxruntime 共享库,以及使用 CUDA OnnxRuntime 库的基础版本支持的版本。[CUDA 支持文档](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html) onnxruntime 版本问题 ------------------------------------ 当前这个库使用的是onnxruntime 1.16.3版本,如果需要使用其他版本,需要修改代码。 1. 替换新版本 `onnxruntime_c_api.h`; 2. 指定新版本的 `libonnxruntime.dll` (or `libonnxruntime.so`) C API header 和 动态库文件 [official repo](https://github.com/microsoft/onnxruntime). 在这里可以下载最新的onnxruntime库. 如果你想要在windows 上支持GPU,还需要`execution_providers_cuda.dll` 和 `execution_providers_shared.dll` 使用示例 ------------- ```go import ( "fmt" ort "gitee.com/wb253/onnxruntime" ) func main() { ort.SetSharedLibraryPath("/usr/local/lib/libonnxruntime.so") ort.InitializeEnvironment() defer ort.DestroyEnvironment() inputData := []float32{0.19473445415496826, 0.9139836430549622, 0.7043011784553528, 0.7685686945915222} inputShape := ort.NewShape(1, 1, 4) inputTensor, _ := ort.NewTensor(inputShape, inputData) defer inputTensor.Destroy() // This hypothetical network maps a 2x5 input -> 2x3x4 output. outputShape := ort.NewShape(1, 1, 2) outputTensor, _ := ort.NewEmptyTensor[float32](outputShape) defer outputTensor.Destroy() session, _ := ort.NewSession("./example_network.onnx", []string{"1x4 Input Vector"}, []string{"1x2 Output Vector"}, nil) defer session.Destroy() // Calling Run() will run the network, reading the current contents of the // input tensors and modifying the contents of the output tensors. err := session.Predict([]ort.ArbitraryTensor{inputTensor}, []ort.ArbitraryTensor{outputTensor}) if err != nil { fmt.Printf("err %s", err) return } fmt.Printf("output: %+v\r\n", outputTensor.GetData()) } ```