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

Issue with Preprocessing Input for TFLite Model [1, 640, 640, 3] #245

Open
yAlqubati opened this issue Dec 14, 2024 · 0 comments
Open

Issue with Preprocessing Input for TFLite Model [1, 640, 640, 3] #245

yAlqubati opened this issue Dec 14, 2024 · 0 comments

Comments

@yAlqubati
Copy link

Description

I’m trying to run an object detection TFLite model with the following specifications:

  • Input shape: [1, 640, 640, 3]
  • Output shape: [1, 25200, 7]

The main issue is preprocessing the input image to match the required format (float32). Below are the approaches I’ve tried so far, but none of them worked.


First Attempt

I used a straightforward method to read the image and convert it into float32:

import 'dart:io';
import 'package:tflite_flutter/tflite_flutter.dart';
import 'dart:developer' as developer;

class ScanController {
  void loadAndPredict() async {
    final interpreter = await Interpreter.fromAsset('assets/model.tflite');
    var image = File('assets/test.jpg');
    var imageBytes = await image.readAsBytes();

    // Convert image to float32
    final input = imageBytes.buffer.asFloat32List();

    // Expected output size: [1, 25200, 7]
    final output = List.filled(1 * 25200 * 7, 0.0).reshape([1, 25200, 7]);

    interpreter.run(input, output);
    developer.log(output.toString());
  }
}

Result: This approach failed because the image wasn’t resized, normalized, or correctly formatted for the model input.


Second Attempt

I used tflite_flutter_helper to preprocess the image, assuming it would handle resizing and normalization:

import 'dart:io';
import 'package:tflite_flutter/tflite_flutter.dart' as tflite;
import 'package:tflite_flutter_helper/tflite_flutter_helper.dart';
import 'dart:developer' as developer;

class ScanController {
  void loadAndPredict() async {
    try {
      final interpreter = await tflite.Interpreter.fromAsset('assets/model.tflite');
      developer.log('Model loaded successfully.');

      // Log input/output details
      var inputShape = interpreter.getInputTensor(0).shape;
      var inputType = interpreter.getInputTensor(0).type;
      var outputShape = interpreter.getOutputTensor(0).shape;
      developer.log('Input Shape: $inputShape, Type: $inputType');
      developer.log('Output Shape: $outputShape');

      var file = File('assets/test.jpg');

      // Preprocessing using TensorImage and ImageProcessor
      ImageProcessor imageProcessor = ImageProcessorBuilder()
          .add(ResizeOp(640, 640, ResizeMethod.BILINEAR))
          .add(NormalizeOp(0, 255))
          .build();

      TensorImage tensorImage = TensorImage.fromFile(file);
      tensorImage = imageProcessor.process(tensorImage);

      // Create output buffer
      TensorBuffer outputBuffer = TensorBuffer.createFixedSize(outputShape, tflite.TfLiteType.float32);

      // Run inference
      interpreter.run(tensorImage.buffer, outputBuffer.buffer);

      // Log the result
      var output = outputBuffer.getDoubleList();
      developer.log('Output: $output');
    } catch (e) {
      developer.log('Failed to load model: $e');
    }
  }
}

Result: This approach also failed. The main issue was with the tflite_flutter_helper package, which relies on outdated dependencies and is no longer actively maintained.


Request

Can someone please:

  1. Explain the steps I need to follow to preprocess the input image to match the required format [1, 640, 640, 3] and type float32.
  2. Provide guidance on resizing, normalizing, and converting the image effectively.
  3. Suggest packages or dependencies to use, considering that tflite_flutter_helper is outdated.

Any help or example code would be greatly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant