【问题标题】:Tensorflowjs Unhandled Rejection (Error): The shape of dict['input_1'] provided in model.execute(dict) must be [-1,380,380,1], but was [1,380,380,3]Tensorflowjs Unhandled Rejection(错误):model.execute(dict)中提供的dict['input_1']的形状必须是[-1,380,380,1],但是是[1,380,380,3]
【发布时间】: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


【解决方案1】:

形状不匹配。您正在为图像使用 3 个通道而不是单个通道。

有多种方法可以根据彩色图像(通道 = 3)计算灰度图像(通道 = 1)。此处应应用训练模型时应用的相同处理。但是,如果您正在寻找一种将您的形状转换为模型预期形状的快速方法,那么这个方法可以满足您的需求:

const tensorWithCorrectShape = tf.mean(tensor, 3, true)

answer 讨论了几种获取灰度图像的方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 2021-06-20
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多