首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能使go.cv接口tensorflow模型

不能使go.cv接口tensorflow模型
EN

Stack Overflow用户
提问于 2022-06-21 15:02:54
回答 1查看 90关注 0票数 1

我正在尝试连接到修改后的resnet模型,该模型使用tensorflowـmodel_serving提供服务

代码语言:javascript
运行
复制
tensorflow_model_server --port=8500 --rest_api_port=8501 \
                        --model_name=resnet_model \
                        --model_base_path=/home/pc3/deeplearning/models/resnet 

我的模型除了我从tensorflow中心获得的原始resnet模型还有一个额外的层。它要求256×256像素图像进行分类,并且只有两个输出节点。

下面是我可以在docs 这里的帮助下提出的这里接口

代码语言:javascript
运行
复制
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)

}

但是获取这个运行时错误:

代码语言:javascript
运行
复制
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/gocv@v0.31.0/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片段无缝地与模型通信:

代码语言:javascript
运行
复制
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)

感谢你帮助解决这个问题,因为官方文档真的很缺乏,我找不到任何关于这方面的教程。

EN

回答 1

Stack Overflow用户

发布于 2022-06-23 07:18:51

如果有人感兴趣,下面是我们找到的完整解决方案:

代码语言:javascript
运行
复制
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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72703098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档