Add a script to convert a jpg to the correct input for resnet50 with the vulkan gui (#362)

This commit is contained in:
Quinn Dawkins
2022-09-23 19:32:52 -04:00
committed by GitHub
parent 587d74b449
commit 6438bce023
3 changed files with 39 additions and 3 deletions

View File

@@ -42,6 +42,16 @@ cmake --build build/
wget https://storage.googleapis.com/shark_tank/latest/resnet50_tf/resnet50_tf.mlir
iree-compile --iree-input-type=mhlo --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-hal-target-backends=vulkan --iree-llvm-embedded-linker-path=`python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'`/iree/compiler/tools/../_mlir_libs/iree-lld --mlir-print-debuginfo --mlir-print-op-on-diagnostic=false --mlir-pass-pipeline-crash-reproducer=ist/core-reproducer.mlir --iree-llvm-target-cpu-features=host -iree-vulkan-target-triple=rdna2-unknown-linux --iree-stream-resource-index-bits=64 --iree-vm-target-index-bits=64 resnet50_tf.mlir -o resnet50_tf.vmfb
```
*Prepare the input*
```bash
python save_img.py
```
Note that this requires tensorflow, e.g.
```bash
python -m pip install tensorflow
```
*Run the vulkan_gui*
```bash
./build/vulkan_gui/iree-samples-vulkan-gui

19
cpp/save_img.py Normal file
View File

@@ -0,0 +1,19 @@
import numpy as np
import tensorflow as tf
from shark.shark_inference import SharkInference
from shark.shark_downloader import download_tf_model
def load_and_preprocess_image(fname: str):
image = tf.io.read_file(fname)
image = tf.image.decode_image(image, channels=3)
image = tf.image.resize(image, (224, 224))
image = image[tf.newaxis, :]
# preprocessing pipeline
input_tensor = tf.keras.applications.resnet50.preprocess_input(image)
return input_tensor
data = load_and_preprocess_image("dog_imagenet.jpg").numpy()
data.tofile("dog.bin")

View File

@@ -17,6 +17,7 @@
#include <cstring>
#include <set>
#include <vector>
#include <fstream>
#include "iree/hal/drivers/vulkan/api.h"
@@ -915,14 +916,20 @@ extern "C" int iree_main(int argc, char** argv) {
char filename[] = "dog_imagenet.jpg";
fprintf(stdout, "loading: %s\n", filename);
int x,y,n;
unsigned char *image_raw = stbi_load(filename, &x, &y, &n, 3);
//unsigned char *image_raw = stbi_load(filename, &x, &y, &n, 3);
stbi_load(filename, &x, &y, &n, 3);
fprintf(stdout, "res: %i x %i x %i\n", x, y, n);
/* Preprocessing needs to go here. For now use a buffer preprocessed in python.
//convert image into floating point format
for(int i=0;i<224*224*3;i++)
{
input_res50[i]= ((float)image_raw[i])/255.0f;
}
}*/
std::ifstream fin("dog.bin", std::ifstream::in | std::ifstream::binary);
fin.read((char*)input_res50, 224*224*3*sizeof(float));
// load image again so imgui can display it
int my_image_width = 0;
@@ -1060,7 +1067,7 @@ extern "C" int iree_main(int argc, char** argv) {
// ImGui Inputs for two input tensors.
// Run computation whenever any of the values changes.
static bool dirty = false;
static bool dirty = true;
if (dirty) {
// Synchronously invoke the function.