tf.image.cropAndResize 抛出 "method must be bilinear or nearest, but was undefined" 错误
tf.image.cropAndResize throwing "method must be bilinear or nearest, but was undefined" error
我正在尝试使用 tensorflow.js(特别是 tfjs-node)在 firebase 云函数中 运行 图像分类模型,但是 运行 遇到了流动错误:
Error: method must be bilinear or nearest, but was undefined
at assert (/workspace/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:698:15)
at cropAndResize_ (/workspace/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:21340:5)
at Object.cropAndResize__op [as cropAndResize] (/workspace/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4287:29)
at prepImage (/workspace/handlers/models.js:58:35)
at /workspace/handlers/models.js:68:44
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async exports.isFurnished (/workspace/handlers/models.js:10:17)
at async exports.getanalysis (/workspace/handlers/apis.js:103:16)
tf.image.cropAndResize()
函数正在抛出错误。此错误的奇怪之处在于 cropAndResize()
应自动使用其在 docs.
中指定的默认值“双线性”
奇怪的是,当我在本地 运行 时,我没有收到任何错误。我的本地机器是 运行ning node v12.16.0.
下面是我的代码。请注意,我只是从 firebase 存储中加载 signature.json 并获取/标准化图像(我没有加载和 运行 实际的 ts 模型)。
const { admin, db } = require("../util/admin");
const firebase = require("firebase");
const tf = require("@tensorflow/tfjs-node");
const fetch = require("node-fetch");
exports.isFurnished = async (imgUrl) => {
const sigPath = "models/signature.json";
const signature = await loadSignature(sigPath);
const image = await loadImage(imgUrl, signature);
return "It worked!";
};
//signature---------------------------
const loadSignature = (filePath) => {
let file = admin.storage().bucket().file(filePath);
return file
.download()
.then((res) => JSON.parse(res[0].toString("utf8")))
.catch((err) => err.message);
};
//Image-------------------------------
const loadImage = (imgUrl, signature) => {
return fetchImage(imgUrl).then((image) => prepImage(image, signature));
};
const fetchImage = async (url) => {
const response = await fetch(url);
const buffer = await response.buffer();
return buffer;
};
const prepImage = (rawImage, signature) => {
const image = tf.node.decodeImage(rawImage, 3);
const [height, width] = signature.inputs.Image.shape.slice(1, 3);
const [imgHeight, imgWidth] = image.shape.slice(0, 2);
const normalizedImage = tf.div(image, tf.scalar(255));
const reshapedImage = normalizedImage.reshape([1, ...normalizedImage.shape]);
let top = 0;
let left = 0;
let bottom = 1;
let right = 1;
if (imgHeight != imgWidth) {
const size = Math.min(imgHeight, imgWidth);
left = (imgWidth - size) / 2 / imgWidth;
top = (imgHeight - size) / 2 / imgHeight;
right = (imgWidth + size) / 2 / imgWidth;
bottom = (imgHeight + size) / 2 / imgHeight;
}
return tf.image.cropAndResize(
reshapedImage,
[[top, left, bottom, right]],
[0],
[height, width]
);
};
我是否犯了一个我没有看到的错误,或者这是一个节点 and/or tsjs 问题?
此外,添加“双线性”参数会产生此错误:
Error: Invalid napi_status: A number was expected
我正在使用 bodypix。直到今天早上它都工作正常。虽然我没有做任何更改,但从今天下午开始就出现了这个确切的错误。这可能是 Tensorflow 的问题。或者,
我查看了 Windows 8.1。在那里,它工作得很好。问题发生在 windows 10.
编辑:我很确定它来自 TensorFlow。不是 windows。我正在使用 CDN 获取 bodypix,更新 cdn 地址后错误消失了。
上一个:https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix/dist/body-pix.min.js
https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js
现在:https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix@2.0.5/dist/body-pix.min.js
https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.7.0/dist/tf.min.js
正如评论 ,TensorFlow.js 版本 2.8.0 似乎引入了一些重大变化。解决方法(在撰写本文时)是继续使用 2.7.0 版。
我正在尝试使用 tensorflow.js(特别是 tfjs-node)在 firebase 云函数中 运行 图像分类模型,但是 运行 遇到了流动错误:
Error: method must be bilinear or nearest, but was undefined
at assert (/workspace/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:698:15)
at cropAndResize_ (/workspace/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:21340:5)
at Object.cropAndResize__op [as cropAndResize] (/workspace/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4287:29)
at prepImage (/workspace/handlers/models.js:58:35)
at /workspace/handlers/models.js:68:44
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async exports.isFurnished (/workspace/handlers/models.js:10:17)
at async exports.getanalysis (/workspace/handlers/apis.js:103:16)
tf.image.cropAndResize()
函数正在抛出错误。此错误的奇怪之处在于 cropAndResize()
应自动使用其在 docs.
奇怪的是,当我在本地 运行 时,我没有收到任何错误。我的本地机器是 运行ning node v12.16.0.
下面是我的代码。请注意,我只是从 firebase 存储中加载 signature.json 并获取/标准化图像(我没有加载和 运行 实际的 ts 模型)。
const { admin, db } = require("../util/admin");
const firebase = require("firebase");
const tf = require("@tensorflow/tfjs-node");
const fetch = require("node-fetch");
exports.isFurnished = async (imgUrl) => {
const sigPath = "models/signature.json";
const signature = await loadSignature(sigPath);
const image = await loadImage(imgUrl, signature);
return "It worked!";
};
//signature---------------------------
const loadSignature = (filePath) => {
let file = admin.storage().bucket().file(filePath);
return file
.download()
.then((res) => JSON.parse(res[0].toString("utf8")))
.catch((err) => err.message);
};
//Image-------------------------------
const loadImage = (imgUrl, signature) => {
return fetchImage(imgUrl).then((image) => prepImage(image, signature));
};
const fetchImage = async (url) => {
const response = await fetch(url);
const buffer = await response.buffer();
return buffer;
};
const prepImage = (rawImage, signature) => {
const image = tf.node.decodeImage(rawImage, 3);
const [height, width] = signature.inputs.Image.shape.slice(1, 3);
const [imgHeight, imgWidth] = image.shape.slice(0, 2);
const normalizedImage = tf.div(image, tf.scalar(255));
const reshapedImage = normalizedImage.reshape([1, ...normalizedImage.shape]);
let top = 0;
let left = 0;
let bottom = 1;
let right = 1;
if (imgHeight != imgWidth) {
const size = Math.min(imgHeight, imgWidth);
left = (imgWidth - size) / 2 / imgWidth;
top = (imgHeight - size) / 2 / imgHeight;
right = (imgWidth + size) / 2 / imgWidth;
bottom = (imgHeight + size) / 2 / imgHeight;
}
return tf.image.cropAndResize(
reshapedImage,
[[top, left, bottom, right]],
[0],
[height, width]
);
};
我是否犯了一个我没有看到的错误,或者这是一个节点 and/or tsjs 问题?
此外,添加“双线性”参数会产生此错误:
Error: Invalid napi_status: A number was expected
我正在使用 bodypix。直到今天早上它都工作正常。虽然我没有做任何更改,但从今天下午开始就出现了这个确切的错误。这可能是 Tensorflow 的问题。或者,
我查看了 Windows 8.1。在那里,它工作得很好。问题发生在 windows 10.
编辑:我很确定它来自 TensorFlow。不是 windows。我正在使用 CDN 获取 bodypix,更新 cdn 地址后错误消失了。
上一个:https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix/dist/body-pix.min.js https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js
现在:https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix@2.0.5/dist/body-pix.min.js https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.7.0/dist/tf.min.js
正如评论