Exemplo n.º 1
0
def run(input_path: str,
        dest_dir_path: str,
        project_name: str,
        activate_hard_quantization: bool,
        threshold_skipping: bool = False,
        num_pe: int = 16,
        use_tvm: bool = False,
        use_onnx: bool = False,
        debug: bool = False,
        cache_dma: bool = False):

    output_dlk_test_dir = path.join(dest_dir_path, f'{project_name}.test')
    optimized_pb_path = path.join(dest_dir_path, f'{project_name}')
    optimized_pb_path += '.onnx' if use_onnx else '.pb'
    output_project_path = path.join(dest_dir_path, f'{project_name}.prj')

    config = Config(num_pe=num_pe,
                    activate_hard_quantization=activate_hard_quantization,
                    threshold_skipping=threshold_skipping,
                    tvm_path=(path.join(ROOT_DIR, 'tvm') if use_tvm else None),
                    test_dir=output_dlk_test_dir,
                    optimized_pb_path=optimized_pb_path,
                    output_pj_path=output_project_path,
                    debug=debug,
                    cache_dma=cache_dma
                    )

    dest_dir_path = path.abspath(dest_dir_path)
    utils.make_dirs(dest_dir_path)

    click.echo('import pb file')
    if use_onnx:
        try:
            __import__('onnx')
        except ImportError:
            raise ImportError('ONNX is required but not installed.')
        from frontend.base import BaseIO
        from frontend.onnx import OnnxIO
        io: BaseIO = OnnxIO()

    else:
        io = TensorFlowIO()
    model: Model = io.read(input_path)

    click.echo('optimize graph step: start')
    optimize_graph_step(model, config)
    click.echo('optimize graph step: done!')

    click.echo('generate code step: start')
    generate_code_step(model, config)
    click.echo(f'generate code step: done!')
Exemplo n.º 2
0
    def test_tf_import(self) -> None:
        """Test code for importing Tensorflow file with TensorflowIO."""
        tf_path = path.join('examples', 'classification', 'lmnet_v1_toy_graph',
                            'minimal_graph_with_shape.pb')

        tf_io = TensorFlowIO()
        graph: Graph = tf_io.read(tf_path)

        outputs = graph.get_outputs()

        self.assertEqual(len(outputs), 1)
        self.assertEqual(outputs[0].shape, [1, 10])

        print("TF file import test passed!")
Exemplo n.º 3
0
    def test_import_classification(self) -> None:
        """Test code for importing Tensorflow file with TensorflowIO."""
        tf_path = path.join('examples', 'classification',
                            'lmnet_quantize_cifar10_stride_2.20180523.3x3',
                            'minimal_graph_with_shape.pb')

        tf_io = TensorFlowIO()
        graph: Graph = tf_io.read(tf_path)

        outputs = graph.get_outputs()

        self.assertEqual(len(outputs), 1)
        self.assertEqual(outputs[0].shape, [1, 10])

        print("TF file import test passed for classification!")
Exemplo n.º 4
0
    def test_import_group_convolution_classification(self) -> None:
        """Test code for importing Tensorflow file with TensorflowIO."""
        tf_path = path.join('examples', 'classification',
                            'lmnet_v1_group_conv',
                            'minimal_graph_with_shape.pb')

        tf_io = TensorFlowIO()
        model = tf_io.read(tf_path)

        graph: Graph = model.graph
        outputs = graph.get_outputs()

        self.assertEqual(len(outputs), 1)
        self.assertEqual(outputs[0].shape, [1, 10])

        print("TF file import test passed for group convolution!")
Exemplo n.º 5
0
    def test_tf_export2(self) -> None:
        """Test code for exporting Tensorflow file with TensorflowIO #2."""
        tmpdir = 'tmp'
        tf_path = path.join(tmpdir, 'test2.pb')
        if not path.exists(tmpdir):
            makedirs(tmpdir)
        elif not path.isdir(tmpdir):
            raise ValueError('tmp directory is not a directory.')

        model: Model = make_model()

        tf_io = TensorFlowIO()
        tf_io.write(model, tf_path)
        new_model = tf_io.read(tf_path)

        self.assertEqual(model.graph, new_model.graph)

        print("TF file export test #2 passed!")
Exemplo n.º 6
0
    def test_import_object_detection_lack_shape_info(self) -> None:
        """Test code for importing tf pb file of object detection
        (lack of shape info for some operator) with TensorflowIO.
        """
        tf_path = path.join(
            'examples', 'object_detection',
            'fyolo_quantize_17_v14_some_1x1_wide_pact_add_conv',
            'minimal_graph_with_shape.pb')

        tf_io = TensorFlowIO()
        graph: Graph = tf_io.read(tf_path)

        outputs = graph.get_outputs()

        self.assertEqual(len(outputs), 1)
        self.assertEqual(outputs[0].shape, [1, 10, 10, 125])

        print("TF file import test passed for object detection!")
Exemplo n.º 7
0
def run(input_path: str,
        dest_dir_path: str,
        project_name: str,
        activate_hard_quantization: bool,
        threshold_skipping: bool = False,
        num_pe: int = 16,
        debug: bool = False,
        cache_dma: bool = False):

    output_dlk_test_dir = path.join(dest_dir_path, f'{project_name}.test')
    optimized_pb_path = path.join(dest_dir_path, f'{project_name}')
    optimized_pb_path += '.pb'
    output_project_path = path.join(dest_dir_path, f'{project_name}.prj')

    config = Config(num_pe=num_pe,
                    activate_hard_quantization=activate_hard_quantization,
                    threshold_skipping=threshold_skipping,
                    test_dir=output_dlk_test_dir,
                    optimized_pb_path=optimized_pb_path,
                    output_pj_path=output_project_path,
                    debug=debug,
                    cache_dma=cache_dma)

    dest_dir_path = path.abspath(dest_dir_path)
    utils.make_dirs(dest_dir_path)

    click.echo('import pb file')
    io = TensorFlowIO()
    model: Model = io.read(input_path)

    click.echo('optimize graph step: start')
    optimize_graph_step(model, config)
    click.echo('optimize graph step: done!')

    click.echo('generate code step: start')
    generate_code_step(model, config)
    click.echo(f'generate code step: done!')