我一直在尝试使用最新的 MobileNet MobileNet_v3 来运行对象检测。您可以从此处找到 Google 的预训练模型,例如我正在尝试使用的模型“ssd_mobilenet_v3_large_coco”:https ://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/ detection_model_zoo.md
我不知道这些新模型是如何获取图像数据输入的,我在网上也找不到任何关于此的深入文档。以下 java 代码总结了我如何尝试从我可以在线收集的有限数量中提供模型(特别是使用 TensorFlow Lite 的 .tflite 模型)图像数据,但该模型仅返回 10^-20 阶的预测置信度,所以它从不真正识别任何东西。我认为我一定做错了。
//Note that the model takes a 320 x 320 image
//Get image data as integer values
private int[] intValues;
intValues = new int[320 * 320];
private Bitmap croppedBitmap = null;
croppedBitmap = Bitmap.createBitmap(320, 320, Config.ARGB_8888);
croppedBitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
//create ByteBuffer as input for running ssd_mobilenet_v3
private ByteBuffer imgData;
imgData = ByteBuffer.allocateDirect(320 * 320 * 3);
imgData.order(ByteOrder.nativeOrder());
//fill Bytebuffer
//Note that & 0xFF is for just getting the last 8 bits, which converts to RGB values here
imgData.rewind();
for (int i = 0; i < inputSize; ++i) {
for (int j = 0; j < inputSize; ++j) {
int pixelValue = intValues[i * inputSize + j];
// Quantized model
imgData.put((byte) ((pixelValue >> 16) & 0xFF));
imgData.put((byte) ((pixelValue >> 8) & 0xFF));
imgData.put((byte) (pixelValue & 0xFF));
}
}
// Set up output buffers
private float[][][] output0;
private float[][][][] output1;
output0 = new float[1][2034][91];
output1 = new float[1][2034][1][4];
//Create input HashMap and run the model
Object[] inputArray = {imgData};
Map<Integer, Object> outputMap = new HashMap<>();
outputMap.put(0, output0);
outputMap.put(1, output1);
tfLite.runForMultipleInputsOutputs(inputArray, outputMap);
//Examine Confidences to see if any significant detentions were made
for (int i = 0; i < 2034; i++) {
for (int j = 0; j < 91; j++) {
System.out.println(output0[0][i][j]);
}
}