Fix test_snpe so it works with new olive commands.

This commit is contained in:
Chris Lovett 2023-04-04 21:24:52 -07:00
Родитель 3a115cd048
Коммит b6ee9af9db
3 изменённых файлов: 37 добавлений и 65 удалений

5
tasks/face_segmentation/snpe/azure/loop.sh Normal file → Executable file
Просмотреть файл

@ -4,10 +4,7 @@
script_dir="$(dirname ${BASH_SOURCE})"
source ~/anaconda3/etc/profile.d/conda.sh
conda activate snap-37
# pushd $SNPE_ROOT
# source bin/envsetup.sh -o ~/anaconda3/envs/snap/lib/python3.6/site-packages/onnx
# popd
conda activate snap37
while true
do

Просмотреть файл

@ -71,52 +71,23 @@ you need to double check something.
1. **Prepare data**. Run `python create_data.py --help`, this scripts creates data for both quantization and test and puts it in your local experiment folder under `data/test` and `data/quant`.
For example:
```
python create_data.py --input d:\datasets\FaceSynthetics --count 100 --dim 256
python create_data.py --input ~/datasets/FaceSynthetics --count 1000 --dim 256
```
1. **Copy your tensorflow model**. In your experiment folder create a folder named `model` and copy your
trained tensorflow model into this folder. You should have something like:
1. **Convert and quantize model**. You can use `test_snpe.py` to convert a .onnx model to .dlc and
quantize it. For example:
``
python test_snpe.py --quantize --model model.onnx
```
checkpoint
model.pb
model_11.cptk.data-00000-of-00001
model_11.cptk.index
model_11.cptk.meta
This can take about 10 minutes depending on the size of your quantization data set and the size of your model.
1. **Run test images on device**. You can use `test_snpe.py` to test your quantized model on a Qualcomm 888 dev board.
You can find the dev board id using `adb devices`:
```
python test_snpe.py --device e6dc0375 --images ./data/test --model model.onnx --dlc ./snpe_models/model.quant.dlc
```
1. **Copy your onnx model**. In your experiment folder create a folder named `model` and copy your
trained ONNX model into this folder. You should have something like:
6. **Performance benchmark SNPE model**.
```
model.onnx
python test_snpe.py --device e6dc0375 --benchmark --images ./data/test --model model.onnx --dlc ./snpe_models/model.quant.dlc
```
1. **Setup your snpe environment**. For onnx toolset use the following:
```
pushd ~/snpe/snpe-1.64.0.3605
source bin/envsetup.sh -o ~/anaconda3/envs/snap/lib/python3.6/site-packages/onnx
```
For tensorflow use:
```
pushd ~/snpe/snpe-1.64.0.3605
source bin/envsetup.sh -ot ~/anaconda3/envs/snap/lib/python3.6/site-packages/tensorflow
```
1. **Convert and quantize model**. Inside your experiment folder, run `bash convert_tf.sh` or `bash
convert_onnx.sh modelname` which uses your SNPE SDK install to convert the tensorflow model in the
model folder to a Qualcomm .dlc file, and then runs the SNPE quantization tool on that using the
`quant` dataset to produce a quantized version of that model, so the output is
`snpe_models/model.dlc` and `snpe_models/model.quant.dlc` and it is the quantized model we will run
on the dev board using the DSP processor.
1. **Run test images on device**. Inside your experiment folder, run `python run.py --help` and you
will see the args you need to pass in order to upload the test images to the device, upload the
model, then run the test on the DSP processor, then download the results. For example:
```
python run.py --images --model model.quant.dlc --run --download
```
6. **Profile SNPE model**.
Update `benchmark/config.json` so it has the right paths and
run `cd benchmark && bash run_benchmark.sh && cd ..`
The above command line will generate a csv file with per layer profiling result.
See [Performance Analysis Using Benchmarking Tools](https://developer.qualcomm.com/software/qualcomm-neural-processing-sdk/learning-resources/vision-based-ai-use-cases/performance-analysis-using-benchmarking-tools)

Просмотреть файл

@ -183,7 +183,7 @@ def convert_model(model, model_dir):
basename, ext = os.path.splitext(filename)
if ext != ".onnx":
print(f"convert_model was not provided with a valid .onnx model")
print("convert_model was not provided with a valid .onnx model")
sys.exit(1)
try:
@ -205,13 +205,6 @@ def quantize_model(model, onnx_model, snpe_model_dir):
from olive.model import SNPEModel
from olive.passes import SNPEQuantization
"""
quant_set = os.path.join('data', 'quant', 'input_list.txt')
if not os.path.isfile(quant_set):
print(f"Quantize dataset {quant_set} is missing")
sys.exit(1)
"""
snpe_model_dir = os.path.realpath(snpe_model_dir) # Olive requires the full path
basename = os.path.splitext(os.path.basename(model))[0]
output_model = os.path.join(snpe_model_dir, f"{basename}.quant.dlc")
@ -538,11 +531,11 @@ def run_benchmark(onnx_model, dlc_model, images_dir, duration, workspace_dir):
# This mirrors what the snpe_bench.py script is doing.
options = SNPESessionOptions(
android_target = get_device(),
device = "dsp" if 'quant' in basename else "cpu",
extra_args = "--perf_profile high_performance --profiling_level basic",
workspace = workspace_dir,
accumulate_outputs = True
android_target=get_device(),
device="dsp" if 'quant' in basename else "cpu",
extra_args="--perf_profile high_performance --profiling_level basic",
workspace=workspace_dir,
accumulate_outputs=True
)
config = create_snpe_config(onnx_model, snpe_model_dir)
@ -561,17 +554,17 @@ def run_benchmark(onnx_model, dlc_model, images_dir, duration, workspace_dir):
output_folder = results['output_dir']
latencies += [results['latencies']]
return output_folder, latencies
return (output_folder, latencies)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run a model on the QUALCOMM DSP using adb and SNPE SDK to quantize ' +
'the model')
parser.add_argument('--device', '-d', help='The Android device id (as returned by adb devices)', default=None)
parser.add_argument('--images', '-i', help='Location of local test image dataset (created with create_data.py)',
default=TEST_IMAGES)
parser.add_argument('--images', '-i', help='Location of local test image dataset (created with create_data.py)')
parser.add_argument('--model', '-m', help='The path to the ONNX model to test')
parser.add_argument('--dlc', help='The specific .dlc model to test, if not provided it will be converted from --model and stored in an snpe_models folder')
parser.add_argument('--quantize', '-q', help='Quantize the given onnx or dlc model', action="store_true")
parser.add_argument('--benchmark', '-b', help='Run a benchmark on the given model', action="store_true")
parser.add_argument('--throughput', '-t', help='Run performance test of the given model', action="store_true")
parser.add_argument('--duration', type=int, help='Duration of throughput and benchmark tests (default 10 seconds)', default=10)
@ -579,10 +572,15 @@ if __name__ == '__main__':
args = parser.parse_args()
VERBOSE = args.verbose
set_device(args.device)
if args.device:
set_device(args.device)
model = args.model
MODEL_DIR = "snpe_models"
if not args.model:
print("Please provide an onnx model as input")
sys.exit(1)
ndk = os.getenv("ANDROID_NDK_ROOT")
if not ndk:
print("you must have a ANDROID_NDK_ROOT installed, see the readme.md")
@ -609,6 +607,12 @@ if __name__ == '__main__':
config = create_snpe_config(args.model, '.')
shape = config['io_config']['output_shapes'][0]
if args.quantize:
quantized_model, error = quantize_model(model, model, MODEL_DIR)
if error is not None:
print(error)
sys.exit(1)
if args.throughput:
run_throughput(model, args.duration)
sys.exit(0)
@ -629,5 +633,5 @@ if __name__ == '__main__':
print(f"batch completed in {end-start} seconds, results in {output_folder}")
for m in latencies:
print(f"total_inference_time={m['total_inference_time']}")
img_shape = tuple(shape)[0:2] # e.g. (256,256)
compute_results(img_shape, output_folder)
input_shape = (1, shape[0], shape[1], 19)
compute_results(input_shape, output_folder)