Replace samples/vision_inference with a Python script + README. (#9403)

Also remove image_util and third_party/stblib, which were only used for the C code in that sample.

Progress on https://github.com/google/iree/issues/9374
diff --git a/samples/vision_inference/CMakeLists.txt b/samples/vision_inference/CMakeLists.txt
deleted file mode 100644
index 89b2ae0..0000000
--- a/samples/vision_inference/CMakeLists.txt
+++ /dev/null
@@ -1,54 +0,0 @@
-# Copyright 2021 The IREE Authors
-#
-# Licensed under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-if(NOT IREE_TARGET_BACKEND_DYLIB_LLVM_AOT OR
-   NOT IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT)
-  return()
-endif()
-
-# Build the bytecode from the mnist.mlir in iree/samples/models.
-iree_bytecode_module(
-  NAME
-    mnist_bytecode_module
-  SRC
-    "../models/mnist.mlir"
-  C_IDENTIFIER
-    "mnist_bytecode_module_c"
-  FLAGS
-    "--iree-input-type=mhlo"
-    "--iree-mlir-to-vm-bytecode-module"
-    "--iree-hal-target-backends=dylib-llvm-aot"
-  PUBLIC
-)
-
-iree_cc_binary(
-  NAME
-    iree-run-mnist-module
-  OUT
-    iree-run-mnist-module
-  SRCS
-    "iree-run-mnist-module.c"
-  DATA
-    "samples/vision_inference/mnist_test.png"
-  DEPS
-    ::mnist_bytecode_module_c
-    iree::runtime
-    iree::tools::utils::image_util
-)
-
-iree_lit_test(
-  NAME
-    iree_run_mnist_module_test
-  TEST_FILE
-    "mnist_test.txt"
-  TOOLS
-    ::iree-run-mnist-module
-    FileCheck
-  DATA
-    "samples/vision_inference/mnist_test.png"
-  LABELS
-    "hostonly"
-)
diff --git a/samples/vision_inference/README.md b/samples/vision_inference/README.md
new file mode 100644
index 0000000..851c76e
--- /dev/null
+++ b/samples/vision_inference/README.md
@@ -0,0 +1,29 @@
+# Vision Inference Sample
+
+This sample demonstrates how to run a MNIST handwritten digit detection vision
+model on an image using IREE's command line tools.
+
+## Instructions
+
+From this directory:
+
+```bash
+# Compile the MNIST program.
+iree-compile \
+    ../models/mnist.mlir \
+    --iree-input-type=mhlo \
+    --iree-hal-target-backends=cpu \
+    -o /tmp/mnist_cpu.vmfb
+
+# Convert the test image to the 1x28x28x1xf32 buffer format the program expects.
+cat mnist_test.png | python3 convert_to_float_grayscale.py > /tmp/mnist_test.bin
+
+# Run the program, passing the path to the binary file as a function input.
+iree-run-module \
+  /tmp/mnist_test.bin \
+  --function_input=1x28x28x1xf32=@/tmp/mnist_test.bin
+
+# Observe the results - a list of prediction confidence scores for each digit.
+```
+
+<!-- TODO(scotttodd): lit test for that ^ (requires python in lit.cfg.py) -->
diff --git a/samples/vision_inference/convert_to_float_grayscale.py b/samples/vision_inference/convert_to_float_grayscale.py
new file mode 100644
index 0000000..0b2e8fb
--- /dev/null
+++ b/samples/vision_inference/convert_to_float_grayscale.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+# Copyright 2022 The IREE Authors
+#
+# Licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+import sys
+from PIL import Image
+import numpy as np
+
+# Read image from stdin (in any format supported by PIL).
+with Image.open(sys.stdin.buffer) as color_img:
+  # Convert to grayscale.
+  grayscale_img = color_img.convert('L')
+  # Rescale to a float32 in range [0.0, 1.0].
+  grayscale_arr = np.array(grayscale_img)
+  grayscale_arr_f32 = grayscale_arr.astype(np.float32) / 255.0
+  # Write bytes back out to stdout.
+  sys.stdout.buffer.write(grayscale_arr_f32.tobytes())
diff --git a/samples/vision_inference/iree-run-mnist-module.c b/samples/vision_inference/iree-run-mnist-module.c
deleted file mode 100644
index 581ef09..0000000
--- a/samples/vision_inference/iree-run-mnist-module.c
+++ /dev/null
@@ -1,120 +0,0 @@
-// Copyright 2021 The IREE Authors
-//
-// Licensed under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-// This sample uses iree/tools/utils/image_util to load a hand-written image
-// as an iree_hal_buffer_view_t then passes it to the bytecode module built
-// from mnist.mlir on the dylib-llvm-aot backend.
-
-#include <float.h>
-
-#include "iree/runtime/api.h"
-#include "iree/tools/utils/image_util.h"
-#include "samples/vision_inference/mnist_bytecode_module_c.h"
-
-iree_status_t Run(const iree_string_view_t image_path) {
-  iree_runtime_instance_options_t instance_options;
-  iree_runtime_instance_options_initialize(IREE_API_VERSION_LATEST,
-                                           &instance_options);
-  iree_runtime_instance_options_use_all_available_drivers(&instance_options);
-  iree_runtime_instance_t* instance = NULL;
-  IREE_RETURN_IF_ERROR(iree_runtime_instance_create(
-      &instance_options, iree_allocator_system(), &instance));
-
-  // TODO(#5724): move device selection into the compiled modules.
-  iree_hal_device_t* device = NULL;
-  IREE_RETURN_IF_ERROR(iree_runtime_instance_try_create_default_device(
-      instance, iree_make_cstring_view("local-task"), &device));
-
-  // Create one session per loaded module to hold the module state.
-  iree_runtime_session_options_t session_options;
-  iree_runtime_session_options_initialize(&session_options);
-  iree_runtime_session_t* session = NULL;
-  IREE_RETURN_IF_ERROR(iree_runtime_session_create_with_device(
-      instance, &session_options, device,
-      iree_runtime_instance_host_allocator(instance), &session));
-  iree_hal_device_release(device);
-
-  const struct iree_file_toc_t* module_file = mnist_bytecode_module_c_create();
-
-  IREE_RETURN_IF_ERROR(iree_runtime_session_append_bytecode_module_from_memory(
-      session, iree_make_const_byte_span(module_file->data, module_file->size),
-      iree_allocator_null()));
-
-  iree_runtime_call_t call;
-  IREE_RETURN_IF_ERROR(iree_runtime_call_initialize_by_name(
-      session, iree_make_cstring_view("module.predict"), &call));
-
-  // Prepare the input hal buffer view with image_util library.
-  // The input of the mmist model is single 28x28 pixel image as a
-  // tensor<1x28x28x1xf32>, with pixels in [0.0, 1.0].
-  iree_hal_buffer_view_t* buffer_view = NULL;
-  iree_hal_dim_t buffer_shape[] = {1, 28, 28, 1};
-  iree_hal_element_type_t hal_element_type = IREE_HAL_ELEMENT_TYPE_FLOAT_32;
-  float input_range[2] = {0.0f, 1.0f};
-  IREE_RETURN_IF_ERROR(
-      iree_tools_utils_buffer_view_from_image_rescaled(
-          image_path, buffer_shape, IREE_ARRAYSIZE(buffer_shape),
-          hal_element_type, iree_hal_device_allocator(device), input_range,
-          IREE_ARRAYSIZE(input_range), &buffer_view),
-      "load image");
-  IREE_RETURN_IF_ERROR(
-      iree_runtime_call_inputs_push_back_buffer_view(&call, buffer_view));
-  iree_hal_buffer_view_release(buffer_view);
-
-  IREE_RETURN_IF_ERROR(iree_runtime_call_invoke(&call, /*flags=*/0));
-
-  // Get the result buffers from the invocation.
-  iree_hal_buffer_view_t* ret_buffer_view = NULL;
-  IREE_RETURN_IF_ERROR(
-      iree_runtime_call_outputs_pop_front_buffer_view(&call, &ret_buffer_view));
-
-  // Read back the results. The output of the mnist model is a 1x10 prediction
-  // confidence values for each digit in [0, 9].
-  float predictions[1 * 10] = {0.0f};
-  IREE_RETURN_IF_ERROR(iree_hal_device_transfer_d2h(
-      iree_runtime_session_device(session),
-      iree_hal_buffer_view_buffer(ret_buffer_view), 0, predictions,
-      sizeof(predictions), IREE_HAL_TRANSFER_BUFFER_FLAG_DEFAULT,
-      iree_infinite_timeout()));
-  iree_hal_buffer_view_release(ret_buffer_view);
-
-  // Get the highest index from the output.
-  float result_val = FLT_MIN;
-  int result_idx = 0;
-  for (iree_host_size_t i = 0; i < IREE_ARRAYSIZE(predictions); ++i) {
-    if (predictions[i] > result_val) {
-      result_val = predictions[i];
-      result_idx = i;
-    }
-  }
-  fprintf(stdout, "Detected number: %d\n", result_idx);
-
-  iree_runtime_call_deinitialize(&call);
-  iree_runtime_session_release(session);
-  iree_runtime_instance_release(instance);
-  return iree_ok_status();
-}
-
-int main(int argc, char** argv) {
-  if (argc > 2) {
-    fprintf(stderr, "Usage: iree-run-mnist-module <image file>\n");
-    return -1;
-  }
-  iree_string_view_t image_path;
-  if (argc == 1) {
-    image_path = iree_make_cstring_view("mnist_test.png");
-  } else {
-    image_path = iree_make_cstring_view(argv[1]);
-  }
-  iree_status_t result = Run(image_path);
-  if (!iree_status_is_ok(result)) {
-    iree_status_fprint(stderr, result);
-    iree_status_ignore(result);
-    return -1;
-  }
-  iree_status_ignore(result);
-  return 0;
-}
diff --git a/samples/vision_inference/mnist_test.txt b/samples/vision_inference/mnist_test.txt
deleted file mode 100644
index 03dcbba..0000000
--- a/samples/vision_inference/mnist_test.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-// RUN: (iree-run-mnist-module %S/mnist_test.png) | FileCheck %s
-// CHECK-LABEL: Detected number
-// CHECK: 4