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

Cannot convert between a TensorFlowLite buffer with xxx bytes and a Java Buffer with xxx bytes. #96

Open
amebrahimi opened this issue May 25, 2020 · 1 comment

Comments

@amebrahimi
Copy link

Hello, I have this problem when I'm trying to use the sample code on my image. Here is the code I have.

Future<bool> _imageIsApproved(File croppedImage) async {
  FirebaseModelInterpreter interpreter = FirebaseModelInterpreter.instance;
  FirebaseModelManager manager = FirebaseModelManager.instance;
  manager.registerLocalModelSource(
    FirebaseLocalModelSource(
        modelName: 'model', assetFilePath: 'assets/model.tflite'),
  );

  Uint8List imageBytes = await _readFileByte(croppedImage.path);
  img.Image image = img.decodeJpg(imageBytes);
  image = img.copyResize(image, width: 224, height: 224);

//The app will download the remote model. While the remote model is being downloaded, it will use the local model.
  var results = await interpreter.run(
    localModelName: "model",
    inputOutputOptions: FirebaseModelInputOutputOptions([
      FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [1, 224, 224, 3])
    ], [
      FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [1, 1001])
    ]),
    inputBytes: imageToByteList(image),
  );

  return false;
}

//float model
Uint8List imageToByteList(img.Image image) {
  var _inputSize = 224;
  var convertedBytes = Float32List(4 * _inputSize * _inputSize * 3);
  var buffer = Float32List.view(convertedBytes.buffer);
  int pixelIndex = 0;
  for (var i = 0; i < _inputSize; i++) {
    for (var j = 0; j < _inputSize; j++) {
      var pixel = image.getPixel(i, j);
      buffer[pixelIndex] = ((pixel >> 16) & 0xFF) / 255;
      pixelIndex += 1;
      buffer[pixelIndex] = ((pixel >> 8) & 0xFF) / 255;
      pixelIndex += 1;
      buffer[pixelIndex] = ((pixel) & 0xFF) / 255;
      pixelIndex += 1;
    }
  }
  return convertedBytes.buffer.asUint8List();
}

Future<Uint8List> _readFileByte(String filePath) async {
  try {
    Uri myUri = Uri.parse(filePath);
    File imageFile = new File.fromUri(myUri);
    final fileAsByte = await imageFile.readAsBytes();
    return Uint8List.fromList(fileAsByte);
  } catch (e) {
    print('Exception Error while reading audio from path:' + e.toString());
    return null;
  }
}

When intercepter.run() is trying to execute I get this error.

W/System.err(10717): Caused by: java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 150528 bytes and a Java Buffer with 602112 bytes.
W/System.err(10717): 	at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:402)
W/System.err(10717): 	at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:369)
W/System.err(10717): 	at org.tensorflow.lite.Tensor.setTo(Tensor.java:187)
W/System.err(10717): 	at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:150)
W/System.err(10717): 	at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:314)
W/System.err(10717): 	at com.google.android.gms.internal.firebase_ml.zzpz.runForMultipleInputsOutputs(Unknown Source:4)
W/System.err(10717): 	at com.google.android.gms.internal.firebase_ml.zzpr.zza(Unknown Source:85)
W/System.err(10717): 	at com.google.android.gms.internal.firebase_ml.zzpr.zza(Unknown Source:125)
W/System.err(10717): 	at com.google.android.gms.internal.firebase_ml.zznt.call(Unknown Source:4)
W/System.err(10717): 	at com.google.android.gms.internal.firebase_ml.zznn.zza(Unknown Source:30)
W/System.err(10717): 	... 6 more

Am I doing something wrong. I researched about this issue in flutter but didn't get any answers.

@amebrahimi
Copy link
Author

any updates by any chance on this issue?

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