0

I'm developing a object detection app by using tflite_flutter, tflite_flutter_helper and a pretrained ssdMobilenet model. I followed same steps that are given in the documentation but still the error encountered.

keep getting this error

[outputShapes] [[1, 10], [1, 10, 4], [1], [1, 10]]
[ERROR AT convert] Invalid argument(s): Axis 2 is not in range (-(D+1), D), where D is the number of dimensions of input tensor (shape=[1, 10])

here is my whole code

   File? imageFile;
  final picker = ImagePicker();

  late Interpreter interpreter;
  late ImageProcessor imageProcessor;
  TensorImage? tensorImage;
  TensorBuffer? probabilityBuffer;
  List<String> labels = [];
  List<List<int>> outputShapes = [];

  /// Types of output tensors
  List<TfLiteType> outputTypes = [];

  Future<File?> getImagec() async {
    final pickedFile = await picker.pickImage(
      source: ImageSource.camera,
      imageQuality: 25,
    );
    if (pickedFile == null) {
      return null;
    }
    log(pickedFile.path);
    imageFile = File(pickedFile.path);
    update();
    processImage();
    return File(pickedFile.path);
  }

  processImage() {
    imageProcessor = ImageProcessorBuilder()
        .add(ResizeOp(224, 224, ResizeMethod.BILINEAR))
        .add(NormalizeOp(127.5, 127.5))
        .add(QuantizeOp(128.0, 1 / 128.0))
        .build();

    tensorImage = TensorImage.fromFile(imageFile!);

    tensorImage = imageProcessor.process(tensorImage!);

    probabilityBuffer =
        TensorBuffer.createFixedSize(<int>[1, 1001], TfLiteType.uint8);
    loadModel();
  }

  loadModel() async {
    // try {
    // TensorProcessor? probabilityProcessor;
    interpreter = await Interpreter.fromAsset(
      'ssd_mobilenet_320x320.tflite',
      options: InterpreterOptions()..threads = 4,
    ); //efficientdet_lite_teeth_final //detect
    loadAssets();
    var outputTensors = interpreter.getOutputTensors();
    outputShapes = [];
    outputTypes = [];
    for (var tensor in outputTensors) {
      outputShapes.add(tensor.shape);
      outputTypes.add(tensor.type);
    }
    log("${outputShapes.shape}", name: "Shapes");
    predict();
  }

  loadAssets() async {
    labels = await FileUtil.loadLabels("assets/teeth_labels_final.txt");
  }

  predict() {
    // try {
    TensorImage inputImage = tensorImage!;

    // TensorBuffers for output tensors
    log("$outputShapes", name: "outputShapes");

    TensorBuffer outputLocations = TensorBufferFloat(outputShapes[0]);
    TensorBuffer outputClasses = TensorBufferFloat(outputShapes[1]);
    TensorBuffer outputScores = TensorBufferFloat(outputShapes[2]);
    TensorBuffer numLocations = TensorBufferFloat(outputShapes[3]);

    List<Object> inputs = [inputImage.buffer];
    log("${outputLocations.getShape()}", name: "outputLocations shape");
    // Outputs map
    Map<int, Object> outputs = {
      0: outputLocations.buffer,
      1: outputClasses.buffer,
      2: outputScores.buffer,
      3: numLocations.buffer,
    };

    // run inference
    try {
      interpreter.runForMultipleInputs(inputs, outputs);
    } catch (e) {
      log('$e', name: 'ERROR AT runForMultipleInputs');
    }

    int resultsCount = math.min(10, numLocations.getIntValue(0));
    List<Rect> locations = [];

    try {
      locations = BoundingBoxUtils.convert(
        tensor: outputLocations,
        valueIndex: [1, 0, 3, 2],
        boundingBoxAxis: 2,
        boundingBoxType: BoundingBoxType.BOUNDARIES,
        coordinateType: CoordinateType.RATIO,
        height: 224,
        width: 224,
      );
    } catch (e) {
      log('$e', name: 'ERROR AT convert');
    }

    log("${locations.length}---", name: "locations");
    log("$resultsCount---", name: "resultsCount");

    try {
      for (int i = 0; i < resultsCount; i++) {
        // Prediction score
        var score = outputScores.getDoubleValue(i);

        // Label string
        var labelIndex = outputClasses.getIntValue(i) + 1;
        var label = labels.elementAt(labelIndex);

        if (score != 0) {
          Rect transformedRect = imageProcessor.inverseTransformRect(
              locations[i], inputImage.height, inputImage.width);

          log("$i $label $score $transformedRect");
        }
      }
    } catch (e) {
      log('$e', name: 'ERROR AT resultsCount');
    }

    // } catch (e) {
    //   log('$e', name: 'ERROR AT predict');
    // }
  }

Executions starts by calling getImagec() method. Also, i tried flutter_tflite too but its not working either, similar error was found - mentioned below.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Failed to run model, Cannot copy from a TensorFlowLite tensor (StatefulPartitionedCall:1) with shape [1, 10] to a Java object with shape [1, 10, 4]., java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (StatefulPartitionedCall:1) with shape [1, 10] to a Java object with shape [1, 10, 4].
Ninad7N
  • 544
  • 4
  • 13

0 Answers0