Skip to content

Commit

Permalink
Merge pull request #14 from kneron/add_script
Browse files Browse the repository at this point in the history
Update toolchain to v0.20.1
  • Loading branch information
MrWhoami authored Feb 1, 2023
2 parents 481e437 + 97bcb9c commit d6cacdd
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__pycache__
site/
scripts/generated_tests/
nohup.out
error.log
*.pyc
5 changes: 5 additions & 0 deletions docs/toolchain/appendix/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

## Toolchain Change log

* **[v0.20.1]**
* Update toolchain example to MobileNet v2.
* Fix knerex bias adjustment.
* Fix knerex shared weight with same name support.
* Fix other bugs.
* **[v0.20.0]**
* Support text procssing models.
* Set flatbuffer as the default 720 compiling mode.
Expand Down
42 changes: 22 additions & 20 deletions docs/toolchain/manual_1_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# 1. Toolchain Overview

**2022 Nov**
**Toolchain v0.20.0**
**2023 Feb**
**Toolchain v0.20.1**

## 1.1. Introduction

Expand All @@ -20,6 +20,11 @@ In this document, you'll learn:

**Major changes of the current version**

* **[v0.20.1]**
* Update toolchain example to MobileNet v2.
* Fix knerex bias adjustment.
* Fix knerex shared weight with same name support.
* Fix other bugs.
* **[v0.20.0]**
* Support text procssing models.
* Set flatbuffer as the default 720 compiling mode.
Expand All @@ -45,7 +50,7 @@ To keep the diagram as clear as possible, some details are omitted. But it is en
2. Fixed-point model generation. Quantize the floating-point model and generate bie file. Test the bie file and compare the result with the previous step.
3. Compilation. Batch compile multiple bie models into a nef format binary file. Test the nef file and compare the result with the previous step.

In the following parts, we will use LittleNet as the example. Details will be explained later in other sections.
In the following parts, we will use MobileNet V2 as the example. Details will be explained later in other sections.
And all the code below in this section can be found inside the docker at `/workspace/examples/test_python_api.py`.

### 1.3. Toolchain Docker Deployment
Expand Down Expand Up @@ -76,7 +81,7 @@ In the following sections, we'll introduce the API and their usage. You can also

**Note that this package is only available in the docker due to the dependency issue.**

Here the LittleNet model is already in ONNX format. So, we only need to optimize the ONNX model to fix our toolchain.
Here the MobileNet V2 model is already in ONNX format. So, we only need to optimize the ONNX model to fix our toolchain.
The following model optimization code is in Python since we are using the Python API.

```python
Expand All @@ -85,7 +90,7 @@ import onnx
import ktc

# Load the model.
original_m = onnx.load("/workspace/examples/LittleNet/LittleNet.onnx")
original_m = onnx.load("/workspace/examples/mobilenetv2/mobilenetv2_zeroq.origin.onnx")
# Optimize the model using optimizer for onnx model.
optimized_m = ktc.onnx_optimizer.onnx2onnx_flow(original_m)
# Save the onnx object optimized_m to path /data1/optimized.onnx.
Expand All @@ -99,9 +104,9 @@ IP evaluator is such a tool which can estimate the performance of your model and

```python
# Create a ModelConfig object. For details about this class, please check Appendix Python API.
# Here we set the model ID to 32769, the version to 0001 and the target platform to 520
# Here we set the model ID to 32769, the version to 8b28 and the target platform to 720
# The `optimized_m` is from the previous code block.
km = ktc.ModelConfig(32769, "0001", "520", onnx_model=optimized_m)
km = ktc.ModelConfig(32769, "8b28", "720", onnx_model=optimized_m)

# Evaluate the model. The evaluation result is saved as string into `eval_result`.
eval_result = km.evaluate()
Expand All @@ -128,18 +133,18 @@ import numpy as np
def preprocess(input_file):
image = Image.open(input_file)
image = image.convert("RGB")
img_data = np.array(image.resize((112, 96), Image.BILINEAR)) / 255
img_data = np.array(image.resize((224, 224), Image.BILINEAR)) / 255
img_data = np.transpose(img_data, (1, 0, 2))
return img_data

