From c3a2f0580ec7cd6ef39aa0966443b048b76f5360 Mon Sep 17 00:00:00 2001 From: "CaptainDario @ MBP M1" Date: Wed, 10 Jan 2024 10:48:35 +0100 Subject: [PATCH 1/3] rollback tf lite to version 2.11 to allow using minSdkVersion 19 --- android/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 8cf93da..61cfc68 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -55,13 +55,13 @@ android { } defaultConfig { - minSdkVersion 26 + minSdkVersion 19 } } dependencies { - def tflite_version = "2.12.0" + def tflite_version = "2.11.0" implementation("org.tensorflow:tensorflow-lite:${tflite_version}") implementation("org.tensorflow:tensorflow-lite-gpu:${tflite_version}") From 06d2fdc3e9329cc3aa356f1a4e492867f3b76e87 Mon Sep 17 00:00:00 2001 From: "CaptainDario @ MBP M1" Date: Sun, 11 Feb 2024 14:22:07 +0100 Subject: [PATCH 2/3] allow decoding tf strings --- lib/src/util/byte_conversion_utils.dart | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/src/util/byte_conversion_utils.dart b/lib/src/util/byte_conversion_utils.dart index a52054e..db25d57 100644 --- a/lib/src/util/byte_conversion_utils.dart +++ b/lib/src/util/byte_conversion_utils.dart @@ -14,6 +14,8 @@ * limitations under the License. */ +import 'dart:convert'; +import 'dart:ffi'; import 'dart:typed_data'; import 'package:tflite_flutter/tflite_flutter.dart'; @@ -165,6 +167,37 @@ class ByteConversionUtils { ); } + /// Decodes a TensorFlow string to a List + static List decodeTFStrings(Uint8List bytes){ + /// The decoded string + List decodedStrings = []; + /// get the first 32bit int representing num of strings + int numStrings = ByteData.view(bytes.sublist(0,sizeOf()).buffer).getInt32(0, Endian.little); + + /// parse subsequent string position and sizes + for(int s = 0; s < numStrings; s++){ + + // get current str index + int startIdx = ByteData.view( + bytes.sublist( + (1+s)*sizeOf(), + (2+s)*sizeOf() + ) + .buffer).getInt32(0, Endian.little); + // get next str index, or in last case the ending byte position + int endIdx = ByteData.view( + bytes.sublist( + (2+s)*sizeOf(), + (3+s)*sizeOf() + ) + .buffer).getInt32(0, Endian.little); + + decodedStrings.add(utf8.decode(bytes.sublist(startIdx,endIdx))); + } + + return decodedStrings; + } + static Object convertBytesToObject( Uint8List bytes, TensorType tensorType, List shape) { // stores flattened data @@ -209,6 +242,9 @@ class ByteConversionUtils { list.add(ByteData.view(bytes.buffer).getInt64(i)); } return list.reshape(shape); + } else if (tensorType.value == TfLiteType.kTfLiteString) { + list.add(decodeTFStrings(bytes)); + return list; } throw UnsupportedError("$tensorType is not Supported."); } From 7c292604eb3e6bf0a3354253f220a8a15cf97d6a Mon Sep 17 00:00:00 2001 From: "CaptainDario @ MBP M1" Date: Sun, 11 Feb 2024 14:30:19 +0100 Subject: [PATCH 3/3] dart format --- lib/src/util/byte_conversion_utils.dart | 33 +++++++++++-------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/src/util/byte_conversion_utils.dart b/lib/src/util/byte_conversion_utils.dart index db25d57..2538c67 100644 --- a/lib/src/util/byte_conversion_utils.dart +++ b/lib/src/util/byte_conversion_utils.dart @@ -168,31 +168,28 @@ class ByteConversionUtils { } /// Decodes a TensorFlow string to a List - static List decodeTFStrings(Uint8List bytes){ + static List decodeTFStrings(Uint8List bytes) { /// The decoded string List decodedStrings = []; + /// get the first 32bit int representing num of strings - int numStrings = ByteData.view(bytes.sublist(0,sizeOf()).buffer).getInt32(0, Endian.little); + int numStrings = ByteData.view(bytes.sublist(0, sizeOf()).buffer) + .getInt32(0, Endian.little); /// parse subsequent string position and sizes - for(int s = 0; s < numStrings; s++){ - + for (int s = 0; s < numStrings; s++) { // get current str index - int startIdx = ByteData.view( - bytes.sublist( - (1+s)*sizeOf(), - (2+s)*sizeOf() - ) - .buffer).getInt32(0, Endian.little); + int startIdx = ByteData.view(bytes + .sublist((1 + s) * sizeOf(), (2 + s) * sizeOf()) + .buffer) + .getInt32(0, Endian.little); // get next str index, or in last case the ending byte position - int endIdx = ByteData.view( - bytes.sublist( - (2+s)*sizeOf(), - (3+s)*sizeOf() - ) - .buffer).getInt32(0, Endian.little); - - decodedStrings.add(utf8.decode(bytes.sublist(startIdx,endIdx))); + int endIdx = ByteData.view(bytes + .sublist((2 + s) * sizeOf(), (3 + s) * sizeOf()) + .buffer) + .getInt32(0, Endian.little); + + decodedStrings.add(utf8.decode(bytes.sublist(startIdx, endIdx))); } return decodedStrings;