Can not make go.cv to interface tensorflow model

Viewed 59

I'm trying to connect to my modified resnet model which is served using tensorflowـmodel_serving

tensorflow_model_server --port=8500 --rest_api_port=8501 \
                        --model_name=resnet_model \
                        --model_base_path=/home/pc3/deeplearning/models/resnet 

My model has an additional layer to the original resnet model which I got from tensorflow hub. It expects 256x256 pixel images to classify and has only two output nodes.

Here is the go.cv interface that I could came up with with the help of the docs here:

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)

}

But get this runtime error:

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 

I can communicate with the model seamlessly using this python snippet:

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)

Appreciate your help to resolve this, as the official docs is really lacking and I could not find any tutorial about this.

1 Answers

In case anyone interested, here is the complete solution that we found:

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)
}
Related