'Uncaught (in promise) Error: TensorList shape mismatch: Shapes -1 and must match

I find it curious that the error states Shapes -1 and must match. ... -1 and what? I'm not entirely sure what tf is comparing the -1 against. I must be doing something obviously wrong but I cannot seem to figure this one out. Any help would be greatly appreciated.

The goal is to just explore the predictions of the custom model (trained from scratch) at this point. I have used this web_model in a different tfjs app, and I was able to get predictions within the context of a class ... extends React.Component approach but that ended up being problematic, so I refactored.

This is the image pre-process function:

  const detectObjectsOnImage = async (imageElement, imgSize) => {
    const model = await tf.loadGraphModel(weights)
    let [modelWidth, modelHeight] = model.inputs[0].shape.slice(1, 3);
    const tensor = await tf.browser.fromPixels(imageElement)
    
    const input = tf.tidy(() => {
      return tf.image.resizeBilinear(tensor, [modelWidth, modelHeight])
        .div(255.0).expandDims(0);
    });
    
    console.log("model: ", model)
    console.log("input: ", input)

    model.executeAsync(input).then(predictions => {
      console.log('Predictions: ');
      console.log(predictions);
    });
  };

Here is the model console.log: Unless I am mistaken, the model is expecting a shape of [1,640,640,3]

model:  
Object { modelUrl: "/web_model/model.json", loadOptions: {}, version: "987.undefined", handler: {…}, artifacts: {…}, executor: {…} }
artifacts: Object { generatedBy: "2.8.0", convertedBy: "TensorFlow.js Converter v3.14.0", format: "graph-model", … }
executor: Object { graph: {…}, parent: undefined, SEPERATOR: ",", … }
handler: Object { DEFAULT_METHOD: "POST", weightPathPrefix: undefined, path: "/web_model/model.json", … }
loadOptions: Object {  }
<prototype>: Object { … }
modelUrl: "/web_model/model.json"
version: "987.undefined"
<prototype>: Object { … }
constructor: class GraphModel { constructor(modelUrl, loadOptions) }​​
convertTensorMapToTensorsMap: function convertTensorMapToTensorsMap(map)​​
dispose: function dispose()​​
execute: function execute(inputs, outputs)​​
executeAsync: async function executeAsync(inputs, outputs)​​
findIOHandler: function findIOHandler()​​
inputNodes: 
inputs: Array [ {…} ]
0: Object { name: "x", shape: (4) […], dtype: "float32" }
dtype: "float32"
name: "x"
​​​​
shape: Array(4) [ 1, 640, 640, … ]
0: 1
1: 640
2: 640
3: 3
length: 4

<prototype>: Array []
<prototype>: Object { … }
length: 1
<prototype>: Array []
load: async function load()​​
loadSync: function loadSync(artifacts)​​
modelVersion: 
normalizeInputs: function normalizeInputs(inputs)​​
normalizeOutputs: function normalizeOutputs(outputs)​​
outputNodes: 
outputs: 
predict: function predict(inputs, config)​​
save: async function save(handlerOrURL, config)​​
weights: 
<get inputNodes()>: function inputNodes()​​
<get inputs()>: function inputs()​​
<get modelVersion()>: function modelVersion()​​
<get outputNodes()>: function outputNodes()​​
<get outputs()>: function outputs()​​
<get weights()>: function weights()​​
<prototype>: Object { … }
ImageBased.js:141

Here is the input console.log: Again, unless I am mistaken, the input is in a shape of [1,640,640,3]

input:  
Object { kept: false, isDisposedInternal: false, shape: (4) […], dtype: "float32", size: 1228800, strides: (3) […], dataId: {}, id: 361, rankType: "4", scopeId: 1 }
dataId: Object {  }
dtype: "float32"
id: 361
isDisposedInternal: false
kept: false
rankType: "4"
scopeId: 1
​
shape: Array(4) [ 1, 640, 640, … ]
0: 1
1: 640
2: 640
3: 3
length: 4

<prototype>: Array []
size: 1228800
strides: Array(3) [ 1228800, 1920, 3 ]
<prototype>: Object { abs: abs(), acos: acos(), acosh: acosh(), … }

Edit: Attempting a simplified test just to help narrow in on the problem.

let [modelWidth, modelHeight] = model.inputs[0].shape.slice(1, 3);
let warmup = tf.zeros([1, modelWidth, modelHeight, 3])
const res = await model.executeAsync(warmup);
console.log(res);

Error:

 Uncaught (in promise) Error: TensorList shape mismatch: Shapes -1 and must match

Edit 2: I downgraded to tensorflow/tfjs @ 3.11.0. web_model seems to be working now. I need to do more testing before I call this one answered.



Solution 1:[1]

Downgrade to tensorflow/tfjs @ 3.11.0. I will test other versions in due time, perhaps even hunt down the source of the problem but so far 3.11.0 works over 3.15.0 with no other changes made to the code, or the web_model. I hope this helps somebody.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeff