Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile] Number of elements in buffer is changed when running InferenceSession #21945

Closed
deKeijzer opened this issue Aug 31, 2024 · 1 comment
Labels
api:Javascript issues related to the Javascript API platform:mobile issues related to ONNX Runtime mobile; typically submitted using template platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@deKeijzer
Copy link

Describe the issue

Hi all,

After a long search I end up opening a GitHub Issue. We are able to run our Expo 51.0.31 app with react-native 0.74.5 on ios perfectly. Recently we tried to do so on a Galaxy Tab A (SM-T510) with Android version 10. Here it appears that the input buffer is modified by onnxruntime-react-native 1.19.0. It is visible with the following code outline.

import { InferenceSession } from 'onnxruntime-react-native';
const session = await InferenceSession.create(modelUri);
const outputs = await session.run(inputs);

Here inputs is the tensor

        const inputTensor = new Tensor('float32', preprocessedData, [1, 3, 640, 640]);
        console.log('Input Tensor Dims:', inputTensor.dims);
        console.log('Input Tensor Buffer Length:', inputTensor.data.length);

with console output

 LOG  Input Tensor Dims: [1, 3, 640, 640]
 LOG  Input Tensor Buffer Length: 1228800

If I then do

const inputs = { [session.inputNames[0]]: inputTensor };
const outputs = await session.run(inputs);

I get

Error: Shape [1, 3, 640, 640], requires 1228800 elements but the buffer has 1229880 elements.

during inference. This stops our model from running on android. The model used is yolov8n. Everything runs fine on ios.

I have not been able to find a similar issue. Is this known to be related to something, or known to have a solution? Any help would be appreciated!

To reproduce

{
"name": "handenscanzorg_app",
"main": "expo-router/entry",
"version": "1.0.0",
"scripts": {
"start": "expo start --dev-client",
"reset-project": "node ./scripts/reset-project.js",
"android": "expo run:android",
"ios": "expo run:ios",
"web": "expo start --web",
"test": "jest --watchAll",
"lint": "expo lint"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"@expo-google-fonts/amatic-sc": "^0.2.3",
"@expo-google-fonts/inter": "^0.2.3",
"@expo/config-plugins": "~8.0.0",
"@expo/vector-icons": "^14.0.0",
"@react-native-async-storage/async-storage": "1.23.1",
"@react-navigation/native": "^6.0.2",
"@tensorflow/tfjs": "^4.20.0",
"@tensorflow/tfjs-react-native": "^1.0.0",
"base64-arraybuffer": "^1.0.2",
"expo": "^51.0.31",
"expo-asset": "^10.0.9",
"expo-camera": "~15.0.15",
"expo-constants": "~16.0.2",
"expo-dev-client": "~4.0.25",
"expo-font": "~12.0.7",
"expo-gl": "~14.0.2",
"expo-image-manipulator": "~12.0.5",
"expo-image-picker": "~15.0.7",
"expo-linking": "~6.3.1",
"expo-router": "~3.5.23",
"expo-screen-orientation": "~7.0.5",
"expo-splash-screen": "~0.27.5",
"expo-status-bar": "~1.12.1",
"expo-system-ui": "~3.0.7",
"onnxruntime-react-native": "1.19.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.74.5",
"react-native-canvas": "^0.1.40",
"react-native-fs": "^2.20.0",
"react-native-gesture-handler": "~2.16.1",
"react-native-reanimated": "~3.10.1",
"react-native-safe-area-context": "4.10.5",
"react-native-screens": "3.31.1",
"react-native-svg": "15.2.0",
"react-native-svg-transformer": "^1.4.0",
"react-native-view-shot": "^3.8.0",
"react-native-webview": "13.8.6"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@types/jest": "^29.5.12",
"@types/react": "~18.2.45",
"@types/react-native": "^0.73.0",
"@types/react-test-renderer": "^18.0.7",
"jest": "^29.2.1",
"jest-expo": "~51.0.4",
"react-test-renderer": "18.2.0",
"typescript": "~5.3.3"
},
"private": true
}

npm install with legacy deps.

Urgency

No response

Platform

Android

OS Version

10

ONNX Runtime Installation

Released Package

Compiler Version (if 'Built from Source')

No response

Package Name (if 'Released Package')

onnxruntime-react-native

ONNX Runtime Version or Commit ID

1.19.0

ONNX Runtime API

JavaScript

Architecture

Other / Unknown

Execution Provider

Default CPU

Execution Provider Library Version

No response

@deKeijzer deKeijzer added the platform:mobile issues related to ONNX Runtime mobile; typically submitted using template label Aug 31, 2024
@github-actions github-actions bot added api:Javascript issues related to the Javascript API platform:web issues related to ONNX Runtime web; typically submitted using template labels Aug 31, 2024
@deKeijzer
Copy link
Author

Solved! Tensorflow did not output float32 as expected. The below code resolves the issue.

        const preprocessedDataFloat32 = new Float32Array(preprocessedData);
        const inputTensor = new Tensor('float32', preprocessedDataFloat32, [1, 3, 640, 640]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:Javascript issues related to the Javascript API platform:mobile issues related to ONNX Runtime mobile; typically submitted using template platform:web issues related to ONNX Runtime web; typically submitted using template
Projects
None yet
Development

No branches or pull requests

1 participant