导入您自己的张量流模型以响应本机

Importing your own tensorflow model to react native

我有一个我训练过的模型并存储为 h5 文件, 我使用 tensorflowJs 转换器将其转换为 json 文件和权重。 我正在使用 expo,我想加载该模型,我知道 bundleResourceIO 不适用于 exp,我想从网络服务器加载它, 但是我找不到任何教程或指南,这可能吗?

您可以使用 bundleResourceIO.

如果您有多个 bin 文件,则再次重新转换并设置 --weight_shard_size_bytes 60000000 这会将 weight_shard_size_bytes 文件的最大大小设置为 60Mb。

import {bundleResourceIO} from "@tensorflow/tfjs-react-native";


 //Loading model from models folder
  const modelJSON = require("../model/model.json");
  const modelWeights = require("../model/group1-shard1of1.bin");


// Load the model from the models folder
  const loadModel = async () => {
    const model = await tf
      .loadLayersModel(bundleResourceIO(modelJSON, modelWeights))
      .catch(e => console.log(e));
    console.log("Model loaded!");
    return model;
  };

依赖关系:

"dependencies": {
    "@react-native-community/async-storage": "^1.12.0",
    "@react-native-community/masked-view": "0.1.10",
    "@react-navigation/native": "^5.7.3",
    "@react-navigation/stack": "^5.9.0",
    "@tensorflow-models/blazeface": "^0.0.5",
    "@tensorflow/tfjs": "^2.0.0",
    "@tensorflow/tfjs-backend-webgl": "2.0.0",
    "@tensorflow/tfjs-converter": "^1.5.2",
    "@tensorflow/tfjs-core": "2.0.0",
    "@tensorflow/tfjs-react-native": "^0.3.0",
    "expo": "~38.0.8",
    "expo-camera": "~8.3.1",
    "expo-gl": "^8.4.0",
    "expo-gl-cpp": "~8.3.1",
    "expo-status-bar": "^1.0.2",
    "jpeg-js": "^0.4.2",
    "react": "~16.11.0",
    "react-dom": "~16.11.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
    "react-native-canvas": "^0.1.37",
    "react-native-fs": "^2.16.6",
    "react-native-gesture-handler": "~1.6.0",
    "react-native-reanimated": "~1.9.0",
    "react-native-safe-area-context": "~3.0.7",
    "react-native-screens": "~2.9.0",
    "react-native-web": "~0.11.7"
  },

我已经通过添加 metro.config.js 文件解决了这个问题。 将以下代码添加到文件中,使其能够读取bin文件。

const { getDefaultConfig } = require('metro-config');
module.exports = (async () => {
  const defaultConfig = await getDefaultConfig();
  const { assetExts } = defaultConfig.resolver;
  return {
    resolver: {
      // Add bin to assetExts
      assetExts: [...assetExts, 'bin'],
    }
  };
})();

如果您需要视频指南,请观看此视频:https://www.youtube.com/watch?v=pC7mCEHiYQw