【问题标题】:Error when checking : expected input to have shape [null,300,300,3] but got array with shape [1,300,300,4]检查时出错:预期输入的形状为 [null,300,300,3] 但得到的数组形状为 [1,300,300,4]
【发布时间】:2023-02-18 08:03:52
【问题描述】:

我正在使用 tfjs-node 在我的 Node.js 应用程序中加载模型和预测结果。它提供了不错的结果,但对于某些图像,显示了以下错误:

Error when checking : expected input to have shape [null,300,300,3] but got array with shape [1,300,300,4].

加载和预测结果的代码:

const loadModel = async (imagePath) => {
  const image = fs.readFileSync(imagePath);
  let tensor = tf.node.decodeImage(image);

  const resizedImage = tensor.resizeNearestNeighbor([300, 300]);
  const batchedImage = resizedImage.expandDims(0);
  const input = batchedImage.toFloat().div(tf.scalar(255));

  const model = await tf.loadLayersModel(
    process.env.ML_MODEL_PATH || "file://./ml-model/model.json"
  );

  let predictions = await model.predict(input).data();
  predictions = Array.from(predictions);
};

如何解决这个问题?谢谢。

【问题讨论】:

    标签: tensorflow tensorflow.js tfjs-node


    【解决方案1】:

    使用

    let tensor = tf.node.decodeImage(image, 3);
    

    如果允许图像具有像 png 这样的透明度,它将删除 alpha。

    Tensorflow documentation source.

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 2020-06-30
      • 2020-09-04
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2021-02-07
      相关资源
      最近更新 更多