【发布时间】:2021-04-22 09:23:11
【问题描述】:
编辑:看到有很多类似的问题,但是没有一个解决方案对我有用。认为这可能是有用的信息:在我的 model.json 文件中,我有以下内容:"tensorShape": {"dim": [{"size": "3"}, {"size": "3"}, {"size": "512"}, {"size": "1"}]}}}
这里有两个错误。我有一个 keras 模型,它已转换为 tfjs_graph_model,当给定 单个图像 时,它应该返回 二元分类。让它在 tensorflowjs 上运行是一个挑战,因为这是我第一次使用这项技术。
第一个 Error: The shape of dict['input_1'] provided in model.execute(dict) must be [-1,380,380,1], but was [1,380,380,3] 来自我的 react 组件中的以下 sn-p 代码。
const imageElement = document.createElement("img");
imageElement.src = file;
imageElement.width = 380;
imageElement.height = 380;
imageElement.onload = async () => {
const tensor = tf.browser
.fromPixels(imageElement)
.resizeNearestNeighbor([380, 380])
.expandDims()
.toFloat();
const prediction = await model.predict(tensor).data();
console.log(prediction);
setPrediction(parseInt(prediction, 10));
setProcessing(false);
setImageLoaded(false);
};
但是,当我似乎使用以下 sn-p 将其大小调整为 [-1,380,380,1] 时,它会返回错误 Error: Tensor must have a shape comprised of positive integers but got shape [-1,380,380,1].
const imageElement = document.createElement("img");
imageElement.src = file;
imageElement.width = 380;
imageElement.height = 380;
imageElement.onload = async () => {
const tfImg = tf.browser.fromPixels(imageElement);
const smalImg = tf.image.resizeBilinear(tfImg, [380, 380]);
const resized = tf.cast(smalImg, "float32");
const tensor = tf.tensor4d(Array.from(resized.dataSync()), [
-1,
380,
380,
1,
]);
const prediction = await model.predict(tensor).data();
console.log(prediction);
setPrediction(parseInt(prediction, 10));
setProcessing(false);
setImageLoaded(false);
};
如果我尝试将其调整为[1,380,380,1],则错误为Error: Based on the provided shape, [1,380,380,1], the tensor should have 144400 values but has 433200。
我不明白为什么会发生这种情况,或者我是否正在调整图像大小或正确转换它。
谁能解释一下这个问题?
【问题讨论】:
-
keras 模型的输入形状是什么?
-
你能不能试试
const tensor = resized.reshape([1, 380,380,1]),然后用const prediction = await model.predict(tensor, { batchSize: 1 });做预测?
标签: reactjs keras tensorflow.js