shaqian / tflite-react-native Goto Github PK
View Code? Open in Web Editor NEWReact Native library for TensorFlow Lite
Home Page: https://www.npmjs.com/package/tflite-react-native
License: MIT License
React Native library for TensorFlow Lite
Home Page: https://www.npmjs.com/package/tflite-react-native
License: MIT License
I have implemented this library with the image classification model in my project, but when I call tflite.runModelOnImage, I have the bug with the following content:
cannot convert between a TensorFlow Lite tensor with dtype UNIT8 and a Java object of type [[F
Did anybody catch this bug? Could you give me a help?
Thank you!
target 'TFDemo' do
pod 'TensorFlowLite', '1.12.0'
config = use_native_modules!
/**
Hey Folks ,
I'm facing this issue . Is this a bug or i'm missing any things
I am having some issues using quantized models, for example ssd_mobilenet_v2_quantized_coco.
Does this library support that?
I'm using custom SSD model (I tried my own and three other models that were trained by users)
They work fine in Android, but in IOS it fails here:
if (interpreter->AllocateTensors() != kTfLiteOk) {
callback(@[@"Failed to allocate tensors!"]);
}
With EXC_BAD_ACCESS
When I use ssd_mobilenet.tflite from model Zoo IOS does not throw error
I'm trying to build the example app in this repo. When I run react-native run-android
without any options, the app successfully runs on the Android emulator, but it fails with --deviceId
option to run it on a real Android device. Is there any solution?
Here is the log.
$ git clone https://github.com/shaqian/tflite-react-native.git
$ cd tflite-react-native/example
$ npm install
$ react-native run-android --deviceId XXXXXXXXXXXXX
Starting JS server...
Building the app...
> Configure project :app
WARNING: The specified Android SDK Build Tools version (28.0.2) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.2.1.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '28.0.2'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
> Configure project :react-native-image-picker
WARNING: The specified Android SDK Build Tools version (28.0.2) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.2.1.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '28.0.2'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
> Configure project :tflite-react-native
WARNING: The specified Android SDK Build Tools version (23.0.1) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.2.1.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '23.0.1'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
> Task :tflite-react-native:compileDebugJavaWithJavac
Note: /Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/src/main/java/com/reactlibrary/TfliteReactNativeModule.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.
Loading dependency graph, done.
bundle: Writing bundle output to: /Users/john/development/tflite-react-native/example/android/app/build/generated/assets/react/release/index.android.bundle
bundle: Done writing bundle output
> Task :tflite-react-native:compileReleaseJavaWithJavac
Note: /Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/src/main/java/com/reactlibrary/TfliteReactNativeModule.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
> Task :tflite-react-native:verifyReleaseResources FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':tflite-react-native:verifyReleaseResources'.
> java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
Output: error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found.
error: resource android:style/TextAppearance.Material.Widget.Button.Colored not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values-v26/values-v26.xml:7: error: resource android:attr/colorError not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values-v26/values-v26.xml:11: error: resource android:attr/colorError not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values-v26/values-v26.xml:15: error: style attribute 'android:attr/keyboardNavigationCluster' not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values-v28/values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values-v28/values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:954: error: resource android:attr/fontStyle not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:955: error: resource android:attr/font not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:956: error: resource android:attr/fontWeight not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:957: error: resource android:attr/fontVariationSettings not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:958: error: resource android:attr/ttcIndex not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:1039: error: resource android:attr/startX not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:1042: error: resource android:attr/startY not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:1045: error: resource android:attr/endX not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:1048: error: resource android:attr/endY not found.
/Users/john/development/tflite-react-native/example/node_modules/tflite-react-native/android/build/intermediates/res/merged/release/values/values.xml:1056: error: resource android:attr/offset not found.
error: failed linking references.
Command: /Users/john/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.1-4818971-osx.jar/a4f31520f4d94325cc40dcb108789e7e/aapt2-3.2.1-4818971-osx/aapt2 link -I\
/Users/john/Library/Android/sdk/platforms/android-23/android.jar\
--manifest\ ...
...
$ react-native info
React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Memory: 47.34 MB / 8.00 GB
Shell: 5.5.1 - /usr/local/bin/zsh
Binaries:
Node: 10.13.0 - ~/.nodebrew/current/bin/node
Yarn: 1.17.3 - ~/.nodebrew/current/bin/yarn
npm: 6.4.1 - ~/.nodebrew/current/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
Android SDK:
API Levels: 23, 24, 25, 26, 28, 29
Build Tools: 28.0.3, 29.0.0, 29.0.2
System Images: android-26 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5900203
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
react: 16.6.3 => 16.6.3
react-native: 0.58.4 => 0.58.4
npmGlobalPackages:
react-native-cli: 2.0.1
Thanks.
First of all thank you for your code!
I've converted a classification model to tflite and I'm trying to run it using your lib but the results do not match. I've already verified the conversion (running the .tflite in Python) and it's ok. Thus I believe it could be a pre-processing fault.
I don't know much about Java but from what I've understood from your code it does the following:
Is my assessment correct? The TopK is applied on the raw logits or softmaxed?
My model only needs a RGB 299x299x3 image, where mean and std are 127.5 but even so I cannot reproduce the results.
Thank you for your time
First of all, thanks for creating this awesome package. Great work ๐
Does this package supports YOLO V3 ?
I tried running the tiny YOLO V2 model from demo project. It works well.
When we tried to run YOLO3 keras model converted to tflite, we are not getting result/ detection.
P.S the same converted model is yielding result when we tried to run on standalone server.
Thanks for this project, it seems quite good.
I'm getting this issue when I try to run it on Android. Could you please help me?
Thanks!
Getting length=2; index=3 error while executing runModelOnImage function
here is my package.json file:
"dependencies": {
"@react-native-async-storage/async-storage": "^1.15.9",
"@react-native-firebase/app": "^12.9.2",
"@react-native-firebase/ml": "^12.9.2",
"@tensorflow/tfjs-react-native": "^0.7.0",
"react": "17.0.2",
"react-native": "0.66.1",
"react-native-camera": "^4.2.1",
"react-native-firebase-mlkit": "^0.6.2",
"react-native-fs": "^2.18.0",
"react-native-image-picker": "^4.1.2",
"react-native-permissions": "^3.1.0",
"react-native-vision-object-detection": "0.0.8",
"tflite-react-native": "0.0.5"
}
Help nedded.
how to load multiple models for tflite
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react-native
npm ERR! react-native@"0.71.8" from the root project
npm ERR! peer react-native@"*" from @react-native-community/[email protected]
npm ERR! node_modules/@react-native-community/geolocation
npm ERR! @react-native-community/geolocation@"^3.0.6" from the root project
npm ERR! 13 more (@react-native-community/masked-view, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react-native@"^0.41.2" from [email protected]
npm ERR! node_modules/tflite-react-native
npm ERR! tflite-react-native@"^0.0.5" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/react-native
npm ERR! peer react-native@"^0.41.2" from [email protected]
npm ERR! node_modules/tflite-react-native
npm ERR! tflite-react-native@"^0.0.5" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR!
npm ERR! For a full report see:
npm ERR! C:\Users\PC\AppData\Local\
Hello!
I followed all the instructions to load a model and I don't get an error. Here is my code for loading a model:
let tflite = new Tflite();
tflite.loadModel({
model: 'models/model.tflite',// required
labels: 'models/labels.txt', // required
numThreads: 1,
async: false // defaults to 1
},
(err, res) => {
if(err) {
console.log(err);
}
else {
console.log(res);
console.log(tflite);
}
});
As you can see in the success else
block, I console log the tflite
object. But it's empty. I figured the loadmodel method would populate it with something?
But in the log, all I get is Tflite {}
Is this correct? Because I am unable to then make calls on image classification with this tflite object I have.
Thanks ahead of time
Ho to fix Are you using old TFLite binary with newer model?
on IOS
Model is converted with v1 tf converter. Thank you
Switching to RN60.0 the library is not working. I added to Libraries the TfliteReactNative.xcodeproj file and added libTfliteReactNative.a to Build phases. After the build fails, with this issue.
The changes in the switch from RN59.10 to RN60.0 version can be seen here:
https://react-native-community.github.io/upgrade-helper/?from=0.59.10&to=0.60.0
For anyone helping solve the issue:
On Android things work just as they should.
My guess on the issue:
When we switch to RN60.0 podfile, we lost some of it's includes, and tflite-react-native library on some of these React-Core pods files. But still I don't know what is missing and how to correct it.
I am developing an app for ios with react-native CLI, and I am facing a problem loading the model.
I've tried every solution possible on the internet, but it still doesn't work, unfortunately.
Linking is working properly, I've tried manual linking and automatic.
I have my model in src/models.
And in Xcode, it's under MyProject > models.
This is my code:
const initModel = () => {
const tflite = new Tflite();
console.log('started initialization of TENSOR FLOW LITE');
tflite.loadModel(
{
model: 'models/model.tflite', // required
labels: 'models/labels.txt', // required
numThreads: 1,
async: false, // defaults to 1
},
(err, res) => {
if (err) {
console.log(err);
} else {
res.runModelOnImage(
{
path: pictureUrl, // required
imageMean: 128.0, // defaults to 127.5
imageStd: 128.0, // defaults to 127.5
numResults: 3, // defaults to 5
threshold: 0.05, // defaults to 0.1
},
(err, res) => {
if (err) console.log(err);
else setPrediction(res);
},
);
}
},
);
};
useEffect(() => {
initModel();
}, []);
Podfile:
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
platform :ios, '11.0'
pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons'
target 'ai_app' do
config = use_native_modules!
use_react_native!(
:path => config[:reactNativePath],
# to enable hermes on iOS, change `false` to `true` and then install pods
:hermes_enabled => false
)
pod 'TensorFlowLite', '1.12.0'
pod 'TfliteReactNative', :path => '../node_modules/tflite-react-native/ios'
# target 'ai_appTests' do
# inherit! :complete
# # Pods for testing
# end
# Enables Flipper.
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
# you should disable the next line.
use_flipper!()
post_install do |installer|
react_native_post_install(installer)
__apply_Xcode_12_5_M1_post_install_workaround(installer)
end
end
Metro.config:
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/
module.exports = {
resolver: {
assetExts: ['tflite', 'txt','png'],
},
};
Building is successful on Xcode.
Thanks in advance.
First of all, thanks for this awesome project. Had this working on Android but not quite on iOS. The home screen with the buttons shows up. However, clicking on a button results in error: null is not an object evaluating TfliteReactNative.loadmodel
The only change I made was in the Pod file to get the build to succeed.
Pod file:
platform :ios, '9.0'
target 'example' do
pod 'TensorFlowLite', '1.12.0'
pod 'react-native-image-picker', :path => '../node_modules/react-native-image-picker'
pod 'React', :path => '../node_modules/react-native'
pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
end
My npm version is 6.5.0
Need help with resolving this. Thanks.
Hi, I am trying to add this library to my project. For android given instructions are working. But for iOS i tried many ways to add library but i am getting null is not an object evaluating (TfliteReactNative.loadModel)
.
I tried below ways induvidually and failed to load model.
Copy items if needed
and Enabled Create groups
-> Selected models folder(which contains model.tflite and label.txt files).Copy items if needed
and enabled Create groups
-> Selected models folder(which contains model.tflite and label.txt files).Where am i going wrong? Please anyone help me out.
My configurations are:
macOS - 10.15.7
react-native - 0.64.2
tflite-react-native - 0.0.5
pods:
TensorFlowLite (1.13.1)
TfliteReactNative (0.0.4)
Thanks in advance.
I can not find how to create tflite model in react native anywhere. Who will answer this?
Hi
I've successfully tried your package using my own model for image classification case.
However as you pointed out in the README, the result is a single prediction :
{
"index": 0,
"label": "person",
"confidence": 0.629
}
I would like to know where I should modify your code to get all predicted probabilities. e.g. something like this :
[
{
"index": 0,
"label": "person",
"confidence": 0.6
},
{
"index": 1,
"label": "cat",
"confidence": 0.3
}
{
"index": 2,
"label": "dog",
"confidence": 0.1
}
]
Thanks in advance
Hi, I am facing below mentioned error while trying to run "tflite-react-native\example" using "react-native run-android" with my Android phone connected using USB cable and with USB debugging "on"
Below is my package.json file in tflite-react-native\example folder:
{
"name": "example",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.6.3",
"react-native": "^0.63.3",
"react-native-image-picker": "0.28.0",
"tflite-react-native": "0.0.5"
},
"devDependencies": {
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.1.0",
"jest": "24.1.0",
"metro-react-native-babel-preset": "0.51.1",
"react-test-renderer": "16.6.3"
},
"jest": {
"preset": "react-native"
}
}
Kindly suggest.
I am running example app on ios/android devices, using same model, same input image in both side, but the results is not same.
Generally the sequence of topN for Mobilenet is same, but the confidence percentage is a bit off, like testing a train image on android it says 69% but in ios it says 73%
I am also testing on some custom model on image classification as well, we result is more off in that case. In android it says 51%/49% and on ios it says 90%/10%
Is it the code of transforming the image to bitmap is a bit different on ios/android so the result is different?
I would love to see support for audio input such that this example could be added:
https://github.com/tensorflow/examples/blob/master/lite/examples/speech_commands/android/README.md
#include "tensorflow/lite/kernels/register.h"
Thanks.
The following warning comes when I include tflite in react native project for android.
WARNING: The specified Android SDK Build Tools version (23.0.1) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.4.2.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '23.0.1'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
Hi,
I have trained and created a tflite on google colab and testing on colab is okay. But, In the application it is not detecting anything.
can I use my own tflite model with tflite-react-native?
in my application, users should select their custom model, so I can't move this selected model into assets.
But library allows only model in assets folder.
My issue is also here
https://stackoverflow.com/questions/65596203/react-native-tensorflow-lite-library-only-use-model-from-assets-but-i-want-to-u
what can I do ? pls help
Given that last commits were made about 3 years ago, should we assume that the project is no longer maintained?
P.S. I'm interested in running Whisper in my (expo) react native apps - openai/whisper#11 (comment)
this can detect images ,but if it can detect videos in real time?And can you give me some advice?
Hi,
Thank you for this library!
I am using my own trained object-detection model (using "SSD Inception v1 coco" as a starting point).
I have only 1 class, and the goal is to count the number of occurences of that item in a picture.
My issue is the following: running the tflite model returns only a maximum of 10 occurences.
Even if I set 'numResultsPerClass' to a number higher than 10, I still get only a maximum of 10 occurences:
My code:
tflite.detectObjectOnImage( { path: imagePath, model: 'SSDMobileNet', imageMean: 127.5, imageStd: 127.5, threshold: 0.5, numResultsPerClass: 60 }, (err: Error, res: any) => { if (err) { reject(err); } else { resolve(res); } } );
Could you please help me find what is causing this?
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.