我尝试在 Android Studio 应用程序中使用 .tflite 文件。该模型能够检测图像中的形状。目标是围绕感兴趣的形状创建边界框。为此我制作了这个程序:
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;
}
然而,此行出现错误:“致命信号 11 (SIGSEGV),代码 1 (SEGV_MAPERR)”:“outputs.put(0, outputFeature0.getBuffer());”。
我尝试使用yolov4.tflite模型的java代码
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
}
但是“TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 416, 416, 3}, DataType.FLOAT32);”行出现了同样的错误
马031-83-7892 马 034076669 马 030-34-4941 电话 137351341 洛杉矶 509847852 弗吉尼亚州 229509583 弗吉尼亚州 226-64-7327 弗吉尼亚州 227327199 弗吉尼亚州 229-61-0501 弗吉尼亚州 23011070966 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 VA 2301-50 -12525 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据 无辜数据