11import os ,sys ,time
2- from subprocess import *
3- import datetime , time
4- import threading
2+ import datetime
3+ import subprocess
4+
55
66### This script do auto testing before release code.
77test_list = ["mnist_f" , "mnist_q" , "mbnet_f" , "mbnet_q" ]
88
99
10- def runcmd (cmd ):
11- r = Popen (cmd ,stdin = PIPE ,stdout = PIPE ,stderr = PIPE , shell = True )
10+ def runcmd (cmd , cwd = None ):
11+
12+ stdout = subprocess .check_output (cmd , shell = True , cwd = cwd )
13+ # stdin=PIPE,stdout=PIPE,stderr=PIPE
1214 a = []
13- for line in r . stdout .readlines ( ):
14- a .append (line .decode ( "utf8" ). strip ())
15+ for line in stdout .decode ( "utf8" ). strip (). split ( ' \n ' ):
16+ a .append (line .strip ())
1517 return a
1618
19+ def run_h5_to_tflite (cmd , ** kwargs ):
20+ program = 'python -m tinymaix.tools.h5_to_tflite'
21+ cmd = program + ' ' + cmd
22+ return runcmd (cmd , ** kwargs )
23+
24+ def run_tflite2tmdl (cmd , ** kwargs ):
25+ program = 'python -m tinymaix.tools.tflite2tmdl'
26+ cmd = program + ' ' + cmd
27+ return runcmd (cmd , ** kwargs )
28+
29+ tools_dir = '../../tools/'
30+ mnist_dir = '../mnist'
31+ mbnet_dir = '../mbnet'
32+
1733
1834print ("This script only test INT8/FP32, you need change OPT0&OPT1" )
1935t00 = time .time ()
@@ -22,25 +38,20 @@ def runcmd(cmd):
2238 t0 = time .time ()
2339 print ("========Step1.1: test MNIST fp32" )
2440 print ("====Step1.1.1: MNIST fp32 cvt" )
25- cmd = "cd ../../tools/ && python3 h5_to_tflite.py h5/mnist_valid.h5 tflite/mnist_valid_f.tflite 0 && python3 tflite2tmdl.py tflite/mnist_valid_f.tflite tmdl/mnist_valid_f.tmdl fp32 1 28,28,1 10"
26- res = runcmd (cmd )
27- print (res [- 1 ])
28- if res [- 1 ] == "Saved to tinymaix model header to tmdl/mnist_valid_f.h" :
29- print ("====Step1.1.1: OK~" )
30- else :
31- print ("====Step1.1.1: ERR!!!" )
32- exit (- 1 )
41+ run_h5_to_tflite ("h5/mnist_valid.h5 tflite/mnist_valid_f.tflite 0" , cwd = tools_dir )
42+ res = run_tflite2tmdl ("tflite/mnist_valid_f.tflite tmdl/mnist_valid_f.tmdl fp32 1 28,28,1 10" , cwd = tools_dir )
43+ assert 'Saved to tmdl/mnist_valid_f.tmdl' in res [- 1 ], ('Step 1.1.1 ERR' , res )
44+
3345
3446 print ("====Step1.1.2: MNIST fp32 compile&run" )
35- cmd = "cd ../mnist && sed -i 's/#define TM_MDL_TYPE TM_MDL_INT8/#define TM_MDL_TYPE TM_MDL_FP32/g' ../../include/tm_port.h && rm -rf build && mkdir build && cd build && cmake .. && make && ./mnist"
36- res = runcmd (cmd )
47+ cmd = "sed -i 's/#define TM_MDL_TYPE TM_MDL_INT8/#define TM_MDL_TYPE TM_MDL_FP32/g' ../../include/tm_port.h"
48+ res = runcmd (cmd , cwd = mnist_dir )
49+ res = runcmd ("rm -rf build && mkdir build && cd build && cmake .. && make && ./mnist" , cwd = mnist_dir )
3750 print (res [- 1 ])
38- if res [- 1 ] == "### Predict output is: Number 2, prob 1.000" :
39- print ("====Step1.1.2: OK~" )
40- runcmd ("rm ../../tools/tmdl/mnist_valid_f.tmdl" ) #clean tmdl
41- else :
42- print ("====Step1.1.2: ERR!!!" )
43- exit (- 2 )
51+ assert 'Predict output is: Number 2, prob 1.0' in res [- 1 ], res [- 1 ]
52+
53+ runcmd ("rm ../../tools/tmdl/mnist_valid_f.tmdl" ) #clean tmdl
54+
4455 t1 = time .time ()
4556 print ("========Step1.1: test MNIST fp32 OK~ use %.1fs" % (t1 - t0 ))
4657
@@ -49,26 +60,17 @@ def runcmd(cmd):
4960 t0 = time .time ()
5061 print ("========Step1.2: test MNIST int8" )
5162 print ("====Step1.2.1: MNIST int8 cvt" )
52- cmd = "cd ../../tools/ && python3 h5_to_tflite.py h5/mnist_valid.h5 tflite/mnist_valid_q.tflite 1 quant_img_mnist/ 0to1 && python3 tflite2tmdl.py tflite/mnist_valid_q.tflite tmdl/mnist_valid_q.tmdl int8 1 28,28,1 10"
53- res = runcmd (cmd )
54- print (res [- 1 ])
55- if res [- 1 ] == "Saved to tinymaix model header to tmdl/mnist_valid_q.h" :
56- print ("====Step1.2.1: OK~" )
57- else :
58- print ("====Step1.2.1: ERR!!!" )
59- exit (- 1 )
63+ res = run_h5_to_tflite ("h5/mnist_valid.h5 tflite/mnist_valid_q.tflite 1 quant_img_mnist/ 0to1" , cwd = tools_dir )
64+ res = run_tflite2tmdl ("tflite/mnist_valid_q.tflite tmdl/mnist_valid_q.tmdl int8 1 28,28,1 10" , cwd = tools_dir )
6065
6166 print ("====Step1.2.2: MNIST int8 compile&run" )
62-
63- cmd = "cd ../mnist && sed -i 's/#define TM_MDL_TYPE TM_MDL_FP32/#define TM_MDL_TYPE TM_MDL_INT8/g' ../../include/tm_port.h && rm -rf build && mkdir build && cd build && cmake .. && make && ./mnist"
64- res = runcmd (cmd )
67+ cmd = "sed -i 's/#define TM_MDL_TYPE TM_MDL_FP32/#define TM_MDL_TYPE TM_MDL_INT8/g' ../../include/tm_port.h"
68+ res = runcmd ( cmd , cwd = mnist_dir )
69+ res = runcmd ("rm -rf build && mkdir build && cd build && cmake .. && make && ./mnist" , cwd = mnist_dir )
6570 print (res [- 1 ])
66- if res [- 1 ] == "### Predict output is: Number 2, prob 0.996" :
67- print ("====Step1.2.2: OK~" )
68- runcmd ("rm ../../tools/tmdl/mnist_valid_q.tmdl" ) #clean tmdl
69- else :
70- print ("====Step1.2.2: ERR!!!" )
71- exit (- 2 )
71+ assert 'Predict output is: Number 2, prob 0.996' in res [- 1 ], res [- 1 ]
72+
73+ runcmd ("rm ../../tools/tmdl/mnist_valid_q.tmdl" ) #clean tmdl
7274
7375 t1 = time .time ()
7476 print ("========Step1.2: test MNIST int8 OK~ use %.1fs" % (t1 - t0 ))
@@ -79,27 +81,17 @@ def runcmd(cmd):
7981 t0 = time .time ()
8082 print ("========Step2.1: test MBNET fp32" )
8183 print ("====Step2.1.1: MBNET fp32 cvt" )
82- cmd = "cd ../../tools/ && python3 h5_to_tflite.py h5/mbnet128_0.25.h5 tflite/mbnet128_0.25_f.tflite 0 && python3 tflite2tmdl.py tflite/mbnet128_0.25_f.tflite tmdl/mbnet128_0.25_f.tmdl fp32 1 128,128,3 1000"
83- res = runcmd (cmd )
84- print (res [- 1 ])
85- if res [- 1 ] == "Saved to tinymaix model header to tmdl/mbnet128_0.25_f.h" :
86- print ("====Step2.1.1: OK~" )
87- else :
88- print ("====Step2.1.1: ERR!!!" )
89- exit (- 1 )
84+ res = run_h5_to_tflite ("h5/mbnet128_0.25.h5 tflite/mbnet128_0.25_f.tflite 0" , cwd = tools_dir )
85+ res = run_tflite2tmdl ("tflite/mbnet128_0.25_f.tflite tmdl/mbnet128_0.25_f.tmdl fp32 1 128,128,3 1000" , cwd = tools_dir )
9086
9187 print ("====Step2.1.2: MBNET fp32 compile&run" )
92-
93- cmd = "cd ../mbnet && sed -i 's/#define TM_MDL_TYPE TM_MDL_INT8/#define TM_MDL_TYPE TM_MDL_FP32/g' ../../include/tm_port.h && rm -rf build && mkdir build && cd build && cmake .. && make && ./mbnet"
94- res = runcmd (cmd )
88+ cmd = "sed -i 's/#define TM_MDL_TYPE TM_MDL_INT8/#define TM_MDL_TYPE TM_MDL_FP32/g' ../../include/tm_port.h"
89+ res = runcmd ( cmd , cwd = mbnet_dir )
90+ res = runcmd ("rm -rf build && mkdir build && cd build && cmake .. && make && ./mbnet" , cwd = mbnet_dir )
9591 print (res [- 1 ])
96- if res [- 1 ] == "### Predict output is: Class 292 (tiger, Panthera tigris), Prob 0.866" or \
97- res [- 1 ] == "### Predict output is: Class 292 (tiger, Panthera tigris), Prob 0.891" :
98- print ("====Step2.1.2: OK~" )
99- runcmd ("rm ../../tools/tmdl/mbnet128_0.25_f.tmdl" ) #clean tmdl
100- else :
101- print ("====Step2.1.2: ERR!!!" )
102- exit (- 2 )
92+ assert 'Class 292 (tiger, Panthera tigris), Prob 0.8' in res [- 1 ], res [- 1 ]
93+
94+ runcmd ("rm ../../tools/tmdl/mbnet128_0.25_f.tmdl" ) #clean tmdl
10395
10496 t1 = time .time ()
10597 print ("========Step2.1: test MBNET fp32 OK~ use %.1fs" % (t1 - t0 ))
@@ -109,26 +101,17 @@ def runcmd(cmd):
109101 t0 = time .time ()
110102 print ("========Step2.2: test MBNET int8" )
111103 print ("====Step2.2.1: MBNET int8 cvt" )
112- cmd = "cd ../../tools/ && python3 h5_to_tflite.py h5/mbnet128_0.25.h5 tflite/mbnet128_0.25_q.tflite 1 quant_img128/ 0to1 && python3 tflite2tmdl.py tflite/mbnet128_0.25_q.tflite tmdl/mbnet128_0.25_q.tmdl int8 1 128,128,3 1000"
113- res = runcmd (cmd )
114- print (res [- 1 ])
115- if res [- 1 ] == "Saved to tinymaix model header to tmdl/mbnet128_0.25_q.h" :
116- print ("====Step2.2.1: OK~" )
117- else :
118- print ("====Step2.2.1: ERR!!!" )
119- exit (- 1 )
104+ res = run_h5_to_tflite ("h5/mbnet128_0.25.h5 tflite/mbnet128_0.25_q.tflite 1 quant_img128/ 0to1" , cwd = tools_dir )
105+ res = run_tflite2tmdl ("tflite/mbnet128_0.25_q.tflite tmdl/mbnet128_0.25_q.tmdl int8 1 128,128,3 1000" , cwd = tools_dir )
120106
121107 print ("====Step2.2.2: MBNET int8 compile&run" )
122-
123- cmd = "cd ../mbnet && sed -i 's/#define TM_MDL_TYPE TM_MDL_FP32/#define TM_MDL_TYPE TM_MDL_INT8/g' ../../include/tm_port.h && rm -rf build && mkdir build && cd build && cmake .. && make && ./mbnet"
124- res = runcmd (cmd )
108+ cmd = "sed -i 's/#define TM_MDL_TYPE TM_MDL_FP32/#define TM_MDL_TYPE TM_MDL_INT8/g' ../../include/tm_port.h"
109+ res = runcmd ( cmd , cwd = mbnet_dir )
110+ res = runcmd ("rm -rf build && mkdir build && cd build && cmake .. && make && ./mbnet" , cwd = mbnet_dir )
125111 print (res [- 1 ])
126- if res [- 1 ] == "### Predict output is: Class 292 (tiger, Panthera tigris), Prob 0.824" :
127- print ("====Step2.2.2: OK~" )
128- runcmd ("rm ../../tools/tmdl/mbnet128_0.25_q.tmdl" ) #clean tmdl
129- else :
130- print ("====Step2.2.2: ERR!!!" )
131- exit (- 2 )
112+ assert 'Class 292 (tiger, Panthera tigris), Prob 0.8' in res [- 1 ]
113+
114+ runcmd ("rm ../../tools/tmdl/mbnet128_0.25_q.tmdl" ) #clean tmdl
132115
133116 t1 = time .time ()
134117 print ("========Step2.2: test MBNET int8 OK~ use %.1fs" % (t1 - t0 ))
0 commit comments