Skip to content

Commit

Permalink
adding some basic linting - prepping support for multiple models bein…
Browse files Browse the repository at this point in the history
…g loaded in by the app.
  • Loading branch information
lucasoskorep committed Jul 8, 2022
1 parent b8119e6 commit 284fa4a
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
/assets/mobilenetv2_gpu.tflite
/assets/mobilenetv2_gpu.tflite
4 changes: 2 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ linter:
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
avoid_print: true # Uncomment to disable the `avoid_print` rule
prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
<!-- Don't delete the meta-outputs below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
logger.i("Building main app");
logger.i('Building main app');
return MaterialApp(
title: 'Tensordex',
theme: ThemeData(
Expand Down
41 changes: 19 additions & 22 deletions lib/tflite/classifier.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:image/image.dart' as image_lib;
import 'package:tflite_flutter/tflite_flutter.dart';
import 'package:tflite_flutter_helper/tflite_flutter_helper.dart';

import 'model/outputs/recognition.dart';
import '../utils/logger.dart';
import 'data/recognition.dart';
import 'data/stats.dart';
import 'model/outputs/stats.dart';

/// Classifier
class Classifier {
static const String modelFileName = "efficientnet_v2s.tflite";
static const String modelFileName = 'efficientnet_v2s.tflite';
static const int inputSize = 224;

/// [ImageProcessor] used to pre-process the image
ImageProcessor? imageProcessor;

///Tensor image to move image data into
///Tensor image to move image outputs into
late TensorImage _inputImage;

/// Instance of Interpreter
Expand All @@ -30,55 +32,50 @@ class Classifier {
late List<String> _labels;
int classifierCreationStart = -1;

Classifier({
Interpreter? interpreter,
Classifier(
Interpreter interpreter, {
List<String>? labels,
}) {
loadModel(interpreter: interpreter);
loadModel(interpreter);
loadLabels(labels: labels);
}

/// Loads interpreter from asset
void loadModel({Interpreter? interpreter}) async {
void loadModel(Interpreter interpreter) async {
try {
_interpreter = interpreter ??
await Interpreter.fromAsset(
modelFileName,
options: InterpreterOptions()..threads = 8,
);
_interpreter = interpreter;
var outputTensor = _interpreter.getOutputTensor(0);
var outputShape = outputTensor.shape;
_outputType = outputTensor.type;
var inputTensor = _interpreter.getInputTensor(0);
// var intputShape = inputTensor.shape;
_inputType = inputTensor.type;
_inputImage = TensorImage(_inputType);
_outputBuffer = TensorBuffer.createFixedSize(outputShape, _outputType);
_outputProcessor =
TensorProcessorBuilder().add(NormalizeOp(0, 1)).build();
} catch (e) {
logger.e("Error while creating interpreter: ", e);
logger.e('Error while creating interpreter: ', e);
}
}

/// Loads labels from assets
void loadLabels({List<String>? labels}) async {
try {
_labels = labels ?? await FileUtil.loadLabels("assets/labels.txt");
_labels = labels ?? await FileUtil.loadLabels('assets/labels.txt');
} catch (e) {
logger.e("Error while loading labels: $e");
logger.e('Error while loading labels: $e');
}
}

/// Pre-process the image
TensorImage? getProcessedImage(TensorImage? inputImage) {
// padSize = max(inputImage.height, inputImage.width);
int cropSize = min(_inputImage.height, _inputImage.width);
if (inputImage != null) {
imageProcessor ??= ImageProcessorBuilder()
.add(ResizeWithCropOrPadOp(224, 224))
.add(ResizeWithCropOrPadOp(cropSize, cropSize))
.add(ResizeOp(inputSize, inputSize, ResizeMethod.BILINEAR))
.add(NormalizeOp(0, 1))
// .add(NormalizeOp(127.5, 127.5))
// .add(NormalizeOp(127.5, 127.5)) // photo vs quant normalization
.build();
return imageProcessor?.process(inputImage);
}
Expand All @@ -102,8 +99,8 @@ class Classifier {
.toList();
var endTime = DateTime.now().millisecondsSinceEpoch;
return {
"recognitions": predictions,
"stats": Stats(
'recognitions': predictions,
'stats': Stats(
totalTime: endTime - preProcStart,
preProcessingTime: inferenceStart - preProcStart,
inferenceTime: postProcStart - inferenceStart,
Expand Down
9 changes: 4 additions & 5 deletions lib/tflite/ml_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IsolateBase {
}

class MLIsolate extends IsolateBase {
static const String debugIsolate = "MLIsolate";
static const String debugIsolate = 'MLIsolate';
late SendPort _sendPort;

SendPort get sendPort => _sendPort;
Expand All @@ -34,19 +34,18 @@ class MLIsolate extends IsolateBase {
var converted = ImageUtils.convertCameraImage(cameraImage);
if (converted != null) {
Classifier classifier = Classifier(
interpreter:
Interpreter.fromAddress(mlIsolateData.interpreterAddress),
Interpreter.fromAddress(mlIsolateData.interpreterAddress),
labels: mlIsolateData.labels);
var result = classifier.predict(converted);
mlIsolateData.responsePort?.send(result);
} else {
mlIsolateData.responsePort?.send({"response": "not working yet"});
mlIsolateData.responsePort?.send({'response': 'not working yet'});
}
}
}
}

/// Bundles data to pass between Isolate
/// Bundles outputs to pass between Isolate
class MLIsolateData {
CameraImage cameraImage;
int interpreterAddress;
Expand Down
16 changes: 16 additions & 0 deletions lib/tflite/model/configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:tflite_flutter/tflite_flutter.dart';
import 'constants.dart';

class ModelConfiguration{
String name;
late List<InterpreterOptions> interpreters;

ModelConfiguration(this.name){
interpreters = name.contains('gpu') ? ModelConstants.gpuInterpreterList : ModelConstants.cpuInterpreterList;
}

@override
String toString() {
return 'ModelConfiguration(name: $name, interpreters: $interpreters)';
}
}
10 changes: 10 additions & 0 deletions lib/tflite/model/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:tflite_flutter/tflite_flutter.dart';


class ModelConstants {
static final InterpreterOptions _npuConfig = InterpreterOptions()..threads = 8..useNnApiForAndroid = true..useMetalDelegateForIOS = true;
static final InterpreterOptions _cpuConfig = InterpreterOptions()..threads = 8;
static final List<InterpreterOptions> gpuInterpreterList = [_npuConfig, _cpuConfig];
static final List<InterpreterOptions> cpuInterpreterList = [_cpuConfig];
}

File renamed without changes.
File renamed without changes.
55 changes: 38 additions & 17 deletions lib/widgets/poke_finder.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'dart:convert';
import 'dart:isolate';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tensordex_mobile/tflite/ml_isolate.dart';
import 'package:tensordex_mobile/tflite/model/configuration.dart';
import 'package:tensordex_mobile/tflite/model/outputs/stats.dart';
import 'package:tflite_flutter/tflite_flutter.dart';

import '../tflite/classifier.dart';
import '../tflite/model/outputs/recognition.dart';
import '../utils/logger.dart';
import '../tflite/data/recognition.dart';
import '../tflite/data/stats.dart';

/// [PokeFinder] sends each frame for inference

class PokeFinder extends StatefulWidget {
/// Callback to pass results after inference to [HomeView]
final Function(List<Recognition> recognitions) resultsCallback;
Expand All @@ -28,17 +31,20 @@ class PokeFinder extends StatefulWidget {
}

class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
late List<CameraDescription> cameras;
late CameraController cameraController;
late MLIsolate _mlIsolate;

/// true when inference is ongoing
bool predicting = false;
bool _cameraInitialized = false;
bool _classifierInitialized = false;

//cameras
late List<CameraDescription> cameras;
late CameraController cameraController;

//ml variables
late Interpreter interpreter;
late Classifier classifier;
late MLIsolate _mlIsolate;
late List<ModelConfiguration> modelConfigurations;

@override
void initState() {
Expand All @@ -55,19 +61,34 @@ class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
predicting = false;
}

Future<List<String>> getModelFiles() async {
final manifestContent = await rootBundle.loadString('AssetManifest.jsn');
final Map<String, dynamic> manifestMap = json.decode(manifestContent);
return manifestMap.keys
.where((String key) => key.contains('.tflite'))
.map((String key) => key.substring(7))
.toList();
}

void initializeModel() async {
var interpreterOptions = InterpreterOptions()..threads = 8;
interpreter = await Interpreter.fromAsset('efficientnet_v2s.tflite',
options: interpreterOptions);
classifier = Classifier(interpreter: interpreter);
var modelFiles = await getModelFiles();
var modelConfigurations =
modelFiles.map((e) => ModelConfiguration(e)).toList();
var currentConfig = modelConfigurations[0];
logger.i(modelFiles);
interpreter = await createInterpreter(currentConfig);
classifier = Classifier(interpreter);
_classifierInitialized = true;
}

Future<Interpreter> createInterpreter(ModelConfiguration config) async {
return await Interpreter.fromAsset(config.name,
options: config.interpreters[0]);
}

/// Initializes the camera by setting [cameraController]
void initializeCamera() async {
cameras = await availableCameras();

// cameras[0] for rear-camera
cameraController =
CameraController(cameras[0], ResolutionPreset.low, enableAudio: false);

Expand All @@ -94,11 +115,11 @@ class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
var results = await inference(MLIsolateData(
cameraImage, classifier.interpreter.address, classifier.labels));

if (results.containsKey("recognitions")) {
widget.resultsCallback(results["recognitions"]);
if (results.containsKey('recognitions')) {
widget.resultsCallback(results['recognitions']);
}
if (results.containsKey("stats")) {
widget.statsCallback(results["stats"]);
if (results.containsKey('stats')) {
widget.statsCallback(results['stats']);
}
logger.i(results);

Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/results.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:tensordex_mobile/widgets/poke_finder.dart';
import 'package:tensordex_mobile/tflite/data/recognition.dart';
import 'package:tensordex_mobile/tflite/data/stats.dart';
import '../tflite/model/outputs/recognition.dart';
import '../tflite/model/outputs/stats.dart';


/// [PokeFinder] sends each frame for inference
Expand Down
8 changes: 4 additions & 4 deletions lib/widgets/tensordex_home.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:tensordex_mobile/tflite/model/outputs/recognition.dart';
import 'package:tensordex_mobile/tflite/model/outputs/stats.dart';
import 'package:tensordex_mobile/widgets/poke_finder.dart';
import 'package:tensordex_mobile/widgets/results.dart';

import '../utils/logger.dart';
import '../tflite/data/recognition.dart';
import '../tflite/data/stats.dart';

class TensordexHome extends StatefulWidget {
const TensordexHome({Key? key, required this.title}) : super(key: key);
Expand All @@ -22,15 +22,15 @@ class TensordexHome extends StatefulWidget {

class _TensordexHomeState extends State<TensordexHome> {
/// Results from the image classifier
List<Recognition> results = [Recognition(1, "NOTHING DETECTED", .5)];
List<Recognition> results = [Recognition(1, 'NOTHING DETECTED', .5)];
Stats stats = Stats();

/// Scaffold Key
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

void _incrementCounter() {
setState(() {
logger.d("Counter Incremented!");
logger.d('Counter Incremented!');
});
}

Expand Down

0 comments on commit 284fa4a

Please sign in to comment.