我的本机张量流模型(我使用tfjs转换器从keras转换而来)对每个图像给出了相同的预测

时间:2020-08-06 11:45:56

标签: reactjs react-native tensorflow keras tensorflow.js

因此,我针对3个类别的图像识别训练了一个keras模型。然后,我将此模型转换为可在TensorflowJs中使用,从而产生了model.json和权重文件。通过遵循此博客文章-> https://blog.tensorflow.org/2020/02/tensorflowjs-for-react-native-is-here.html

,我将此模型加载到了react native应用程序中

但是,无论我现在将哪个图像传递给该模型,它都给出相同的结果。 我首先使用以下预处理功能将图像转换为张量。

  // Function converts image to tensor
  const imageToTensorf = (rawImageData) => {
    console.log("Converting Image to tensor");
    const TO_UINT8ARRAY = true;
    const { width, height, data } = jpeg.decode(rawImageData, TO_UINT8ARRAY);
    console.log(`width of the image -> ${width} and ${height}`);

    const buffer = new Uint8Array(width * height * 3);
    let offset = 0;
    for (let i = 0; i < buffer.length; i += 3) {
      buffer[i] = data[offset];
      buffer[i + 1] = data[offset + 1];
      buffer[i + 2] = data[offset + 2];

      offset += 4;
    }

    const normed = [];
    for (let i = 0; i < buffer.length; i++) normed[i] = buffer[i] / 244.0; // Normalize

    return tf.tensor3d(normed, [height, width, 3]).expandDims();
  };

以下是我按下按钮预测特定图像后调用的功能。请注意,该图像位于我的资产文件夹中,并且我不是要对其进行拍照的相机,这只是出于测试目的。

  //Main Function
  async function predictPose() {
    console.log("Processing Images");
    const image = require("./assets/img/t4.jpg");
    const imageAssetPath = Image.resolveAssetSource(image);
    const response = await fetch(imageAssetPath.uri, {}, { isBinary: true });
    const rawImageData = await response.arrayBuffer();
    const imageTensor = imageToTensorf(rawImageData);

    console.log(`Shape of image tensor -> ${imageTensor.shape}`);

    const model = await tf
      .loadLayersModel(bundleResourceIO(modelPath, modelWeights))
      .then((m) => {
        console.log("Model Loaded Sucessfully !! ");

        console.log("here shape of image tensor is ", imageTensor.shape);

        const pred = m.predict(imageTensor);

        console.log("prediction complete");
        console.log(pred);

        console.log("//////////////////");

        console.log(pred.dataSync());
      })
      .catch((err) => console.log(`Error !! ${err}`));

    // 0 0 1

    return model;
  }

无论我通过哪张图片,它都始终给出相同的结果。我已经确定,在传递图像并将其标准化之前,我将图像的大小调整为224X224。 以下是我的日志:

Tensorflow is ready . . .
Processing Images
Converting Image to tensor
width of the image -> 224 and 224
Shape of image tensor -> 1,224,224,3
Model Loaded Sucessfully !! 
here shape of image tensor is  Array [
  1,
  224,
  224,
  3,
]
prediction complete
Tensor {
  "dataId": Object {},
  "dtype": "float32",
  "id": 1865,
  "isDisposedInternal": false,
  "kept": false,
  "rankType": "2",
  "scopeId": 969,
  "shape": Array [
    1,
    3,
  ],
  "size": 3,
  "strides": Array [
    3,
  ],
}
////////////////// Result : - //
Float32Array [
  0,
  0,
  1,
]

我已经在我的原始角膜模型上测试了相同的图像,它们给出了准确的预测。 任何帮助将不胜感激。

0 个答案:

没有答案