react-native - 为什么无论给定的张量如何,tensorflowjs 中的 model.predict 都会返回相同的错误输出?

标签 react-native tensorflow tensorflow.js

我正在尝试将一个 keras 模型转换为tensorflow js,以便在 native react 中工作,但该模型始终给出错误的响应。做了一些挖掘,发现我传递到 model.predict 的张量是如何改变的,导致它给出相同的错误预测。任何建议,将不胜感激。我几乎陷入困境。代码如下:

import React, {useState, useEffect} from 'react';
import {View, Text, Button} from 'react-native';
import * as tf from '@tensorflow/tfjs';
import {
  bundleResourceIO
} from '@tensorflow/tfjs-react-native';
import * as mobilenet from '@tensorflow-models/mobilenet';

function thing() {
  const [model, setModel] = useState(null);
  const [tensor, setTensor] = useState(null); 
  
  async function loadModel() {
    const modelJson = require('./assets/model.json');
    const weight = require('./assets/group1-shard1of1.bin');
    const backend = await tf.ready();
    const item = await tf.loadLayersModel(
        bundleResourceIO(modelJson, weight)
    );
    const tfTensor = tf.tensor([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]);
    
    setModel(item);
    setTensor(tfTensor);
  }

  useEffect(() => {
    loadModel();
  }, []);

  async function test() {
    if(tensor !== null && model !== null) {
      const result = await model.predict(tensor);
      console.log(result.dataSync())
    }
  }

  return (
    <View>
      <Button
        onPress={test}
        title="click"
        color="#841584"
        accessibilityLabel="Learn more about this purple button"
      />

    </View>
  );
}

export default thing;


最佳答案

就像更改图像中的单个像素不会更改图像一样,更改数组中的一位也不会显着调整预测。

我在黑色 224x224 图像上运行 mobilenet,它预测了 819 类(无论是什么)。然后我将左上角像素更改为白色并重新运行 mobilenet,它仍然分类为 819 类。

See example code here

更改单个位不会像哈希函数那样产生级联效应。 Mobilenet 本质上具有抗噪声能力。

关于react-native - 为什么无论给定的张量如何,tensorflowjs 中的 model.predict 都会返回相同的错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68383643/

相关文章:

python - TensorFlow 获取特定列的每一行的元素

android - 在 C++ 应用程序中使用 TensorFlow 模型

javascript - 将预测张量转换为图像

node.js - 如何保存/加载张量以加快训练速度?

reactjs - 未找到模块 : Can't resolve 'react'

ios - 在 TestFlight 中测试推送通知

python - 在 Intel Pentium 上安装 Tensorflow 和 Keras

react-native - 当我触摸我的文本输入时,我希望键盘根本不显示-React-Native

reactjs - 世博会错误: Cannot read property 'statusBarHeight' of null

tensorflow - 如何将 tensorflow.js 模型和权重转换为标准 tensorflow?