Nodejs Tensorflow 服务客户端错误 3 [英] Nodejs Tensorflow Serving Client Error 3

查看:28
本文介绍了Nodejs Tensorflow 服务客户端错误 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提供一个预先训练好的初始模型,并且我一直按照官方教程提供服务直到现在.我目前收到错误代码 3,如下所示:

I'm serving a pre-trained inception model, and I've followed the official tutorials to serve it up until now. I'm currently getting an Error Code 3, as follows:

{ Error: contents must be scalar, got shape [305]
  [[Node: map/while/DecodeJpeg = DecodeJpeg[_output_shapes=[[?,?,3]], acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](map/while/TensorArrayReadV3)]]
  at /server/node_modules/grpc/src/client.js:554:15 code: 3, metadata: Metadata { _internal_repr: {} } }

我使用的是来自 Tensorflow Serving 的 API 的 predict_service.proto.这是我定义函数的 Nodejs 文件:

I'm using the prediction_service.proto as it is from Tensorflow Serving's API. Here's my Nodejs file where I define the function:

const PROTO_PATH = "./pb/prediction_service.proto";
const TensorflowServing = grpc.load(PROTO_PATH).tensorflow.serving;

const testClient = new TensorflowServing.PredictionService(
    TF_TEST, grpc.credentials.createInsecure()
);

function getTestModelMsg(val){
    return {
        model_spec: { name: "inception", signature_name: "predict_images", version: 1},
        inputs: {
            images: {
                dtype: "DT_STRING",
                tensor_shape: {
                    dim: [{size: 220}, {size: 305}],
                    unknown_rank: false
                },
                string_val: val
            }
        }
    }
}


function predictTest(array, callback) {
    testClient.predict(getTestModelMsg(array), (error, response) => {
        if(error)
            return callback(error);

    callback(null, response.outputs)
})}

我将图像作为二进制图像传递如下:

And I'm passing in the image as a binary image as follows:

fs.readFile('./test/Xiang_Xiang_panda.jpg', (err, data) => {
    if(err) {
        return res.json({message: "Not found"});
    }

    predictTest( data.toString('binary') , (error, outputs) => {
        if (error) {
            console.error(error);
            return res.status(500).json({ error });
        }
        res.status(200).json({ outputs });
    })
})

我已经坚持了一段时间,所以如果有人能在这里帮助我,我将不胜感激!任何帮助都会很棒!提前致谢!:)

I've been stuck at this for a while so would really appreciate if anyone could help me out here! Any help would be great! Thanks in advance! :)

推荐答案

好的,所以我终于设法破解了这个问题.在这里发布它作为答案,以防有人遇到完全相同的问题.

Okay, so I finally managed to crack this. Posting it as an answer here in case someone faces this exact same problem.

所以初始模型需要一个 base64 编码的图像:

So the inception model expects a base64 encoded image:

fs.readFile('./test/Xiang_Xiang_panda.jpg', (err, data) => {
    if(err) {
        return res.json({message: "Not found"});
    }

    predictTest( data.toString('base64') , (error, outputs) => {
        if (error) {
            console.error(error);
            return res.status(500).json({ error });
        }
        res.status(200).json({ outputs });
    })
})

然后查看来自 Tensorflow Serving 的 inception_client.py,我发现张量实际上具有 shape=[1].所以这使得 getTestModelMsg 为:

Then looking at the inception_client.py from Tensorflow Serving, I found out the the tensor actually has the shape=[1]. So this makes the getTestModelMsg as:

function getTestModelMsg(val){
return {
    model_spec: { name: "inception", signature_name: "serving_default", version: 1},
    inputs: {
        images: {
            dtype: "DT_STRING",
            tensor_shape: {
                dim: [{size: 1}],
                unknown_rank: false
            },
            string_val: val
        }
    }
}

希望对某人有所帮助.祝你好运.:)

Hope that helps someone. Goodluck. :)

这篇关于Nodejs Tensorflow 服务客户端错误 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