From a2ed6efe22053f71f54b868107df3cb75fdd38c2 Mon Sep 17 00:00:00 2001 From: Jacob Manning Date: Wed, 9 May 2018 13:20:34 -0400 Subject: [PATCH] Fix label printing order There is a bug here causing predicted labels to be printed in arbitrary order. Traversing a java.util.PriorityQueue with a range-based for loop does not guarantee traversal in sorted order. Fixed by converting PQ to array, sorting array with PQ.comparator(), and returning the results in sorted order. --- .../imageclassifier/classifier/TensorFlowHelper.java | 8 +++++++- .../imageclassifier/classifier/TensorFlowHelper.java | 8 +++++++- .../imageclassifier/classifier/TensorFlowHelper.java | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/imageclassifier-add-camera/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java b/imageclassifier-add-camera/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java index caa93cf..eb64d47 100644 --- a/imageclassifier-add-camera/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java +++ b/imageclassifier-add-camera/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java @@ -34,6 +34,7 @@ import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; +import java.util.Arrays; /** * Helper functions for the TensorFlow image classifier. @@ -97,7 +98,12 @@ public int compare(Recognition lhs, Recognition rhs) { } List results = new ArrayList<>(RESULTS_TO_SHOW); - for (Recognition r: sortedLabels) { + // Array to hold the sorted results from the PQ + Recognition[] sorted_results = sortedLabels.toArray(new Recognition[RESULTS_TO_SHOW]); + // Sort the array based on the PQ's comparator + Arrays.sort(sorted_results, sortedLabels.comparator()); + + for (Recognition r: sorted_results) { results.add(0, r); } diff --git a/imageclassifier-add-intelligence/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java b/imageclassifier-add-intelligence/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java index caa93cf..eb64d47 100644 --- a/imageclassifier-add-intelligence/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java +++ b/imageclassifier-add-intelligence/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java @@ -34,6 +34,7 @@ import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; +import java.util.Arrays; /** * Helper functions for the TensorFlow image classifier. @@ -97,7 +98,12 @@ public int compare(Recognition lhs, Recognition rhs) { } List results = new ArrayList<>(RESULTS_TO_SHOW); - for (Recognition r: sortedLabels) { + // Array to hold the sorted results from the PQ + Recognition[] sorted_results = sortedLabels.toArray(new Recognition[RESULTS_TO_SHOW]); + // Sort the array based on the PQ's comparator + Arrays.sort(sorted_results, sortedLabels.comparator()); + + for (Recognition r: sorted_results) { results.add(0, r); } diff --git a/imageclassifier-start/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java b/imageclassifier-start/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java index caa93cf..eb64d47 100644 --- a/imageclassifier-start/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java +++ b/imageclassifier-start/app/src/main/java/com/example/androidthings/imageclassifier/classifier/TensorFlowHelper.java @@ -34,6 +34,7 @@ import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; +import java.util.Arrays; /** * Helper functions for the TensorFlow image classifier. @@ -97,7 +98,12 @@ public int compare(Recognition lhs, Recognition rhs) { } List results = new ArrayList<>(RESULTS_TO_SHOW); - for (Recognition r: sortedLabels) { + // Array to hold the sorted results from the PQ + Recognition[] sorted_results = sortedLabels.toArray(new Recognition[RESULTS_TO_SHOW]); + // Sort the array based on the PQ's comparator + Arrays.sort(sorted_results, sortedLabels.comparator()); + + for (Recognition r: sorted_results) { results.add(0, r); }