hunglc007 / tensorflow-yolov4-tflite

YOLOv4, YOLOv4-tiny, YOLOv3, YOLOv3-tiny Implemented in Tensorflow 2.0, Android. Convert YOLO v4 .weights tensorflow, tensorrt and tflite
https://github.com/hunglc007/tensorflow-yolov4-tflite
MIT License
2.23k stars 1.24k forks source link

Extracting information from yolov3 tflite model of output shape [1,2535,15] is giving weird results #266

Open agh372 opened 3 years ago

agh372 commented 3 years ago

I tried using my yolov3-tiny.tflite. I'm not sure why but I'm getting something like this

enter image description here

Here is my code

private  float[][][] getBoxes(float [][][] map){
    float[][][] bboxes = new float[map.length][map[0].length][5];
    for(int i = 0; i < map.length; i++){
        for(int j = 0; j < map[i].length; j++){
            for(int k = 0; k < 5; k++){
                bboxes[i][j][k] = map[i][j][k];
            }
        }
    }
    return bboxes;
}

private  float[][][] getScore(float [][][] map){
    float[][][] scores = new float[map.length][map[0].length][labels.size()];
    for(int i = 0; i < map.length; i++){
        for(int j = 0; j < map[i].length; j++){
            for(int k = 0; k < labels.size(); k++){
                scores[i][j][k] = map[i][j][k+5];
            }
        }
    }
    return scores;
}

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]][5 + labels.size()]);
    //outputMap.put(1, new float[1][OUTPUT_WIDTH_TINY[1]][labels.size()]);
    Object[] inputArray = {byteBuffer};
    tfLite.runForMultipleInputsOutputs(inputArray, outputMap);

    int gridWidth = OUTPUT_WIDTH_TINY[0];
    float[][][] bboxes = getBoxes((float[][][])outputMap.get(0));
    float[][][] out_score = getScore((float[][][]) outputMap.get(0));

    int detectedClass = -1;
    float maxClass = 0;

    for (int i = 0; i < gridWidth;i++){
        final float[] classes = new float[labels.size()];
        for (int c = 0;c< labels.size();c++){
            classes [c] = out_score[0][i][c];
        }
        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 ));
            Log.d("Title ****************"," "+labels.get(detectedClass));

        }
    }

    return detections;
}

I don't know where I'm going wrong. Thank you!

Adithia88 commented 3 years ago

Do u use to yolov4 , i got force close when i use it in android bruh can u help me?