代码拉取完成,页面将自动刷新
onnxruntime
Wrapper for Go该库提供golang 运行机器学习模型,由于是通过调用C 和 C++ API来运行,因此需要支持 cgo。 如果要使用 CUDA,则需要使用支持 CUDA 的 onnxruntime 共享库,以及使用 CUDA OnnxRuntime 库的基础版本支持的版本。CUDA 支持文档
当前这个库使用的是onnxruntime 1.16.3版本,如果需要使用其他版本,需要修改代码。
替换新版本 onnxruntime_c_api.h
;
指定新版本的 libonnxruntime.dll
(or libonnxruntime.so
)
C API header 和 动态库文件
official repo. 在这里可以下载最新的onnxruntime库.
如果你想要在windows 上支持GPU,还需要execution_providers_cuda.dll
和 execution_providers_shared.dll
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())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。