admin管理员组文章数量:1356877
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = getAssets().openFd("model1.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
}
private void runModelOnImage() {
if (imageBitmap == null) {
Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
return;
}
Bitmap resizedImage = Bitmap.createScaledBitmap(imageBitmap, MODEL_INPUT_SIZE, MODEL_INPUT_SIZE, true);
ByteBuffer byteBuffer = convertBitmapToByteBuffer(resizedImage);
if (tflite == null) {
Toast.makeText(this, "Model not loaded!", Toast.LENGTH_SHORT).show();
return;
}
float[][] output = new float[1][4]; // Adjust based on the number of classes
try {
tflite.run(byteBuffer, output); // Ensure input matches expected shape
displayPrediction(output);
} catch (Exception e) {
Toast.makeText(this, "Error running model: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 1 * MODEL_INPUT_SIZE * MODEL_INPUT_SIZE * 3); // Adding batch size
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[MODEL_INPUT_SIZE * MODEL_INPUT_SIZE];
bitmap.getPixels(intValues, 0, MODEL_INPUT_SIZE, 0, 0, MODEL_INPUT_SIZE, MODEL_INPUT_SIZE);
for (int pixelValue : intValues) {
byteBuffer.putFloat(((pixelValue >> 16) & 0xFF) / 255.0f);
byteBuffer.putFloat(((pixelValue >> 8) & 0xFF) / 255.0f);
byteBuffer.putFloat((pixelValue & 0xFF) / 255.0f);
}
return byteBuffer;
}
Here is the error running model:
Cannot copy to a tensorflowlite tensor (serving_default_keras_tensor_142:0) with 25165824 bytes from a java buffer with 602112 bytes.
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = getAssets().openFd("model1.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
}
private void runModelOnImage() {
if (imageBitmap == null) {
Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
return;
}
Bitmap resizedImage = Bitmap.createScaledBitmap(imageBitmap, MODEL_INPUT_SIZE, MODEL_INPUT_SIZE, true);
ByteBuffer byteBuffer = convertBitmapToByteBuffer(resizedImage);
if (tflite == null) {
Toast.makeText(this, "Model not loaded!", Toast.LENGTH_SHORT).show();
return;
}
float[][] output = new float[1][4]; // Adjust based on the number of classes
try {
tflite.run(byteBuffer, output); // Ensure input matches expected shape
displayPrediction(output);
} catch (Exception e) {
Toast.makeText(this, "Error running model: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 1 * MODEL_INPUT_SIZE * MODEL_INPUT_SIZE * 3); // Adding batch size
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[MODEL_INPUT_SIZE * MODEL_INPUT_SIZE];
bitmap.getPixels(intValues, 0, MODEL_INPUT_SIZE, 0, 0, MODEL_INPUT_SIZE, MODEL_INPUT_SIZE);
for (int pixelValue : intValues) {
byteBuffer.putFloat(((pixelValue >> 16) & 0xFF) / 255.0f);
byteBuffer.putFloat(((pixelValue >> 8) & 0xFF) / 255.0f);
byteBuffer.putFloat((pixelValue & 0xFF) / 255.0f);
}
return byteBuffer;
}
Here is the error running model:
Share Improve this question edited Mar 31 at 18:42 MetaSnarf 6,1873 gold badges28 silver badges42 bronze badges asked Mar 31 at 6:11 Moses MusireMoses Musire 1 New contributor Moses Musire is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.Cannot copy to a tensorflowlite tensor (serving_default_keras_tensor_142:0) with 25165824 bytes from a java buffer with 602112 bytes.
1 Answer
Reset to default 0Your error indicates that TensorFlow Lite expects a buffer with 25165824 bytes, but your current buffer has 602112 bytes
Ensure that the size of the ByteBuffer
matches the model's input shape. For example, if your model expects an input size of 224x224x3
, you should allocate the ByteBuffer
as follows:
int inputSize = 224; // Replace with the actual size your model expects ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 1 * inputSize * inputSize * 3); byteBuffer.order(ByteOrder.nativeOrder());
Try modifying your method convertBitmapToByteBuffer with below code whether it works
int inputSize = 224; // Replace with the correct model input size
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 1 * inputSize * inputSize * 3);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[inputSize * inputSize];
bitmap.getPixels(intValues, 0, inputSize, 0, 0, inputSize, inputSize);
for (int pixelValue : intValues) {
byteBuffer.putFloat(((pixelValue >> 16) & 0xFF) / 255.0f);
byteBuffer.putFloat(((pixelValue >> 8) & 0xFF) / 255.0f);
byteBuffer.putFloat((pixelValue & 0xFF) / 255.0f);
}
return byteBuffer;
Also, confirm that the model file is loaded properly:
MappedByteBuffer modelFile = loadModelFile(); Interpreter.Options options = new Interpreter.Options(); tflite = new Interpreter(modelFile, options);
本文标签: javaLoading a model but i get this error of bytesStack Overflow
版权声明:本文标题:java - Loading a model but i get this error of bytes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743965281a2569773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论