tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.48k stars 1.93k forks source link

Error: The shape of dict[‘input_tensor’] provided in model.execute(dict) must be [1,-1,-1,3] #5366

Open playground opened 3 years ago

playground commented 3 years ago

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information MacOS Big Sur Node v15.7.0 NPM 7.20.0

I'm running @tensorflow/tfjs-node v 3.7.0

I attempted to convert my saved model to friendly model json using tensorflowjs_converter

    tensorflowjs_converter \                            
    --input_format=tf_saved_model \   
    --output_format=tfjs_graph_model \    
    --saved_model_tags=serve \
    --signature_name=serving_default \
    /saved_model \
   /json-model

When run model.predict(), it's throwing these errors

Error: The shape of dict['input_tensor'] provided in model.execute(dict) must be [1,-1,-1,3], but was [1,600,800,4]
    at Object.assert (/object-detection/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:337:15)
    at /object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:7478:28
    at Array.forEach (<anonymous>)
    at GraphExecutor.checkInputShapeAndType (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:7470:29)
    at GraphExecutor.<anonymous> (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:7272:34)
    at step (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:81:23)
    at Object.next (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:62:53)
    at /object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:55:71
    at new Promise (<anonymous>)
    at __awaiter (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:51:12) 

I have also tried model.executeAsync() instead of model.predict(), executeAsync() NOTE: When use with SavedModel(without the conversion) it works for both image types

for jpg image

Image: 1440000 bytes with shape: Tensor {
  kept: false,
  isDisposedInternal: false,
  shape: [ 1, 600, 800, 3 ],
  dtype: 'int32',
  size: 1440000,
  strides: [ 1440000, 2400, 3 ],
  dataId: {},
  id: 918,
  rankType: '4',
  scopeId: 2
}

Error: Invalid TF_Status: 3
Message: In[0] and In[1] has different ndims: [1,8,8,64,2] vs. [2,1]

for png image

Error: The shape of dict['input_tensor'] provided in model.execute(dict) must be [1,-1,-1,3], but was [1,600,800,4]

If I add ----control_flow_v2=True to do the conversion, it will fail to loadGraphModel.

I'm running @tensorflow/tfjs-node v 3.7.0 and I'm still getting this error "Cannot read property 'outputs' of undefined" when tried to load model json that was converted from saved model using tensorflowjs_converter.
When I changed to @tensorflow/tfjs-node@next, it would throw "Cannot read property 'children' of undefined"

model = await tf.loadGraphModel(modelPath);

2021-07-10 13:26:00.618147: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

TypeError: Cannot read property 'outputs' of undefined
at /object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:3851:31
at Array.forEach ()
at /object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:3848:29
at Array.forEach ()
at OperationMapper.mapFunction (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:3846:18)
at /object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:3679:56
at Array.reduce ()
at OperationMapper.transformGraph (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:3678:48)
at GraphModel.loadSync (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:7763:68)
at GraphModel. (/object-detection/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:7737:52)
rthadur commented 3 years ago

Can you try to reshape like this tensor = tensor.reshape([1, 224, 224, 3]); before you call predict ?

playground commented 3 years ago

Yes I did that too, that didn't help.

playground commented 3 years ago

Also with tfjs-node v3.8.0 seems to have broken the functionality for loadSavedModel() for MacOS. Looks like a regression bug, I have posted that here https://github.com/tensorflow/tensorflow/issues/38260/#issuecomment-881928290

rthadur commented 3 years ago

In order to expedite the trouble-shooting process, please provide a code pen example or sample code to reproduce the issue. Thanks!

playground commented 3 years ago

Basically this is what I did tfnode.loadGraphModel(modelUrl); const image = fs.readFileSync(imageFile); let decodedImage = tfnode.node.decodeImage(image);

tried both predict and executeAsync

I can share the converted model.json if that will help.

rthadur commented 3 years ago

sure, if possible a working codepen example or a git repo. Thank you

playground commented 3 years ago

Here is the converted model.json https://github.com/playground/tfjs-object-detection/tree/main/model-json-service/model

playground commented 3 years ago

@rthadur v3.8.0 seems to have broken for Mac for loadSavedModel, model.predict() is throwing this error

