Enable loading external image input for mobilenet_v2

Add support for loading external image input for mobilenet_v2 (similar as in mobilenet_v1).

Change-Id: I949a1f983e3f44dc4b4efbce56a6d7537d24975d
diff --git a/samples/quant_model/CMakeLists.txt b/samples/quant_model/CMakeLists.txt
index cc88e7c..91d3250 100644
--- a/samples/quant_model/CMakeLists.txt
+++ b/samples/quant_model/CMakeLists.txt
@@ -191,8 +191,11 @@
     SRCS
       "mobilenet_v2.c"
     DEPS
+      ::mobilenet_quant_input_c
       ::mobilenet_v2_bytecode_module_dylib_c
       samples::util::util
+    LINKOPTS
+      "LINKER:--defsym=__stack_size__=150k"
   )
 endif()
 
diff --git a/samples/quant_model/mobilenet_v2.c b/samples/quant_model/mobilenet_v2.c
index 990d724..8a05907 100644
--- a/samples/quant_model/mobilenet_v2.c
+++ b/samples/quant_model/mobilenet_v2.c
@@ -6,9 +6,11 @@
 
 #include "iree/base/api.h"
 #include "iree/hal/api.h"
+#include "mobilenet_v2.h"
 #include "samples/util/util.h"
 
 // Compiled module embedded here to avoid file IO:
+#include "samples/quant_model/mobilenet_quant_input_c.h"
 #include "samples/quant_model/mobilenet_v2_bytecode_module_dylib_c.h"
 
 const MlModel kModel = {
@@ -32,24 +34,37 @@
                                    module_file_toc->size);
 }
 
+MobilenetV2Output score;
+
 iree_status_t load_input_data(const MlModel *model, void **buffer,
                               iree_byte_span_t **byte_span) {
-  iree_status_t result = alloc_input_buffer(model, buffer);
-  // Populate initial value
-  srand(33333333);
-  if (iree_status_is_ok(result)) {
-    for (int i = 0; i < model->input_length[0]; ++i) {
-      ((uint8_t *)*buffer)[i] = (uint8_t)rand();
-    }
-  }
   byte_span[0] = malloc(sizeof(iree_byte_span_t));
-  *byte_span[0] = iree_make_byte_span(
-      buffer[0], model->input_size_bytes[0] * model->input_length[0]);
-  return result;
+  *byte_span[0] =
+      iree_make_byte_span(mobilenet_quant_input,
+                          model->input_size_bytes[0] * model->input_length[0]);
+  return iree_ok_status();
 }
 
 iree_status_t process_output(const MlModel *model,
-                              iree_hal_buffer_mapping_t *buffers,
-                              MlOutput *output) {
-  return iree_ok_status();
+                  iree_hal_buffer_mapping_t *buffers,
+                  MlOutput *output) {
+  iree_status_t result = iree_ok_status();
+  // find the label index with best prediction
+  int best_out = 0;
+  int best_idx = -1;
+  for (int i = 0; i < model->output_length[0]; ++i) {
+    uint8_t out = ((uint8_t *)buffers[0].contents.data)[i];
+    if (out > best_out) {
+      best_out = out;
+      best_idx = i;
+    }
+  }
+  score.best_out = best_out;
+  score.best_idx = best_idx;
+
+  LOG_INFO("Image prediction result is: id: %d", best_idx + 1);
+
+  output->result = &score;
+  output->len = sizeof(score);
+  return result;
 }
diff --git a/samples/quant_model/mobilenet_v2.h b/samples/quant_model/mobilenet_v2.h
new file mode 100644
index 0000000..42b09dc
--- /dev/null
+++ b/samples/quant_model/mobilenet_v2.h
@@ -0,0 +1,11 @@
+#ifndef SAMPLES_MOBILENETV2_H
+#define SAMPLES_MOBILENETV2_H
+
+#include <stdint.h>
+
+typedef struct {
+    int best_idx;
+    int best_out;
+} MobilenetV2Output;
+
+#endif