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

[WiP] [Android] Switch to upstream TFlite binaries instead of precompiled ones #42

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ TensorFlow Lite Flutter plugin provides a flexible and fast solution for accessi

### Android

Copy the folders from https://github.com/tensorflow/flutter-tflite/tree/main/releases/download/android into your project's /android/app/src/main/jniLibs directory.
Define the dependency within your project's `android/app/build.gradle` as follows:

```gradle
dependencies {
implementation 'org.tensorflow:tensorflow-lite:2.12.0'
}
```

If are using GPU delegates, in addition to the above, also add a dependency on

```gradle
implementation 'org.tensorflow:tensorflow-lite-gpu:2.12.0'
```

### iOS

Expand Down
4 changes: 3 additions & 1 deletion example/text_classification/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "example.android.app.src.main.java.com.tfliteflutter.tflite_flutter_plugin_example"
minSdkVersion 16
minSdkVersion 19
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand All @@ -74,4 +74,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'org.tensorflow:tensorflow-lite:2.12.0'
implementation 'org.tensorflow:tensorflow-lite-gpu:2.12.0'
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion example/text_classification/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
5 changes: 4 additions & 1 deletion example/text_classification/lib/classifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class Classifier {

void _loadModel() async {
// Creating the interpreter using Interpreter.fromAsset
_interpreter = await Interpreter.fromAsset(_modelFile);
final gpuDelegateV2 = GpuDelegateV2();
final options = InterpreterOptions()..addDelegate(gpuDelegateV2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo on the double dots?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cascade notation, compiles as expected

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I'll undo the changes in this file once the GPU delegates are working. Keeping this around so it's easy to reproduce the issue

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL - Thanks! Still learning Dart (I've been doing Android for 12 years) since I think Flutter has a lot of advantages, but also a little behind where I'd like to be :)

_interpreter =
await Interpreter.fromAsset(_modelFile, options: options);
print('Interpreter loaded successfully');
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/bindings/delegates/gpu_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import '../types.dart';
///
/// When `options` is set to `nullptr`, then default options are used.
Pointer<TfLiteDelegate> Function(Pointer<TfLiteGpuDelegateOptionsV2> options)
tfLiteGpuDelegateV2Create = tflitelib
tfLiteGpuDelegateV2Create = tflitelibgpu
.lookup<NativeFunction<_TfLiteGpuDelegateV2CreateNativeT>>(
'TfLiteGpuDelegateV2Create')
.asFunction();
Expand All @@ -41,7 +41,7 @@ typedef _TfLiteGpuDelegateV2CreateNativeT = Pointer<TfLiteDelegate> Function(
Pointer<TfLiteGpuDelegateOptionsV2> options);

/// Destroys a delegate created with `TfLiteGpuDelegateV2Create` call.
void Function(Pointer<TfLiteDelegate>) tfLiteGpuDelegateV2Delete = tflitelib
void Function(Pointer<TfLiteDelegate>) tfLiteGpuDelegateV2Delete = tflitelibgpu
.lookup<NativeFunction<_TFLGpuDelegateV2DeleteNativeT>>(
'TfLiteGpuDelegateV2Delete')
.asFunction();
Expand All @@ -51,7 +51,7 @@ typedef _TFLGpuDelegateV2DeleteNativeT = Void Function(

/// Creates TfLiteGpuDelegateV2 with default options
TfLiteGpuDelegateOptionsV2 Function() tfLiteGpuDelegateOptionsV2Default =
tflitelib
tflitelibgpu
.lookup<
NativeFunction<
_TfLiteTfLiteGpuDelegateOptionsV2DefaultNativeT>>(
Expand Down
14 changes: 13 additions & 1 deletion lib/src/bindings/dlib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ String get binaryName {
// ignore: missing_return
DynamicLibrary tflitelib = () {
if (Platform.isAndroid) {
return DynamicLibrary.open('libtensorflowlite_c.so');
return DynamicLibrary.open('libtensorflowlite_jni.so');
} else if (Platform.isIOS) {
return DynamicLibrary.process();
} else {
return DynamicLibrary.open(
'${Directory(Platform.resolvedExecutable).parent.path}/blobs/$binaryName');
}
}();

DynamicLibrary tflitelibgpu = () {
if (Platform.isAndroid) {
return DynamicLibrary.open('libtensorflowlite_gpu_jni.so');
} else if (Platform.isIOS) {
return DynamicLibrary.process();
} else {
return DynamicLibrary.open(
Directory(Platform.resolvedExecutable).parent.path +
'/blobs/${binaryName}');
}
}();
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed releases/download/android/x86/libtensorflowlite_c.so
Binary file not shown.
Binary file not shown.