1

I am struggling to load a locally hosted Onnyxruntime model in ReactNative.

Imports:

import { Asset } from 'expo-asset';
import { InferenceSession } from "onnxruntime-react-native";

Here is what I am doing and the error this gives me.

const startOnnxRuntimeSession = async () => {
        //path to model:
        const model = require('../assets/model.ort');

        const asset = Asset.fromModule(model);
        if (!asset.localUri) {
            await asset.downloadAsync();
          }

        // load a model
        if (onnxSession !== null) {
            const session: InferenceSession = await InferenceSession.create(asset.localUri);
            setOnnxSession(session);
            console.log("Onnx session started successfully");
        }
    }

The error that I am getting is that the model is null

Error: Can't load a model: null is not an object (evaluating '(0, _classPrivateFieldLooseBase2.default)(this, _inferenceSession)[_inferenceSession].loadModel')

I also tried just passing the path '../assets/model.ort' to the InferenceSession

const session: InferenceSession = await InferenceSession.create('../assets/model.ort');

This gives the same error "Null"

Cyprian
  • 9,423
  • 4
  • 39
  • 73

1 Answers1

0

Did you try logging to see if the localUri is the expected path? The expected path should be accessible by the lib, using RNFS I would do something like this:

let path = RNFS.DocumentDirectoryPath + '/model.ort'
await RNFS.downloadFile({ fromUrl: <file uri>, toFile: path }).promise

const InferenceSession = await InferenceSession.create(path);
Ekaansh Arora
  • 1,168
  • 6
  • 15
  • The file uri can not be an online file uri because model size often gets too large if you really want to do advance stuff. If it is a local file, where the URI should be? Like '/model.ort'? – Jonathan Sum Mar 25 '22 at 14:58
  • 1
    https://github.com/microsoft/onnxruntime/issues/10302 You can see some people have the same issue too. And I also put the reproduce example over there. – Jonathan Sum Mar 25 '22 at 17:19