title: YoloV3 object detection with tflite model returns around 160 bounding boxes randomly all tagged with first class from the label text
body: <p>The shape of the TFLite model is [1, 2535, 85]. You can find the TFLite model <a href="https://drive.google.com/file/d/17wyP3Vy9RG3EC6gZ5LfaCOQFCZaenwBs/view?usp=sharing" rel="nofollow noreferrer">here</a> and label text <a href="https://drive.google.com/file/d/1L_Tdb-aggzXEJG1qyjb0e9tbcSlCRnPT/view?usp=sharing" rel="nofollow noreferrer">here</a>.</p>
  <p>This is how the bug looks.</p>
  <p><a href="https://i.stack.imgur.com/QK1IS.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QK1IS.jpg" alt="enter image description here" /></a></p>
  <p>This is the project I used <a href="https://github.com/hunglc007/tensorflow-yolov4-tflite/tree/master/android" rel="nofollow noreferrer">https://github.com/hunglc007/tensorflow-yolov4-tflite/tree/master/android</a> with some few changes. The changes are as following:</p>
  <ol>
  <li><p>Added the TFLite model and the text in the assets folder (the label text is already present in the project, its the same).</p>
  </li>
  <li><p>Line 57 DetectorActivity.java.</p>
  </li>
  </ol>
  <pre><code>private static final String TF_OD_API_MODEL_FILE = &quot;yolov3-tiny.tflite&quot;;
  private static final String TF_OD_API_LABELS_FILE = &quot;file:///android_asset/coco.txt&quot;;
  </code></pre>
  <ol start="3">
  <li>line 181 tflite/YoloV4Classifier.java.</li>
  </ol>
  <pre><code>private static boolean isTiny = true;
  </code></pre>
  <ol start="4">
  <li>line 426 tflite/YoloV4Classifier.java, (Replace the function to the down below).</li>
  </ol>
  <p>This is the code:</p>
  <pre><code>private ArrayList&lt;Recognition&gt; getDetectionsForTiny(ByteBuffer byteBuffer, Bitmap bitmap) {
      ArrayList&lt;Recognition&gt; detections = new ArrayList&lt;Recognition&gt;();
      Map&lt;Integer, Object&gt; outputMap = new HashMap&lt;&gt;();
      //  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 &lt; gridWidth; i++) {
          float maxClass = 0;
          int detectedClass = -1;
          final float[] classes = new float[labels.size()];
          for (int c = 0; c &lt; labels.size(); c++) {
              classes [c] = bboxes[0][i][c + 5];
          }
          for (int c = 0; c &lt; labels.size(); ++c) {
              if (classes[c] &gt; maxClass) {
                  detectedClass = c;
                  maxClass = classes[c];
              }
          }
          final float score = maxClass;
          if (score &gt; 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(&quot;&quot; + i, labels.get(detectedClass), score, rectF, detectedClass));
              count++;
          }
      }
      Log.d(&quot;Count&quot;, &quot; &quot; + count);
      return detections;
  }
  </code></pre>
  <p>Please I don't know where I'm going wrong! Struggling with it since days! Thanks for helping.</p>