ONE - On-device Neural Engine
Loading...
Searching...
No Matches
infer_testcases.py
Go to the documentation of this file.
1from __future__ import print_function
2import sys
3import glob
4import subprocess
5import res2bin
6import datetime
7
8# This script uses nnkit to run inference for given model on a given data
9# Messages are printed to stderr
10# Usage:
11# -b - specifies path to nnkit build folder, inside which tools/run is located
12# -f - specifies framework ('tfl' for tflite or 'caf' for caffe) that the model belogs to
13# -t - specifies path to testcase folder (see it's structure in readme)
14# -p - allow some sort of parallelism by processing only a subset of files,
15# you need to specify number of processes and run as much of them
16# manually with diferent numbers
17# -r - infer all testcases regardless of whether the result files are present
18# last argument(s) is the model to infer
19#
20# Example of usage:
21# python3 infer_testcases.py -f tfl -b /mnt/nncc_ci/nncc_new/build/contrib/nnkit -t /mnt/nncc_ci/images/inc_slim/testcases/ -p 10 1 -r /mnt/nncc_ci/images/inc_slim/models/inception_v3_2018.tflite
22#
23
24helpstr = "Expected arguments: -b <path_to_nnkit>" + \
25 "-f (tfl | caf) " + \
26 "-t <testcases_dir> " + \
27 "[-p <nporc> <proc_num>] " + \
28 "[-r] " + \
29 "(<tflite_model_file> | <caffe_prototxt_model> <caffe_caffemodel_file>)"
30
31
32def eprint(*args, **kwargs):
33 print(*args, file=sys.stderr, **kwargs)
34
35
36nproc = 1
37proc_num = 1
38min_argc = 8
39
40args = {}
41args['-p'] = (1, 1)
42args['-r'] = False
43
44argc = len(sys.argv)
45for i in range(argc):
46 arg = sys.argv[i]
47 if arg == '-r':
48 args[arg] = True
49 elif arg == '-b' or arg == '-f' or arg == '-t':
50 if i + 1 >= argc:
51 eprint(arg, " is missing it's value")
52 eprint(helpstr)
53 exit()
54 args[arg] = sys.argv[i + 1]
55 elif arg == '-p':
56 min_argc += 3
57 if i + 2 >= argc:
58 eprint(arg, " is missing some of it's values")
59 eprint(helpstr)
60 exit()
61 args[arg] = (int(sys.argv[i + 1]), int(sys.argv[i + 2]))
62 elif arg[0] == '-':
63 print('Unsupported argument: ', arg)
64 exit()
65
66if not ('-b' in args and '-f' in args and '-t' in args):
67 eprint('Some arguments are not provided')
68 eprint(helpstr)
69 exit()
70
71fw = args['-f']
72build_path = args['-b']
73testcases_dir = args['-t']
74nproc, proc_num = args['-p']
75remove_existing = args['-r']
76
77if fw == 'tfl':
78 model = sys.argv[-1]
79 print('Model: ', model)
80elif fw == 'caf':
81 model_proto = sys.argv[-2]
82 model_caffe = sys.argv[-1]
83 print('Models: ', model_proto, model_caffe)
84else:
85 eprint('Unsupported framework:', fw)
86 exit()
87
88eprint('started at', datetime.datetime.now())
89print('Framework: ', fw)
90print('Path to nnkit: ', build_path)
91print('Testcases folder: ', testcases_dir)
92
93hdf_suffix = '.hdf5'
94bin_suffix = '.dat'
95
96
97def get_command_caf(infilename, outfilename, proto, caffemodel):
98 return [
99 build_path + "/tools/run/nnkit-run", "--pre",
100 build_path + "/actions/HDF5/libnnkit_HDF5_import_action.so", "--pre-arg",
101 infilename, "--backend", build_path + "/backends/caffe/libnnkit_caffe_backend.so",
102 "--backend-arg", proto, "--backend-arg", caffemodel, "--post",
103 build_path + "/actions/HDF5/libnnkit_HDF5_export_action.so", "--post-arg",
104 outfilename
105 ]
106
107
108def get_command_tfl(infilename, outfilename, model_file):
109 return [
110 build_path + "/tools/run/nnkit-run", "--pre",
111 build_path + "/actions/HDF5/libnnkit_HDF5_import_action.so", "--pre-arg",
112 infilename, "--backend",
113 build_path + "/backends/tflite/libnnkit_tflite_backend.so", "--backend-arg",
114 model_file, "--post", build_path + "/actions/builtin/libnnkit_show_action.so",
115 "--post", build_path + "/actions/HDF5/libnnkit_HDF5_export_action.so",
116 "--post-arg", outfilename
117 ]
118
119
120testcase_num = 0
121testcases = glob.glob(testcases_dir + '/testcase*')
122
123#testcases = [t
124# for t in testcases
125# if remove_existing
126# or len(glob.glob(t + '/output/output' + hdf_suffix)) == 0
127# or len(glob.glob(t + '/output/output' + bin_suffix)) == 0]
128testcases = testcases[proc_num - 1::nproc]
129
130testcases.sort()
131for testcase in testcases:
132 testcase_num += 1
133 try:
134 infile = glob.glob(testcase + '/input/*' + hdf_suffix)
135 if len(infile) > 0:
136 infile = infile[0]
137 outfile = testcase + '/output/output' + hdf_suffix
138 outfile_bin = testcase + '/output/output' + bin_suffix
139 if len(glob.glob(outfile)) == 0 or remove_existing:
140 if fw == 'tfl':
141 command = get_command_tfl(infile, outfile, model)
142 elif fw == 'caf':
143 command = get_command_caf(infile, outfile, model_proto, model_caffe)
144 #subprocess.call(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
145 subprocess.call(command)
146 if len(glob.glob(outfile_bin)) == 0 or remove_existing:
147 res2bin.res2bin(outfile, outfile_bin)
148 eprint(testcase_num, "/", len(testcases))
149 else:
150 eprint(testcase, ': input not found')
151 except:
152 eprint(testcase, 'failed')
153
154eprint('ended at', datetime.datetime.now())
get_command_tfl(infilename, outfilename, model_file)
eprint(*args, **kwargs)
get_command_caf(infilename, outfilename, proto, caffemodel)
res2bin(infilename, outfilename)
Definition res2bin.py:11