Skip to content

Commit 087052f

Browse files
authored
Merge pull request hunglc007#162 from WinstonHuTiger/master
Fixing yolov4-tiny not running on Android
2 parents dafb42f + c8a7e70 commit 087052f

File tree

5 files changed

+78
-11
lines changed

5 files changed

+78
-11
lines changed

Diff for: android/app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ dependencies {
5050
implementation 'com.google.android.material:material:1.1.0'
5151
// implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
5252
// implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
53-
implementation 'org.tensorflow:tensorflow-lite:2.1.0'
54-
implementation 'org.tensorflow:tensorflow-lite-gpu:2.1.0'
53+
implementation 'org.tensorflow:tensorflow-lite:2.2.0'
54+
implementation 'org.tensorflow:tensorflow-lite-gpu:2.2.0'
5555
// implementation 'org.tensorflow:tensorflow-lite:0.0.0-gpu-experimental'
5656
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
5757
implementation 'com.google.code.gson:gson:2.8.6'

Diff for: android/app/src/main/assets/yolov4-416-fp32.tflite

23.2 MB
Binary file not shown.

Diff for: android/app/src/main/java/org/tensorflow/lite/examples/detection/DetectorActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class DetectorActivity extends CameraActivity implements OnImageAvailable
5454

5555
private static final int TF_OD_API_INPUT_SIZE = 416;
5656
private static final boolean TF_OD_API_IS_QUANTIZED = false;
57-
private static final String TF_OD_API_MODEL_FILE = "yolov4full.tflite";
57+
private static final String TF_OD_API_MODEL_FILE = "yolov4-416-fp32.tflite";
5858

5959
private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/coco.txt";
6060

Diff for: android/app/src/main/java/org/tensorflow/lite/examples/detection/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void run() {
7474

7575
private static final boolean TF_OD_API_IS_QUANTIZED = false;
7676

77-
private static final String TF_OD_API_MODEL_FILE = "yolov4full.tflite";
77+
private static final String TF_OD_API_MODEL_FILE = "yolov4-416-fp32.tflite";
7878

7979
private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/coco.txt";
8080

Diff for: android/app/src/main/java/org/tensorflow/lite/examples/detection/tflite/YoloV4Classifier.java

+74-7
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,17 @@ public float getObjThresh() {
178178
// Number of threads in the java app
179179
private static final int NUM_THREADS = 4;
180180
private static boolean isNNAPI = false;
181-
private static boolean isGPU = false;
181+
private static boolean isGPU = true;
182+
183+
// tiny or not
184+
private static boolean isTiny = true;
185+
186+
// config yolov4 tiny
187+
private static final int[] OUTPUT_WIDTH_TINY = new int[]{2535, 2535};
188+
private static final int[][] MASKS_TINY = new int[][]{{3, 4, 5}, {1, 2, 3}};
189+
private static final int[] ANCHORS_TINY = new int[]{
190+
23, 27, 37, 58, 81, 82, 81, 82, 135, 169, 344, 319};
191+
private static final float[] XYSCALE_TINY = new float[]{1.05f, 1.05f};
182192

183193
private boolean isModelQuantized;
184194

@@ -294,9 +304,8 @@ protected ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
294304
return byteBuffer;
295305
}
296306

297-
public ArrayList<Recognition> recognizeImage(Bitmap bitmap) {
298-
ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
299-
307+
private ArrayList<Recognition> getDetections(ByteBuffer byteBuffer, Bitmap bitmap) {
308+
ArrayList<Recognition> detections = new ArrayList<Recognition>();
300309
Map<Integer, Object> outputMap = new HashMap<>();
301310
for (int i = 0; i < OUTPUT_WIDTH.length; i++) {
302311
float[][][][][] out = new float[1][OUTPUT_WIDTH[i]][OUTPUT_WIDTH[i]][3][5 + labels.size()];
@@ -308,8 +317,6 @@ public ArrayList<Recognition> recognizeImage(Bitmap bitmap) {
308317
Object[] inputArray = {byteBuffer};
309318
tfLite.runForMultipleInputsOutputs(inputArray, outputMap);
310319

311-
ArrayList<Recognition> detections = new ArrayList<Recognition>();
312-
313320
for (int i = 0; i < OUTPUT_WIDTH.length; i++) {
314321
int gridWidth = OUTPUT_WIDTH[i];
315322
float[][][][][] out = (float[][][][][]) outputMap.get(i);
@@ -364,9 +371,69 @@ public ArrayList<Recognition> recognizeImage(Bitmap bitmap) {
364371
}
365372
Log.d("YoloV4Classifier", "out[" + i + "] detect end");
366373
}
374+
return detections;
375+
}
367376

368-
final ArrayList<Recognition> recognitions = nms(detections);
377+
/**
378+
* For yolov4-tiny, the situation would be a little different from the yolov4, it only has two
379+
* output. Both has three dimenstion. The first one is a tensor with dimension [1, 2535,4], containing all the bounding boxes.
380+
* The second one is a tensor with dimension [1, 2535, class_num], containing all the classes score.
381+
* @param byteBuffer input ByteBuffer, which contains the image information
382+
* @param bitmap pixel disenty used to resize the output images
383+
* @return an array list containing the recognitions
384+
*/
385+
private ArrayList<Recognition> getDetectionsForTiny(ByteBuffer byteBuffer, Bitmap bitmap) {
386+
ArrayList<Recognition> detections = new ArrayList<Recognition>();
387+
Map<Integer, Object> outputMap = new HashMap<>();
388+
outputMap.put(0, new float[1][OUTPUT_WIDTH_TINY[0]][4]);
389+
outputMap.put(1, new float[1][OUTPUT_WIDTH_TINY[1]][labels.size()]);
390+
Object[] inputArray = {byteBuffer};
391+
tfLite.runForMultipleInputsOutputs(inputArray, outputMap);
392+
393+
int gridWidth = OUTPUT_WIDTH_TINY[0];
394+
float[][][] bboxes = (float [][][]) outputMap.get(0);
395+
float[][][] out_score = (float[][][]) outputMap.get(1);
396+
397+
for (int i = 0; i < gridWidth;i++){
398+
float maxClass = 0;
399+
int detectedClass = -1;
400+
final float[] classes = new float[labels.size()];
401+
for (int c = 0;c< labels.size();c++){
402+
classes [c] = out_score[0][i][c];
403+
}
404+
for (int c = 0;c<labels.size();++c){
405+
if (classes[c] > maxClass){
406+
detectedClass = c;
407+
maxClass = classes[c];
408+
}
409+
}
410+
final float score = maxClass;
411+
if (score > getObjThresh()){
412+
final float xPos = bboxes[0][i][0];
413+
final float yPos = bboxes[0][i][1];
414+
final float w = bboxes[0][i][2];
415+
final float h = bboxes[0][i][3];
416+
final RectF rectF = new RectF(
417+
Math.max(0, xPos - w / 2),
418+
Math.max(0, yPos - h / 2),
419+
Math.min(bitmap.getWidth() - 1, xPos + w / 2),
420+
Math.min(bitmap.getHeight() - 1, yPos + h / 2));
421+
detections.add(new Recognition("" + i, labels.get(detectedClass),score,rectF,detectedClass ));
422+
}
423+
}
424+
return detections;
425+
}
369426

427+
public ArrayList<Recognition> recognizeImage(Bitmap bitmap) {
428+
ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
429+
ArrayList<Recognition> detections;
430+
//check whether the tiny version is specified
431+
if (isTiny) {
432+
detections = getDetectionsForTiny(byteBuffer, bitmap);
433+
} else {
434+
detections = getDetections(byteBuffer, bitmap);
435+
}
436+
final ArrayList<Recognition> recognitions = nms(detections);
370437
return recognitions;
371438
}
372439

0 commit comments

Comments
 (0)