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.