4

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

Browser version

not in the browser, using Node command

Describe the problem or feature request

I am using tensorflow.js of coco-SSD for mobilenet v2 when I am using the pre-trained model [coco-ssd] it's working perfectly fine, see the below code

const tfnode = require('@tensorflow/tfjs-node');
const cocoSsd = require('./coco-ssd.js'); 
const fs = require('fs');
const readImage = path => {
    const imageBuffer = fs.readFileSync(path);
    const tfimage = tfnode.node.decodeImage(imageBuffer);
    return tfimage;
  }
  const objectDetection = async path => {
    const image = readImage(path);
    const loadlModelPromise = await cocoSsd.load({base: "mobilenet_v2"})
    const result = await loadlModelPromise.detect(image);
    console.log('Classification Results:', result);
  }
  objectDetection(process.argv[2]);

while calling the above code node filename.js ./testImage.png it gives the desired result.

Now I have a custom trained coco-ssd model which i have converted to tensorflow.js format using the below command **tensorflowjs_converter --input_format=tf_saved_model --output_node_names='num_detections,detection_boxes,detection_scores,detection_classes' --signature_name=serving_default --saved_model_tags=serve ./saved_model ./ ** once i have the converted model.json i wrote the below code for inference on the custom converted model.

const tfnode = require('@tensorflow/tfjs-node');
const cocoSsd = require('./coco-ssd.js'); 
const fs = require('fs');
const readImage = path => {
    const imageBuffer = fs.readFileSync(path);
    const tfimage = tfnode.node.decodeImage(imageBuffer);
    return tfimage;
  }
  const objectDetection = async path => {
    const image = readImage(path);
    const modelUrl = 'file:///Users/xx/model.json'
    const loadlModelPromise = await cocoSsd.load({base: "mobilenet_v2",modelUrl: modelUrl})
    const result = await loadlModelPromise.detect(image);
    console.log('Classification Results:', result);
  }
  objectDetection(process.argv[2]);

now when I am running the above code node fileName.js ./testImage.png getting the bellow error

(node:32504) UnhandledPromiseRejectionWarning: Error: Tensor must have a shape comprised of positive integers but got shape [100,]. at assert (C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\util.js:105:15) at C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\util.js:646:9 at Array.forEach () at Object.assertNonNegativeIntegerDimensions (C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\util.js:645:11) at makeTensor (C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\ops\tensor_ops.js:73:16) at Object.tensor2d (C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\ops\tensor_ops.js:189:12) at C:\Users\xx\tfJs\tensorflowJs Classifier\coco-ssd.js:17:7039 at C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\engine.js:388:22 at Engine.scopedRun (C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\engine.js:398:23) at Engine.tidy (C:\Users\xx\tfJs\node_modules@tensorflow\tfjs-core\dist\engine.js:387:21)

please help :-)

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mithilesh
  • 223
  • 2
  • 13
  • Can you please indicate what is the shape of `image` ? – edkeveked Feb 18 '20 at 12:35
  • image.shape: [ 327, 547, 3 ] – Mithilesh Feb 18 '20 at 13:14
  • The error has to do with your model that you are loading. What does contain model.json ? – edkeveked Feb 18 '20 at 14:31
  • I Used the trained model from here -> [ssd_mobilenet_v2_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz) and converted the saved_model using tensorflowjs_converter, which produced the model.json – Mithilesh Feb 18 '20 at 15:41
  • Did you try loading the model using `tf.loadGraphModel` ? – edkeveked Feb 18 '20 at 16:20
  • Yes, I did, its able to load the model. But when I am doing `const output = await loadlModelPromise.executeAsync(image); console.log(output);` i am getting [ Tensor { kept: false, isDisposedInternal: false, shape: [ 1, 100, 4 ], dtype: 'float32', size: 400, strides: [ 400, 4 ], dataId: {}, id: 2504, rankType: '3', scopeId: 2516 }, Tensor {.. not able to interpret this, do you know this? – Mithilesh Feb 18 '20 at 17:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208122/discussion-between-edkeveked-and-mithilesh). – edkeveked Feb 19 '20 at 12:28
  • I have exactly the same issue @edkeveked. Could you help? – Claret Nnamocha Mar 08 '20 at 22:18
  • @ClaretNnamocha, could you please ask your question in a new thread and let me know about it ? – edkeveked Mar 09 '20 at 08:57
  • You can follow the status on GitHub Issue -> https://github.com/tensorflow/tfjs/issues/2766 – Mithilesh Mar 11 '20 at 12:33

0 Answers0