Monday 16 November 2020

YoloV3 object detection with tflite model returns around 160 bounding boxes randomly all tagged with first class from the label text

The shape of the TFLite model is [1, 2535, 85]. You can find the TFLite model here and label text here.

This is how the bug looks.

enter image description here

This is the project I used https://github.com/hunglc007/tensorflow-yolov4-tflite/tree/master/android with some few changes. The changes are as following:

  1. Added the TFLite model and the text in the assets folder (the label text is already present in the project, its the same).

  2. Line 57 DetectorActivity.java.

private static final String TF_OD_API_MODEL_FILE = "yolov3-tiny.tflite";
private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/coco.txt";
  1. line 181 tflite/YoloV4Classifier.java.
private static boolean isTiny = true;
  1. line 426 tflite/YoloV4Classifier.java, (Replace the function to the down below).

This is the code:

private ArrayList<Recognition> getDetectionsForTiny(ByteBuffer byteBuffer, Bitmap bitmap) {
    ArrayList<Recognition> detections = new ArrayList<Recognition>();
    Map<Integer, Object> outputMap = new HashMap<>();
    //  outputMap.put(0, new float[1][OUTPUT_WIDTH_TINY[0]][4]);
    outputMap.put(0, new float[1][OUTPUT_WIDTH_TINY[1]][labels.size() + 5]);
    Object[] inputArray = {byteBuffer};
    tfLite.runForMultipleInputsOutputs(inputArray, outputMap);
    int gridWidth = OUTPUT_WIDTH_TINY[0];
    float[][][] bboxes = (float [][][]) outputMap.get(0);
    // float[][][] out_score = (float[][][]) outputMap.get(1);
    int count = 0;
    for (int i = 0; i < gridWidth; i++) {
        float maxClass = 0;
        int detectedClass = -1;
        final float[] classes = new float[labels.size()];
        for (int c = 0; c < labels.size(); c++) {
            classes [c] = bboxes[0][i][c + 5];
        }
        for (int c = 0; c < labels.size(); ++c) {
            if (classes[c] > maxClass) {
                detectedClass = c;
                maxClass = classes[c];
            }
        }
        final float score = maxClass;
        if (score > getObjThresh()) {
            final float xPos = bboxes[0][i][0];
            final float yPos = bboxes[0][i][1];
            final float w = bboxes[0][i][2];
            final float h = bboxes[0][i][3];
            final RectF rectF = new RectF(
                Math.max(0, xPos - w / 2),
                Math.max(0, yPos - h / 2),
                Math.min(bitmap.getWidth() - 1, xPos + w / 2),
                Math.min(bitmap.getHeight() - 1, yPos + h / 2));
            detections.add(new Recognition("" + i, labels.get(detectedClass), score, rectF, detectedClass));
            count++;
        }
    }
    Log.d("Count", " " + count);
    return detections;
}

Please I don't know where I'm going wrong! Struggling with it since days! Thanks for helping.



from YoloV3 object detection with tflite model returns around 160 bounding boxes randomly all tagged with first class from the label text

No comments:

Post a Comment