I’m making a flutter app running a tensorflow Lite model. I got the following error:
Cannot copy from a TensorFlowLite tensor (Identity) with shape [1, 2] to a Java object with shape [1, 215].
Backstory: My trained algorithm uses 215 columns and outputs 1 or 0. Below you can find a screenshot using netron.app: Netron graph
For the life of me I cannot understand why the thing wants to copy the shape [1,2] (output) into the input Java class. The code for this is so high level that I cannot debug it properly (I guess there is a way but I’m a bit of a newbie). I can’t find my way around the Tensorflow Lite documentation for flutter, as is too vague for me, it only describes input arguments of the function breaking the code. Below is the function that is breaking, the error is in the runModelOnBinary() function. Any general direction/help/experience would be greatly appreciated as I’m kinda new to flutter, dart and AI in general. If needed, I can post the entire main.dart code but I’m not sure it’s necessary.
void evaluateModel() async {
try {
// Generate random input data with 215 columns to match the expected input size of 860 bytes
List<double> inputData = List.generate(215, (index) => Random().nextInt(2).toDouble());
// Convert List<double> to Float32List
Float32List inputBytes = Float32List.fromList(inputData);
// Convert Float32List to Uint8List
Uint8List inputUint8List = inputBytes.buffer.asUint8List();
print(inputData);
// Run the model on the input data
var output = await Tflite.runModelOnBinary(
binary: inputUint8List,
numResults: 2, // Adjust this as necessary
);
print("here2");
// Handle null or empty output
if (output == null || output.isEmpty) {
var result = output?[0]; // Assuming the output is a list of maps
setState(() {
_output = "Predicted: ${result['label']} (${result['confidence']})";
});
} else {
setState(() {
_output = output.toString();
});
}
} catch (e) {
// Catch any errors and display them
setState(() {
_output = "Error running model: $e";
});
}
}
I have tried adjusting the numResults argument, looking at documentation and even chatGippidy, put everything is too vague and doesn’t go into how this particular stack works.