tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.37k stars 1.92k forks source link

RangeError: new TypedArray (buffer, [byteOffset], [length]): buffer's size must be evenly divisible by the element size #7936

Open Borgo99 opened 1 year ago

Borgo99 commented 1 year ago

Hello, I'm using React Native and I have an error just for iOS production, while it works fine for Android production and in the Expo app for both platforms.

My code is the following

const modelJson = require("./../assets/tfjs_model/model.json");
const modelWeights1 = require("./../assets/tfjs_model/group1-shard1of1.bin");
const bundleResources = bundleResourceIO(modelJson, modelWeights1);
this.TFModel = await tf.loadLayersModel(bundleResources);

As I said, the model is loaded without problem in the Expo app for both iOS and Android. When I do eas build to try my project in production it works perfectly for Android, while on different iOS device I got this error

RangeError: new TypedArray (buffer, [byteOffset], [length]): buffer's size must be evenly divisible by the element size

It seems to be a problem with .bin file, but I use always the same file that works well for Android and in the Expo app.

My dependencies are

"@react-native-async-storage/async-storage": "1.17.11", "@react-navigation/native": "^6.1.7", "@react-navigation/native-stack": "^6.9.12", "@tensorflow/tfjs-react-native": "^0.8.0", "expo": "~48.0.15", "expo-av": "~13.2.1", "expo-camera": "~13.2.1", "expo-gl": "~12.4.0", "expo-linking": "~4.0.1", "expo-status-bar": "~1.4.4", "expo-system-ui": "^2.2.1", "expo-tracking-transparency": "~3.0.3", "expo-updates": "~0.16.4", "i18next": "^23.2.0", "react": "18.2.0", "react-i18next": "^13.0.0", "react-native": "0.71.8", "react-native-fs": "^2.20.0", "react-native-rate": "^1.2.12", "react-native-restart": "^0.0.27", "react-native-safe-area-context": "^4.5.2", "react-native-sass-transformer": "^2.0.0", "react-native-screens": "^3.20.0", "react-native-svg": "13.4.0", "socket.io-client": "^4.6.1", "xlsx-translations-to-json": "^0.1.0", "expo-file-system": "~15.2.2",

and I tried also with different versions of tensorflowjs_converter to convert the weights from .h5 to .bin but I get always the same problem. P.S.: To convert the weights I use these lines in my Colab notebook

!pip install tensorflowjs
!tensorflowjs_converter --input_format=keras model.h5 tfjs_model
jflad commented 1 year ago

Same issue. In debug mode everything works fine. In release mode Android works as expected but iOS throw an RangeError: new TypedArray (buffer, [byteOffset], [length]): buffer's size must be evenly divisible by the element size error.

"@react-navigation/elements": "^1.3.18",
"@react-navigation/native": "^6.1.6",
"@react-navigation/native-stack": "^6.9.12",
"@tensorflow/tfjs": "^4.10.0",
"@tensorflow/tfjs-react-native": "^0.8.0",
"expo": "~48.0.18",
"expo-application": "~5.1.1",
"expo-asset": "~8.9.1",
"expo-camera": "~13.2.1",
"expo-constants": "~14.2.1",
"expo-device": "~5.2.1",
"expo-font": "~11.1.1",
"expo-gl": "~12.4.0",
"expo-image": "~1.0.1",
"expo-image-manipulator": "~11.1.1",
"expo-image-picker": "~14.1.1",
"expo-screen-orientation": "~5.1.1",
"expo-splash-screen": "~0.18.2",
"expo-status-bar": "~1.4.4",
"expo-updates": "~0.16.4",
"react": "18.2.0",
"react-native": "0.71.8",
"react-native-fs": "^2.20.0",
"react-native-gesture-handler": "~2.9.0",
"react-native-safe-area-context": "4.5.0",
"react-native-screens": "~3.20.0",
"react-native-svg": "13.4.0",
jflad commented 1 year ago

@Borgo99 bumped expo SDK to 49 (expo upgrade 49) and it works

Borgo99 commented 1 year ago

@jflad Can you share your dependencies after the update? Also, do you export the weights with the latest version of tensorflowjs_converter? Thanks!

jflad commented 1 year ago

@Borgo99

Dependencies after update:

"@react-navigation/elements": "^1.3.18",
"@react-navigation/native": "^6.1.6",
"@react-navigation/native-stack": "^6.9.12",
"@tensorflow/tfjs": "^4.10.0",
"@tensorflow/tfjs-react-native": "^0.8.0",
"expo": "^49.0.0",
"expo-application": "~5.3.0",
"expo-asset": "~8.10.1",
"expo-camera": "~13.4.2",
"expo-constants": "~14.4.2",
"expo-device": "~5.4.0",
"expo-font": "~11.4.0",
"expo-gl": "~13.0.1",
"expo-image": "~1.3.2",
"expo-image-manipulator": "~11.3.0",
"expo-image-picker": "~14.3.2",
"expo-screen-orientation": "~6.0.5",
"expo-splash-screen": "~0.20.5",
"expo-status-bar": "~1.6.0",
"expo-updates": "~0.18.12",
"react": "18.2.0",
"react-native": "0.72.4",
"react-native-fs": "^2.20.0",
"react-native-gesture-handler": "~2.12.0",
"react-native-safe-area-context": "4.6.3",
"react-native-screens": "~3.22.0",
"react-native-svg": "13.4.0",

We converted tflite to json + bin like in this post: https://discuss.tensorflow.org/t/how-to-convert-tflite-to-model-json-weights/17174