Android(Kotlin)端TFLite版YOLOX模型推理结果与Python端不一致问题求助
Android(Kotlin)端TFLite版YOLOX模型推理结果与Python端不一致问题求助
我最近在做不同目标检测模型的推理速度测试,RT-DETRv2、Yolo11和Yolo8在Python和Android两端的推理都正常,但YOLOX却遇到了棘手的问题:Python端能对测试图片输出正确的边界框,可Android端完全检测不到任何目标。
我已经把Python端能正常运行的推理逻辑复刻到了Android项目里,并且确认Android模型的输入buffer数值和Python端ravel后的Numpy数组完全一致——不管是可视化对比,还是检查数组特定位置的数值都没问题。
目前我已经排查了这些点:
- 确认输入buffer的数值与Python端ravel后的Numpy数组完全匹配
- 排除了端序问题(使用TensorImage自动处理,无需手动调整)
- 其他模型在两端都能正常推理,排除了通用的TFLite部署流程问题
现在我怀疑TensorFlow处理输入值的顺序,和数组ravel后展示的顺序不一样,但不知道怎么快速验证这个猜想。希望能得到各位的经验和思路,感谢大家!
Python端核心处理代码
if len(img.shape) == 3: padded_img = np.ones((input_size[0], input_size[1], 3), dtype=np.uint8) * 114 else: padded_img = np.ones(input_size, dtype=np.uint8) * 114 r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1]) resized_img = cv2.resize(img,(int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR,).astype(np.uint8) padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img padded_img = np.ascontiguousarray(padded_img, dtype=np.float32) interpreter = tf.lite.Interpreter(model_path=MODEL_PATH) interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() interpreter.set_tensor(input_details[0]['index'], img[None, :, :, :])
Android(Kotlin)端核心处理代码
val imgmat = Mat() Utils.bitmapToMat(decode,imgmat) val imgmat3 = Mat() Imgproc.cvtColor(imgmat,imgmat3,Imgproc.COLOR_RGBA2BGR) val resizedmat = Mat() val paddedmat = Mat() val size = Size((1920F*ratio).toDouble(),(1080F*ratio).toDouble()) val scalar = Scalar(114.0,114.0,114.0) Imgproc.resize(imgmat3,resizedmat,size, 0.0, 0.0,INTER_LINEAR) Core.copyMakeBorder(resizedmat,paddedmat,0,(imsize- (1080*ratio)).toInt(),0,0,Core.BORDER_CONSTANT,scalar) val bitmap = createBitmap(paddedmat.cols(),paddedmat.width(),Bitmap.Config.ARGB_8888) Imgproc.cvtColor(paddedmat,argbmat,Imgproc.COLOR_RGB2RGBA) Utils.matToBitmap(argbmat,bitmap) val image = TensorImage(DataType.UINT8) image.load(bitmap) val tensorproc = ImageProcessor.Builder().add(CastOp(INPUT_IMAGE_TYPE)).build() val proctensor = tensorproc.process(image) val imageBuffer = proctensor.buffer val output = TensorBuffer.createFixedSize(intArrayOf(numChannel, numElements), OUTPUT_IMAGE_TYPE) interpreter.run(imageBuffer, output.buffer)
内容来源于stack exchange
相关产品推荐
相关产品推荐

