我将模型训练为: https ://www.google.com.au/amp/s/blog.roboflow.com/training-a-tensorflow-object-detection-model-with-a-custom-dataset/amp/ 并将其转换为 tflite。然后我尝试将 AI 模型放入一个 android APP 中。我跟着: https://developers.google.com/ml-kit/vision/object-detection/custom-models/android?fbclid=IwAR07uNgzQ2c5PTp13TiPVeKGQsXaJnJR9jzyvtviXCRegFFJlM-_G799TlY 将位图转换为 InputImage 对象。并进行所有配置。我转换了图像,然后加载模型尝试打印结果:
// getting bitmap of the image
Bitmap photo = (Bitmap) data.getExtras().get("data");
//convert image
InputImage image = InputImage.fromBitmap(photo,0);
//load local model
LocalModel localModel =
new LocalModel.Builder()
.setAssetFilePath("mobilenet_v1_1.0_224_quant.tflite")
// or .setAbsoluteFilePath(absolute file path to tflite model)
.build();
// Multiple object detection in static images
CustomObjectDetectorOptions customObjectDetectorOptions =
new CustomObjectDetectorOptions.Builder(localModel)
.setDetectorMode(CustomObjectDetectorOptions.SINGLE_IMAGE_MODE)
.enableMultipleObjects()
.enableClassification()
.setClassificationConfidenceThreshold(0.5f)
.setMaxPerObjectLabelCount(3)
.build();
ObjectDetector objectDetector =
ObjectDetection.getClient(customObjectDetectorOptions);
objectDetector
.process(image)
.addOnFailureListener(e -> {System.out.println(e.getMessage());})
.addOnSuccessListener(results -> {
for (DetectedObject detectedObject : results) {
Rect boundingBox = detectedObject.getBoundingBox();
Integer trackingId = detectedObject.getTrackingId();
for (DetectedObject.Label label : detectedObject.getLabels()) {
String text = label.getText();
int index = label.getIndex();
float confidence = label.getConfidence();
System.out.println(text);
System.out.println(index);
System.out.println(confidence);
}
System.out.println(boundingBox);
System.out.println(trackingId);
}
});
但我得到了错误:
“无法初始化检测器。输出索引 0 的维度数量异常:获得 3D,预期为 2D。”
你对这个问题有什么想法吗?如果您能给我一些解决方案,非常感谢。
完整的错误:
E/native: calculator_graph.cc:776 INVALID_ARGUMENT: CalculatorGraph::Run() failed in Run:
Calculator::Open() for node "[BoxClassifierCalculator, BoxClassifierCalculator with output stream: detection_results0]" failed: #vk Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
pipeline_jni.cc:62 CalculatorGraph::Run() failed in Run:
Calculator::Open() for node "[BoxClassifierCalculator, BoxClassifierCalculator with output stream: detection_results0]" failed: #vk Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
E/native: pipeline_jni.cc:209 Graph has errors:
Calculator::Open() for node "[BoxClassifierCalculator, BoxClassifierCalculator with output stream: detection_results0]" failed: #vk Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
E/MobileVisionBase: Error preloading model resource
com.google.mlkit.common.MlKitException: Failed to initialize detector. Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
at com.google.mlkit.vision.vkp.PipelineManager.start(com.google.mlkit:vision-internal-vkp@@16.0.0:68)
at com.google.mlkit.vision.objects.custom.internal.zzd.load(com.google.mlkit:object-detection-custom@@16.0.0:79)
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@16.0.0:22)
at com.google.mlkit.common.sdkinternal.zzn.call(com.google.mlkit:common@@16.0.0)
at com.google.mlkit.common.sdkinternal.zzm.run(com.google.mlkit:common@@16.0.0:5)
at com.google.mlkit.common.sdkinternal.zzq.run(com.google.mlkit:common@@16.0.0:3)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.google.android.gms.internal.mlkit_common.zzb.dispatchMessage(com.google.mlkit:common@@16.0.0:6)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)