I have to put a 2D array of 30*126 to tflite model. How to convert a 2D array of float to ByteBuffer in java
I’m Nguyen, a Vietnamese high school student who is working on a sign language translation app project using computer vision and AI. In my app, I used LSTM model, when converted to tflite model I saw this sample code:
try {
SignLangModel model = SignLangModel.newInstance(context);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 30, 126}, DataType.FLOAT32);
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
SignLangModel.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
}
This is what my 2d array looks like
[[ 0.62733257, 0.44471735, -0.69024068, ..., 0.40363967, 0.28696212, -0.06274992],
[ 0.62688404, 0.4438577 , -0.73676074, ..., 0.40629318, 0.28771287, -0.05781016],
[ 0.62661999, 0.44294813, -0.7216031 , ..., 0.40591961, 0.28609812, -0.06014785],
...
[ 0.62216419, 0.43501934, -0.69985718, ..., 0.38580206, 0.29433241, -0.05569796]]
I wondering how to convert 2D float array to ByteBuffer.
Solution 1:
You can try the conversion as it is shown here:
public byte[] ToByteArray(float[,] nmbs)
{
byte[] nmbsBytes = new byte[nmbs.GetLength(0) * nmbs.GetLength(1)*4];
int k = 0;
for (int i = 0; i < nmbs.GetLength(0); i++)
{
for (int j = 0; j < nmbs.GetLength(1); j++)
{
byte[] array = BitConverter.GetBytes(nmbs[i, j]);
for (int m = 0; m < array.Length; m++)
{
nmbsBytes[k++] = array[m];
}
}
}
return nmbsBytes;
}
using of course floats wherever the code has bytes...and then
floar[] array = returnedArrayfromAbove;
ByteBuffer buffer = ByteBuffer.wrap(array);
BUT I think you can follow the TensorFlow Lite guide here using the appropriate dependencies in your build.gradle file and then:
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.run(input, output);
}
using Interpreter.run()
where you can input directly your 2D array.
Generally the Interpreter.run()
method will give you more flexibility than the generated code from AS. You can find many examples for using Interpreter directly there
Tag me if you need more help.