Error: Session fail to run with error: Cannot parse tensor from proto: dtype: DT_VARIANT
tensor_shape {
}
variant_val {
  type_name: "tensorflow::TensorList"
  metadata: "\001\000\001\377\377\377\377\377\377\377\377\377\001\030\001"
}

         [[{{node StatefulPartitionedCall/StatefulPartitionedCall/map/TensorArrayV2_1/_9__cf__9}}]]
google-ml-butler[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.

playground commented 3 years ago

Any update?

lucky0604 commented 3 years ago

Any update? I meet the same issue, but the different thing is when I use tf.browser.fromPixels(imgPath), I got the shape 3(it's a JPG file), not 4(as the doc says it should be 4). the img path is an image URL that stores in the cloud. The same thing is that when I use the model.execute or model.predict, I got the Uncaught (in promise) Error: The shape of dict['input_tensor'] provided in model.execute(dict) must be [1,-1,-1,3], but was [711,474,3] error.

gaikwadrahul8 commented 1 year ago

Hi, @playground

Apologize for the delayed response and we're re-visiting our older issues and checking whether those issues got resolved or not as of now so May I know are you still looking for the solution or your issue got resolved ?

If issue still persists after trying with latest version of TFJs please let us know with error log and code snippet to replicate the same issue from our end ?

Could you please confirm if this issue is resolved for you ? Please feel free to close the issue if it is resolved ? Thank you!

google-ml-butler[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.

playground commented 1 year ago

Yes, this issue still exists, please keep this open until it's resolved.

gaikwadrahul8 commented 1 year ago

Hi, @playground

Apologize for the delayed response and I was trying to replicate the same issue from my end and I'm getting below error and I have added error log below so Could you please help me with the steps to replicate the same issue from my end ?

I have tried your code snippet from here and also I used the same model from your Github repo which you converted with the help of TFJs convertor so may be I'm doing something wrong here to replicate the same issue from my end. Thank you!

gaikwadrahul-macbookpro:TFJS gaikwadrahul$ node index.js
/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:25608
                        throw new Error(message);
                              ^

Error: Failed to parse model JSON of response from https://github.com/playground/tfjs-object-detection/blob/main/model-json-service/model/model.json. Please make sure the server is serving valid JSON for this request.
    at HTTPRequest.<anonymous> (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:25608:31)
    at step (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:138:27)
    at Object.throw (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:87:53)
    at rejected (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:74:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.15.0
gaikwadrahul-macbookpro:TFJS gaikwadrahul$ 
pyu10055 commented 1 year ago

@gaikwadrahul8 I think you need to try the raw link of the model https://raw.githubusercontent.com/playground/tfjs-object-detection/main/model-json-service/model/model.json

github-actions[bot] commented 1 year ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 1 year ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 1 year ago

Are you satisfied with the resolution of your issue? Yes No

gaikwadrahul8 commented 1 year ago

Hi, @playground

I'm really sorry due to google-ml-butler bot this issue got closed so now I have re-opened this issue, we'll update you about this issue soon. Thank you!

gaikwadrahul8 commented 1 year ago

Hi, @playground

Apologize for the delayed response and I tried to replicate the same issue from my end and it's giving similar error message so we'll have to dig more into this issue and will update you soon, thank you for noticing this issue I really appreciate your efforts and time. Thank you!

CC :@mattsoulanille

Here is error log output for your reference, I tried with .predict(), .execute() and .executeAsync() :

gaikwadrahul-macbookpro:test-5366 gaikwadrahul$ node index.js
/Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:454
        throw new Error(typeof msg === 'string' ? msg : msg());
              ^

Error: The shape of dict['input_tensor'] provided in model.execute(dict) must be [1,-1,-1,3], but was [400,600,3]
    at Object.assert (/Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:454:15)
    at /Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:31349:26
    at Array.forEach (<anonymous>)
    at GraphExecutor.checkInputShapeAndType (/Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:31341:29)
    at GraphExecutor.execute (/Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:30855:14)
    at GraphModel.execute (/Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:31913:36)
    at GraphModel.predict (/Users/gaikwadrahul/Desktop/TFJS/test-5366/node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:31758:34)
    at file:///Users/gaikwadrahul/Desktop/TFJS/test-5366/index.js:9:33
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.15.0
gaikwadrahul-macbookpro:test-5366 gaikwadrahul$ 
playground commented 1 year ago

Thanks @gaikwadrahul8 , I look forward to your update. I also have this issue sitting out there for sometime https://github.com/tensorflow/tfjs/issues/7398#issuecomment-1503170942, can you please provide an update there too?