# Use the previous function to preprocess an example image as the input.
input_data = [preprocess("/workspace/examples/LittleNet/pytorch_imgs/Abdullah_0001.png")]
input_data = [preprocess("/workspace/examples/mobilenetv2/images/000007.jpg")]

# The `onnx_file` is generated and saved in the previous code block.
# The `input_names` are the input names of the model.
# The `input_data` order should be kept corresponding to the input names. It should be in channel last format (HWC).
# The inference result will be save as a list of array.
floating_point_inf_results = ktc.kneron_inference(input_data, onnx_file='/data1/optimized.onnx', input_names=["data_out"])
floating_point_inf_results = ktc.kneron_inference(input_data, onnx_file='/data1/optimized.onnx', input_names=["images"])
```

After getting the `floating_point_inf_results` and post-process it, you may want to compare the result with the one generated by the source model.
Expand All @@ -160,15 +165,12 @@ This is a very simple example usage. There are many more parameters for fine-tun

```python
# Preprocess images as the quantization inputs. The preprocess function is defined in the previous section.
input_images = [
preprocess("/workspace/examples/LittleNet/pytorch_imgs/Abdullah_0001.png"),
preprocess("/workspace/examples/LittleNet/pytorch_imgs/Abdullah_0002.png"),
preprocess("/workspace/examples/LittleNet/pytorch_imgs/Abdullah_0003.png"),
preprocess("/workspace/examples/LittleNet/pytorch_imgs/Abdullah_0004.png"),
]
import os
raw_images = os.listdir("/workspace/examples/mobilenetv2/images")
input_images = [preprocess("/workspace/examples/mobilenetv2/images/" + image_name) for image_name in raw_images]

# We need to prepare a dictionary, which mapping the input name to a list of preprocessed arrays.
input_mapping = {"data_out": input_images}
input_mapping = {"images": input_images}

# Quantization the model. `km` is the ModelConfig object defined in the previous section.
# The quantized model is saved as a bie file. The path to the bie file is returned as a string.
Expand All @@ -185,10 +187,10 @@ The python code would be like:
```python
# Use the previous function to preprocess an example image as the input.
# Here the input image is the same as in section 1.4.3.
input_data = [preprocess("/workspace/examples/LittleNet/pytorch_imgs/Abdullah_0001.png")]
input_data = [preprocess("/workspace/examples/mobilenetv2/images/000007.jpg")]

# Inference with a bie file. `bie_path` is defined in section 1.5.1.
fixed_point_inf_results = ktc.kneron_inference(input_data, bie_file=bie_path, input_names=["data_out"])
fixed_point_inf_results = ktc.kneron_inference(input_data, bie_file=bie_path, input_names=["images"], platform=720)

# Compare `fixed_point_inf_results` and `floating_point_inf_results` to check the precision loss.
```
Expand Down Expand Up @@ -222,7 +224,7 @@ We would use `ktc.kneron_inference` here again. And we are using the generated n

```python
# `nef_path` is defined in section 1.6.1.
binary_inf_results = ktc.kneron_inference(input_data, nef_file=nef_path)
binary_inf_results = ktc.kneron_inference(input_data, nef_file=nef_path, platform=720)

# Compare binary_inf_results and fixed_point_inf_results. They should be almost the same.
```
Expand Down
4 changes: 2 additions & 2 deletions docs/toolchain/manual_2_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ You can use the following command to pull the latest toolchain docker.
docker pull kneron/toolchain:latest
```

Note that the latest toolchain version v0.20.0. You can find the version of the toolchain in
`/workspace/version.txt` inside the docker. If you find your toolchain is earlier than v0.20.0, you may need to find the
Note that the latest toolchain version v0.20.1. You can find the version of the toolchain in
`/workspace/version.txt` inside the docker. If you find your toolchain is earlier than v0.20.1, you may need to find the
document from the [manual history](appendix/history.md).

## 2.3. Toolchain Docker Overview
Expand Down
Loading

0 comments on commit d6cacdd

Please sign in to comment.