连接张量流模型的尝试未成功
来源:stackoverflow
2024-02-07 10:18:22
0浏览
收藏
怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《连接张量流模型的尝试未成功》,涉及到,有需要的可以收藏一下
问题内容
我正在尝试连接到使用tensorflowـmodel_serving提供的修改后的resnet模型
tensorflow_model_server --port=8500 --rest_api_port=8501 \
--model_name=resnet_model \
--model_base_path=/home/pc3/deeplearning/models/resnet
我的模型在我从tensorflow hub获得的原始resnet模型上有一个附加层。 它期望对 256x256 像素图像进行分类,并且只有两个输出节点。
这是我在此处文档的帮助下想出的 go.cv 接口:
package main
import (
"fmt"
"image"
"log"
"gocv.io/x/gocv"
)
func main() {
net := gocv.readnetfromtensorflow("/home/pc3/deeplearing/models/resnet/1")
imagefilepath := "./1.jpg"
img := gocv.imread(imagefilepath, gocv.imreadanycolor)
if img.empty() {
log.panic("can not read image file : ", imagefilepath)
return
}
blob := gocv.blobfromimage(img, 1.0, image.pt(256, 256), gocv.newscalar(0, 0, 0, 0), true, false)
defer blob.close()
// feed the blob into the classifier
net.setinput(blob, "input")
// run a forward pass thru the network
prob := net.forward("softmax")
defer prob.close()
// reshape the results into a 1x1000 matrix
probmat := prob.reshape(1, 2)
defer probmat.close()
// determine the most probable classification, and display it
_, maxval, _, maxloc := gocv.minmaxloc(probmat)
fmt.printf("maxloc: %v, maxval: %v\n", maxloc, maxval)
}
但出现此运行时错误:
terminate called after throwing an instance of 'cv::exception'
what(): opencv(4.6.0) /tmp/opencv/opencv-4.6.0/modules/dnn/src/tensorflow/tf_importer.cpp:2986: error: (-215:assertion failed) netbinsize || nettxtsize in function 'populatenet'
sigabrt: abort
pc=0x7fe95f86b00b m=0 sigcode=18446744073709551610
signal arrived during cgo execution
goroutine 1 [syscall]:
runtime.cgocall(0x4abc50, 0xc00005fda8)
/usr/local/go/src/runtime/cgocall.go:157 +0x5c fp=0xc00005fd80 sp=0xc00005fd48 pc=0x41f39c
gocv.io/x/gocv._cfunc_net_readnetfromtensorflow(0x223a020)
_cgo_gotypes.go:6044 +0x49 fp=0xc00005fda8 sp=0xc00005fd80 pc=0x4a7569
gocv.io/x/gocv.readnetfromtensorflow({0x4e7fcf?, 0x428d87?})
/home/pc3/go/pkg/mod/gocv.io/x/[email protected]/dnn.go:280 +0x5e fp=0xc00005fde8 sp=0xc00005fda8 pc=0x4a815e
main.main()
/home/pc3/go/src/test-go-ml/main.go:13 +0x51 fp=0xc00005ff80 sp=0xc00005fde8 pc=0x4a89b1
runtime.main()
/usr/local/go/src/runtime/proc.go:250 +0x212 fp=0xc00005ffe0 sp=0xc00005ff80 pc=0x44f892
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1571 +0x1 fp=0xc00005ffe8 sp=0xc00005ffe0 pc=0x4780a1
我可以使用这个 python 代码片段与模型无缝通信:
from urllib import response
import requests
import base64
import cv2
import json
import numpy as np
from keras.applications.imagenet_utils import decode_predictions
image = cv2.imread("10.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (256, 256))
image = np.expand_dims(image, axis=0)
image = np.true_divide(image, 255)
data = json.dumps({"signature_name":"serving_default", "instances": image.tolist()})
url = "http://localhost:8501/v1/models/resnet_model:predict"
response = requests.post(url, data=data, headers = {"content_type": "application/json"})
predictions = json.loads(response.text)
感谢您帮助解决此问题,因为官方文档确实缺乏,而且我找不到任何有关此问题的教程。
正确答案
如果有人感兴趣,这里是我们找到的完整解决方案:
package main
import (
"bytes"
"encoding/json"
"image"
"io"
"log"
"net/http"
"os"
"github.com/barnex/matrix"
"gocv.io/x/gocv"
"gorgonia.org/tensor"
)
type Data struct {
Signature_name string `json:"signature_name"`
Instances []interface{} `json:"instances"`
}
func main() {
imageFilePath := "1.jpeg"
mat := gocv.IMRead(imageFilePath, gocv.IMReadAnyColor)
if mat.Empty() {
log.Panic("Can not read Image file : ", imageFilePath)
return
}
resizeImage := gocv.NewMat()
gocv.Resize(mat, &resizeImage, image.Point{X: 256, Y: 256}, 0, 0, gocv.InterpolationNearestNeighbor)
img := resizeImage.Clone()
gocv.CvtColor(resizeImage, &img, gocv.ColorBGRToRGB)
a := tensor.New(tensor.WithBacking(img.ToBytes()))
backing := a.Data().([]byte)
backing2 := make([]float64, len(backing))
for i := range backing {
x := float64(backing[i]) / 255
backing2[i] = x
}
matrix := matrix.ReshapeD3(backing2, [3]int{256, 256, 3})
var dim []interface{}
dim = append(dim, []interface{}{matrix}...)
data := Data{
Signature_name: "serving_default",
Instances: dim,
}
// send marshalled `data` to the Model
url := "http://localhost:8501/v1/models/resnet_model:predict"
payloadBuf := new(bytes.Buffer)
json.NewEncoder(payloadBuf).Encode(data)
req, _ := http.NewRequest("POST", url, payloadBuf)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
return
}
defer res.Body.Close()
io.Copy(os.Stdout, res.Body)
}到这里,我们也就讲完了《连接张量流模型的尝试未成功》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
如何将 widget.NewMultiLineEntry() 滚动到底部?
- 上一篇
- 如何将 widget.NewMultiLineEntry() 滚动到底部?
- 下一篇
- 在 Go 语言中如何编写数组的映射并添加到 JSON 文件中?
查看更多
最新文章
-
- Golang · Go问答 | 2小时前 | go · CGO · Go CGO CGO_ENABLED build constraints
- Go CGO_ENABLED=0 后找不到实现怎样处理
- 329浏览 收藏
-
- Golang · Go问答 | 2小时前 | Go问答 · Go GOARCH GOOS build tags go list
- Go build tags 选错实现文件怎么办
- 496浏览 收藏
-
- Golang · Go问答 | 3小时前 |
- Go 下载依赖卡在 sum.golang.org 时怎么处理
- 290浏览 收藏
-
- Golang · Go问答 | 3小时前 |
- Go module path 与仓库地址不一致为什么拉取失败
- 192浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go http.Flusher 断言失败的兼容处理
- 103浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go linker flags 传入空格参数的拆分方案
- 273浏览 收藏
-
- Golang · Go问答 | 11小时前 | 依赖管理 · Go问答 · go list -mod=vendor GOFLAGS Go vendor Go module cache
- Go vendor 与 module cache 选择不一致的定位
- 275浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go replace 指向本地目录后 CI 失败的迁移方法
- 445浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go database/sql Tx 提交后查询结果不一致的排查方法
- 450浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go 无缓冲 channel 发送卡住的定位方法
- 272浏览 收藏
-
- Golang · Go问答 | 1天前 |
- Go io.Pipe 写入端阻塞的背压排查
- 468浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 562次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 607次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 636次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 593次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 414次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go JSONJSON null 解码到指针后状态不符合预期怎么办
- 2026-10-18 501浏览
-
- Go crypto/rand.Text 的长度为什么不是固定字符数
- 2026-10-04 501浏览
-
- Go strings.ToValidUTF8 清洗日志内容的边界
- 2026-10-03 501浏览

