我发现错误状态Shapes-1并且必须匹配-1和什么?我不完全确定tf将-1与什么进行比较。我一定是做错了什么,但我似乎不明白这一点。如有任何帮助,我们将不胜感激。
目标是在这一点上只探索自定义模型(从头开始训练(的预测。我在另一个tfjs应用程序中使用了这个web_model,并且我能够在class ... extends React.Component
方法的上下文中获得预测,但最终出现了问题,所以我进行了重构。
这是图像预处理功能:
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);
});
};
这是模型console.log:除非我弄错了,否则模型预期的是[1640640,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
这是输入console.log:同样,除非我弄错了,否则输入是[11640640,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(), … }
编辑:尝试一个简化的测试来帮助缩小问题范围。
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);
错误:
Uncaught (in promise) Error: TensorList shape mismatch: Shapes -1 and must match
编辑2:我在3.11.0降级为tensorflow/tfjs。web_model现在似乎正在运行。我需要做更多的测试,然后才能给这个问题打电话。
降级至tensorflow/tfjs@3.11.0。我将在适当的时候测试其他版本,甚至可能找出问题的根源,但到目前为止,3.11.0在3.15.0版本上运行,没有对代码或web_model进行其他更改。我希望这能帮助到别人。