【问题标题】:Error on provided shape, expected (1200000) and actual (400000) tensor values are different提供的形状错误,预期 (1200000) 和实际 (400000) 张量值不同
【发布时间】:2022-01-20 01:58:10
【问题描述】:

有没有办法让输入图像值与预期值匹配?我知道的输入图像的宽度是 800px,高度是 500px,并且是彩色的(意思是 3 个通道),所以形状应该是 [800,500,3]。 但是,我收到以下错误:

错误:Error: Based on the provided shape, [800,500,3], the tensor should have 1200000 values but has 400000

有没有什么方法可以给图像赋值 1200000,或者可以将形状调整为 400000 但仍然保留 3 个通道的 800x500 尺寸?

代码:

var tf = require('@tensorflow/tfjs-node');
const fs = require(`fs`)
const Jimp = require(`jimp`)

const image = `./1-1.png`

const imageWidth = 800;
const imageHeight = 500;
const imageChannels = 3;

const getData = async function (path) {
  const data = [];
  const image = await Jimp.read(path);
  await image
      .scan(0, 0, imageWidth, imageHeight, (x, y, idx) => {
        let v = image.bitmap.data[idx + 0];
        data.push(v / 255);
      });
  return data;
};

const createImage = async (data) => {
  const imTen = tf.tensor(data, [imageWidth, imageHeight, 3]);
  const inTen = imTen.expandDims();
  return inTen;
}

const main = async () => {
  const model = await tf.loadLayersModel('file:///retake/savedmodels/model.json');
  model.summary();

  const a = await getData(image)
  const b = await createImage(a)

  const tfImage = b
  console.log(im)

  const prediction = model.predict(tfImage);
  prediction.print();
}
main()

【问题讨论】:

    标签: node.js tensorflow tensor tensorflow.js jimp


    【解决方案1】:

    据我所知,输入图像的宽度为 800 像素,高度为 500 像素,并且是彩色的(意思是 3 个通道)

    检查您的 getData 方法的返回值,因为它似乎返回的灰度图像 - 这将匹配 400,000 而不是预期的 1,200,000 值

    【讨论】:

    • 当我将所有内容更改为适合 1 个通道(灰度)时,我收到此错误:ValueError: Error when checking : expected flatten_Flatten1_input to have shape [null,7,7,256] but got array with shape [1,800,500,1].
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多