def test_validate_mn1_quantized1(mn1q_graph, mn1f_graph): tfi = TfliteImporter() Gf = tfi.create_graph(mn1f_graph, {'load_tensors': True}) Gf.add_dimensions() Gf.adjust_order() matcher = get_pow2_match_group() matcher.match(Gf) Gf.add_dimensions() tfi = TfliteImporter() G = tfi.create_graph(mn1q_graph, { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions() fpnode = Gf.graph_state.steps[2]['node'] fpcnode = fpnode.contained_filters()[0] qpnode = G.graph_state.steps[2]['node'] qpcnode = qpnode.contained_filters()[0] nid = NodeId(qpnode, qpcnode) qrec = G.quantization[nid] dqbiases = qrec.biases_q.get_dequantized(qpcnode.biases) assert np.max(np.abs(fpcnode.biases - dqbiases)) < 0.1 input_tensor = np.load('tests/mobv1_valid/COCO_val2014_000000362331_0.npy') input_tensor = input_tensor.reshape((224, 224, 3)).transpose((2, 0, 1)) executer = GraphExecuter(Gf) foutput_tensors = executer.execute([input_tensor]) foutput_tensor = np.load( 'tests/mobv1_valid/output_COCO_val2014_000000362331_0_float.npy') assert np.max(np.abs(foutput_tensors[-1][0] - foutput_tensor[0])) < 0.0001 executer = GraphExecuter(G, qrecs=G.quantization) qfroutput_tensors = executer.execute([input_tensor], qmode=QuantizationMode.none()) assert np.max(np.abs(qfroutput_tensors[-1][0] - foutput_tensor[0])) < 0.2 executer = GraphExecuter(G, qrecs=G.quantization) qroutput_tensors = executer.execute( [input_tensor], qmode=QuantizationMode.all_dequantize()) output_tensor = np.load( 'tests/mobv1_valid/output_COCO_val2014_000000362331_0_quant.npy') # assert np.max(np.abs(qroutput_tensors[-1][0] - output_tensor[0])) < 0.16 assert np.max(np.abs(qroutput_tensors[-1][0] - output_tensor[0])) < 0.28
def test_fusions1(mnist_graph): tfi = TfliteImporter() G = tfi.create_graph(mnist_graph, {}) G.add_dimensions() matcher = MatchAllGapConv() matcher.match(G) G.add_dimensions()
def test_fusions4(ssd_graph): tfi = TfliteImporter() G = tfi.create_graph(ssd_graph, {}) G.add_dimensions() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions()
def test_liveness6(vww_graph): tfi = TfliteImporter() G = tfi.create_graph(vww_graph, {}) assert G steps = add_dimensions(G) liveness = calculate_liveness(G, steps) assert len(liveness) == 121 # no record for 1 output
def test_load9(mn1q_graph): tfi = TfliteImporter() G = tfi.create_graph(mn1q_graph, { 'load_tensors': True, 'load_quantization': True }) assert G
def test_adjust8(qvww_graph): tfi = TfliteImporter() G = tfi.create_graph(qvww_graph, {'load_tensors': True}) G.add_dimensions() G.adjust_order() matcher = get_fusion("fuse_external_bias") matcher.match(G) G.add_dimensions()
def test_adjust_new(): tfi = TfliteImporter() G = tfi.create_graph("tests/graph/ocr_cnn_notile_fquant.tflite", { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order()
def test_mn3_match1(mn3_graph, caplog): tfi = TfliteImporter() G = tfi.create_graph(mn3_graph, {'load_tensors': True}) G.add_dimensions() G.adjust_order() fac = MatScalePairMatchFactory() caplog.set_level(logging.DEBUG) res = fac.get_matcher().match_graph(G) assert len(res) == 8
def test_mn3_fusion1(mn3_graph, caplog): tfi = TfliteImporter() G = tfi.create_graph(mn3_graph, {'load_tensors': True}) G.add_dimensions() G.adjust_order() match = FuseMatScalePair() caplog.set_level(logging.DEBUG) match.match(G) G.verify_edges()
def test_adjust11(): tfi = TfliteImporter() G = tfi.create_graph("tests/graph/imu.tflite", {'load_tensors': True}) G.add_dimensions() G.adjust_order() assert all([ not (node.transpose_in or node.transpose_out) for node in G.nodes() if isinstance(node, Transposable) ]), "shouldn't have transposes"
def test_load8(mn2_graph): tfi = TfliteImporter() G = tfi.create_graph(mn2_graph, { 'load_tensors': True, 'load_quantization': True }) for node in G.nodes(): assert NodeId( node) in G.quantization, "node %s doesn't have a qrec" % ( node.name) assert G
def test_adjust_new2(): tfi = TfliteImporter() G = tfi.create_graph( "tests/graph/ssdlite_v2_quant_ocr_nopostprocess.tflite", { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G['output_1'].fixed_order = True G['output_2'].fixed_order = True G.adjust_order()
def test_mn3_match2(mn3_graph, caplog): tfi = TfliteImporter() G = tfi.create_graph(mn3_graph, {'load_tensors': True}) G.add_dimensions() G.adjust_order() caplog.set_level(logging.DEBUG) fragment = GraphMatcher( match_function=lambda state, frag: (frag, state['match'])) fragment.add_node(MatScaleNodeMatch()) res = fragment.match_graph(G) assert len(res) == 16
def test_validate_mn1_quantized2(mn1q_graph): tfi = TfliteImporter() G = tfi.create_graph(mn1q_graph, { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions()
def test_mobv2_quant_asym_tf1_15_vwwvehicle(): graph = 'tests/mobv2_valid/mobv2_vwwvehicle_quant_asym.tflite' tfi = TfliteImporter() G = tfi.create_graph(graph, { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_scale8_match_group() matcher.match(G) G.add_dimensions()
def test_adjust9(mn3q_graph, caplog): caplog.set_level(logging.INFO) tfi = TfliteImporter() G = tfi.create_graph(mn3q_graph, { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_scale8_match_group() matcher.match(G) G.add_dimensions()
def test_adjust10(caplog): caplog.set_level(logging.INFO) tfi = TfliteImporter() G = tfi.create_graph( "tests/graph/ssdlite_v2_quant_ocr_nopostprocess.tflite", { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_scale8_match_group() matcher.match(G) G.add_dimensions()
def test_validate_mn1_dequant_quantfloat(mn1q_graph): # load dequantized graph same results as quant graph and float execution tfi = TfliteImporter() G = tfi.create_graph(mn1q_graph, { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions() Gdq = tfi.create_graph(mn1q_graph, { 'load_tensors': True, 'load_dequantized': True }) Gdq.add_dimensions() Gdq.adjust_order() matcher = get_pow2_match_group() matcher.match(Gdq) Gdq.add_dimensions() input_tensor = np.load('tests/mobv1_valid/COCO_val2014_000000362331_0.npy') input_tensor = input_tensor.reshape((224, 224, 3)).transpose((2, 0, 1)) executer = GraphExecuter(G, qrecs=G.quantization) qfoutput_tensors = executer.execute([input_tensor], qmode=QuantizationMode.none()) executer = GraphExecuter(Gdq) dfoutput_tensors = executer.execute([input_tensor]) diff_list = [ np.abs(df[0] - qf[0]) for df, qf in zip(dfoutput_tensors, qfoutput_tensors) ] max_diff = [np.max(elem) for elem in diff_list] assert max(max_diff) < 0.003
def test_adjust6(): tfi = TfliteImporter() try: G = tfi.create_graph( "tests/graph/character_recogniction_cnn_ocr.tflite", {'load_tensors': True}) # This graph has an insance concat which multiplies the output of a linear # layer. It will never be supported. G.add_dimensions() error = False G.adjust_order() except NotImplementedError: error = True assert error
def test_validate_mn1_float(mn1f_graph): tfi = TfliteImporter() G = tfi.create_graph(mn1f_graph, {'load_tensors': True}) G.add_dimensions() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions() input_tensor = np.load('tests/mobv1_valid/COCO_val2014_000000362331_0.npy') input_tensor = input_tensor.reshape((224, 224, 3)) executer = GraphExecuter(G, qrecs=G.quantization) routput_tensors = executer.execute([input_tensor]) output_tensor = np.load( 'tests/mobv1_valid/output_COCO_val2014_000000362331_0_float.npy') assert np.max(np.abs(routput_tensors[-1][0] - output_tensor[0])) < 0.0001
def test_adjust7(concat_test_graph): tfi = TfliteImporter() G = tfi.create_graph(concat_test_graph, {'load_tensors': True}) G.node('input_1').fixed_order = True G.node('output_1').fixed_order = True G.node('output_2').fixed_order = True G.add_dimensions() G.adjust_order() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions() report = GraphReporter().report(G, None) renderer = TextTableRenderer(maxwidth=200) print(report.render(renderer)) report = GraphReporter(split_dims=True).report(G, None)
def test_external_biases_sq8(qvww_graph): # this model has at the end an external biases layer as constant add tfi = TfliteImporter() G = tfi.create_graph(qvww_graph, {"load_quantization": True, "load_tensors": True}) G.add_dimensions() matcher = get_scale8_match_group() matcher.match(G) G.add_dimensions() image = 'tests/vwwimages/COCO_val2014_000000174838_1.png' img_in = Image.open(image) img_in = img_in.resize((238, 208)) input_tensor = np.array(img_in, dtype=np.uint8) input_tensor = (input_tensor.astype(np.float32) - 128) / 128 executer = GraphExecuter(G, qrecs=G.quantization) # check if nntool can execute qoutput_tensors = executer.execute([input_tensor], qmode=QuantizationMode.all_dequantize()) foutput_tensors = executer.execute([input_tensor], qmode=None) diff = [q[0]-f[0] for q,f in zip(qoutput_tensors, foutput_tensors)] assert max([np.max(d) for d in diff]) < 2.2
def test_gen_vergesense(caplog): caplog.set_level(logging.DEBUG) tfi = TfliteImporter() G = tfi.create_graph("tests/graph/marco_17_04.tflite", { 'load_tensors': True, 'load_quantization': True }) G.add_dimensions() G.adjust_order() matcher = get_scale8_match_group() matcher.match(G) G.add_dimensions() with tempfile.TemporaryDirectory() as tempdir: opts = { 'default_input_location': 'ARG_LOC_L2', 'default_output_location': 'ARG_LOC_L2', 'default_global_location': 'ARG_LOC_L3_HFLASH', 'default_local_location': 'AT_MEM_UNDEF', 'tensor_directory': tempdir } code_gen = CodeGenerator(G, DefaultNamingConvension(G), opts) default_template(G, code_generator=code_gen) code_gen.write_constants()
def test_adjust3(ssd_graph): tfi = TfliteImporter() G = tfi.create_graph(ssd_graph, {'load_tensors': True}) G.add_dimensions() G.adjust_order()
def test_load3(ssd_graph): tfi = TfliteImporter() G = tfi.create_graph(ssd_graph, {}) assert G
def test_load1(mnist_graph): tfi = TfliteImporter() G = tfi.create_graph(mnist_graph, {}) assert G
def test_liveness2(ir_graph): tfi = TfliteImporter() G = tfi.create_graph(ir_graph, {}) steps = add_dimensions(G) liveness = calculate_liveness(G, steps) assert len(liveness) == 23 # no record for 8 outputs
def test_load4(cifar10_graph): tfi = TfliteImporter() G = tfi.create_graph(cifar10_graph, {}) assert G
def test_load5(kws_graph): tfi = TfliteImporter() G = tfi.create_graph(kws_graph, {}) assert G
def test_load6(vww_graph): tfi = TfliteImporter() G = tfi.create_graph(vww_graph, {}) assert G