2017-11-18 211 views
0

我正在服務一個預先訓練的啓動模型,並且我已經按照官方教程直到現在都提供它。目前,我得到一個錯誤代碼3,如下:Nodejs Tensorflow服務客戶端錯誤3

{ 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: {} } } 

我使用的prediction_service.proto,因爲它是從Tensorflow服務的API。這是我的文件的NodeJS,我定義函數:

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

而我路過的圖像作爲二進制圖像如下:

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

我一直停留在這一段時間,以便如果有人能幫助我,我會很感激!任何幫助將是偉大的! 在此先感謝! :)

回答

0

好吧,所以我終於設法解決這個問題。如果有人面臨完全相同的問題,請將其作爲答案發布。

所以以來模型預計base64編碼的圖像:

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服務的inception_client.py,我發現了張量卻有shape=[1]。所以這使得getTestModelMsg如下:

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

希望能幫助別人。祝你好運。 :)