コード例 #1
0
ファイル: fusions.py プロジェクト: dilawar/gap_sdk
    def do_fusions(self, args):
        """
Carry out the default set of fusions on the graph"""
        self._check_graph()
        if args.list:
            self.ppaged("\n".join(
                ["%s - %s" % (name, desc) for name, desc in get_fusions()]))
            return
        if args.apply:
            fusions = [get_fusion(name) for name in args.apply]
            if not fusions:
                self.perror('fusion %s not found' % args.apply)
                return
        elif args.pow2:
            fusions = [get_pow2_match_group()]
        elif args.scale8:
            fusions = [get_scale8_match_group()]
        else:
            self.perror(
                "No fusion set selected. Nothing to do. Select --pow2 or --scale8."
            )
            return
        for fusion in fusions:
            fusion.match(self.G)
        self.G.add_dimensions()
        if self.G.quantization and not self.G.quantization.verify_quantization(
                self.G):
            self.G.quantization = None
コード例 #2
0
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()
コード例 #3
0
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()
コード例 #4
0
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()
コード例 #5
0
    def do_fusions(self, args):
        """
Carry out the default set of fusions on the graph"""
        if args.list:
            table = texttable.Texttable()
            table.set_cols_align(['l', 'l'])
            table.set_max_width(120)
            table.add_rows([['Name', 'Description']] + get_fusions())
            self.ppaged(table.draw())
            return
        self._check_graph()
        state = ConstantInputParameters.save_compression_state(self.G)
        try:
            if args.apply:
                fusions = [get_fusion(name) for name in args.apply]
                invalid_names = [
                    args.apply[idx] for idx, fusion in enumerate(fusions)
                    if fusion is None
                ]
                if invalid_names:
                    self.perror(
                        f'fusion{"s" if len(invalid_names) > 1 else ""} {", ".join(invalid_names)} not found'
                    )
                    return
            elif args.pow2:
                fusions = [get_pow2_match_group()]
            elif args.scale8:
                fusions = [get_scale8_match_group()]
            else:
                self.perror(
                    "No fusion set selected. Nothing to do. Select --pow2 or --scale8."
                )
                return
            for fusion in fusions:
                fusion.match(self.G)
            self.G.add_dimensions()
            if self.G.quantization and verify_quantization(self.G):
                quantizer = NewQuantizer(self.G)
                quantizer.quantize()
                problems = verify_quantization(self.G)
                if problems:
                    self.perror('quantization issue after fusions')
                    for problem in problems:
                        self.perror(problem)
        finally:
            ConstantInputParameters.restore_compression_state(self.G, state)
コード例 #6
0
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
コード例 #7
0
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()
コード例 #8
0
    def do_fusions(self, args):
        """
Carry out the default set of fusions on the graph"""
        if args.list:
            table = texttable.Texttable()
            table.set_cols_align(['l', 'l'])
            table.set_max_width(120)
            table.add_rows([['Name', 'Description']] + get_fusions())
            self.ppaged(table.draw())
            return
        self._check_graph()
        if args.apply:
            fusions = [get_fusion(name) for name in args.apply]
            invalid_names = [
                args.apply[idx] for idx, fusion in enumerate(fusions)
                if fusion is None
            ]
            if invalid_names:
                self.perror(
                    f'fusion{"s" if len(invalid_names) > 1 else ""} {", ".join(invalid_names)} not found'
                )
                return
        elif args.pow2:
            fusions = [get_pow2_match_group()]
        elif args.scale8:
            fusions = [get_scale8_match_group()]
        else:
            self.perror(
                "No fusion set selected. Nothing to do. Select --pow2 or --scale8."
            )
            return
        for fusion in fusions:
            fusion.match(self.G)
        self.G.add_dimensions()
        if self.G.quantization and not self.G.quantization.verify_quantization(
                self.G):
            self.G.quantization = None