Reputation: 11
I try to use a .tflite file in my Android Studio Application. This model is able to detect shape in an image. The objective is to create boundings boxes around the shape of interest. For that I made this programm :
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = getContext().getAssets().openFd("yolov4.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
MappedByteBuffer modelBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset,
declaredLength);
if (modelBuffer != null) {
System.out.println("Model load with success.");
} else {
System.out.println("Error during loading model.");
}
return modelBuffer;
}
// Method to do the inference
public void createBoundingBox(Bitmap image){
int width = image.getWidth();
int height = image.getHeight();
Bitmap resizedImage = Bitmap.createBitmap(width, height, image.getConfig());
try {
MappedByteBuffer tfliteModel = loadModelFile();
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 416, 416, 3}, DataType.FLOAT32);
ByteBuffer byteBuffer = convertBitmapToByteBuffer(resizedImage);
inputFeature0.loadBuffer(byteBuffer);
Interpreter tflite = new Interpreter(tfliteModel);
Object[] inputs = {inputFeature0.getBuffer()};
Map<Integer, Object> outputs = new HashMap<>();
TensorBuffer outputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 416, 416, 3}, DataType.FLOAT32);
outputs.put(0, outputFeature0.getBuffer());
tflite.runForMultipleInputsOutputs(inputs, outputs);
List<Detection> detections = processModelOutputs(outputFeature0);
//draw the bounding box
Canvas canvas = new Canvas(resizedImage);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
for(Detection detection : detections){
float left = (detection.centerX - detection.width/2)*resizedImage.getWidth();
float top = (detection.centerY - detection.height/2)*resizedImage.getHeight();
float bottom = (detection.centerY + detection.height/2)*resizedImage.getHeight();
float right = (detection.centerX + detection.width/2)*resizedImage.getWidth();
canvas.drawRect(left, top, right, bottom, paint);
}
imageView.setImageBitmap(resizedImage);
// Releases model resources if no longer used.
tflite.close();
} catch (IOException e) {
System.out.println("Not connected to the model");
}
}
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap){
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4*416*416*3);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[imageSize*imageSize];
bitmap.getPixels(intValues, 0, 416, 0, 0, 416, 416);
for (int i = 0; i<imageSize; i++){
for(int j = 0; j<imageSize; j++) {
int val = intValues[i*416+j];
byteBuffer.putFloat(((val>>16)&0xFF)*(1.f/255));
byteBuffer.putFloat(((val>>8)&0xFF)*(1.f/255));
byteBuffer.putFloat((val&0xFF)*(1.f/255));
}
}
return byteBuffer;
}
However the error :
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)" appears for this line : "outputs.put(0, outputFeature0.getBuffer());
I tried to use the java code of the yolov4.tflite model
try {
Yolov4 model = Yolov4.newInstance(context);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 416, 416, 3}, DataType.FLOAT32);
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
Yolov4.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
}
But the same error appeared for the the line
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 416, 416, 3}, DataType.FLOAT32);
Upvotes: 1
Views: 105