连接张量流模型的尝试未成功
时间:2024-02-07
浏览:240
作者:佚名
怎么入门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学习网公众号,带你了解更多关于的知识点!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
