【问题标题】:How to reshape 3D tensor in Tensorflow.js to 4D tensor?如何将 Tensorflow.js 中的 3D 张量重塑为 4D 张量?
【发布时间】:2020-09-11 02:57:53
【问题描述】:

我正在使用将 [null, 224,224,3] 作为输入的自定义模型

但是当我尝试对模型进行预测时,出现以下错误。

Total size of new array must be unchanged.

正在传入的张量:

Tensor
  dtype: int32
  rank: 3
  shape: [224,224,3]
  values:
    [[[124, 130, 132],
      [137, 148, 147],
      [123, 134, 127],
      ...,
      [0  , 0  , 0  ],
      [0  , 0  , 0  ],
      [0  , 0  , 0  ]],
  const getPrediction = async tensor => {
    if (!tensor) {
      console.log("Tensor not found!");
      return;
    }
    const reshapeLayers = tf.layers.reshape({
      targetShape: [1, 224, 224, 3]
    });
    reshapeLayers.apply(tensor);
    const model = await loadedModel;

    const prediction = model.predict(reshapeLayers, 1);
    console.log(`Predictions: ${JSON.stringify(prediction)}`);

    if (!prediction || prediction.length === 0) {
      return;
    }

    // Only take the predictions with a probability of 30% and greater
    if (prediction[0].probability > 0.3) {
      //Stop looping
      cancelAnimationFrame(requestAnimationFrameId);
      setPredictionFound(true);
      setModelPrediction(prediction[0].className);
      tensor.dispose();
    }
  };

【问题讨论】:

  • 张量的形状是什么?
  • [224,224,3] 你可以在 Tensor.shape 下看到它
  • 你正在传递给 model.predict 一个层而不是一个张量。它应该是 model.predict(tensor.reshape([1,224,224,3]))
  • 是的!
  • 让我添加一个你可以投票的答案

标签: tensorflow deep-learning tensorflow-lite tensorflow.js


【解决方案1】:

您正在向model.predict 传递一个层而不是一个张量。应该是

model.predict(tensor.reshape([1,224,224,3]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 2018-10-26
    • 2022-01-18
    • 2021-07-19
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多