Esempio n. 1
0
def moc_emit_ir(ngraph_function: Model, argv: argparse.Namespace):
    output_dir = argv.output_dir if argv.output_dir != '.' else os.getcwd()

    # Apply preprocessing (mean/scale/reverse_channels/convert_layout/etc)
    apply_preprocessing(ov_function=ngraph_function, argv=argv)

    # Apply transformations
    from openvino.tools.mo.back.offline_transformations import apply_user_transformations, apply_moc_transformations
    apply_user_transformations(ngraph_function, parse_transform(argv.transform))
    apply_moc_transformations(ngraph_function)

    if argv.compress_fp16:
        from openvino.tools.mo.back.offline_transformations import compress_model
        compress_model(ngraph_function)

    orig_model_name = os.path.normpath(os.path.join(output_dir, argv.model_name))

    from openvino.offline_transformations_pybind import serialize # pylint: disable=import-error,no-name-in-module
    serialize(ngraph_function, (orig_model_name + ".xml").encode('utf-8'), (orig_model_name + ".bin").encode('utf-8'))

    del argv.feManager

    # add meta information to IR
    append_ir_info(file=orig_model_name,
                   meta_info=get_meta_info(argv),
                   mean_data=None,
                   input_names=None)

    print('[ SUCCESS ] Generated IR version {} model.'.format(get_ir_version(argv)))
    print('[ SUCCESS ] XML file: {}.xml'.format(orig_model_name))
    print('[ SUCCESS ] BIN file: {}.bin'.format(orig_model_name))
    return 0
def apply_offline_transformations(input_model: str,
                                  framework: str,
                                  transforms: list,
                                  compress_fp16=False):
    # This variable is only needed by GenerateMappingFile transformation
    # to produce correct mapping
    extract_names = framework in ['tf', 'mxnet', 'kaldi']

    from openvino.offline_transformations_pybind import generate_mapping_file, serialize  # pylint: disable=import-error,no-name-in-module
    from openvino.frontend import FrontEndManager, FrontEnd  # pylint: disable=no-name-in-module,import-error

    fem = FrontEndManager()

    # We have to separate fe object lifetime from fem to
    # avoid segfault during object destruction. So fe must
    # be destructed before fem object explicitly.
    def read_model(path_to_xml):
        fe = fem.load_by_framework(framework="ir")
        function = fe.convert(fe.load(path_to_xml))
        return function

    func = read_model(input_model + "_tmp.xml")

    apply_user_transformations(func, transforms)
    apply_moc_transformations(func)

    if compress_fp16:
        compress_model(func)

    serialize(func,
              str(input_model + ".xml").encode('utf-8'),
              (input_model + ".bin").encode('utf-8'))
    path_to_mapping = input_model + ".mapping"
    generate_mapping_file(func, path_to_mapping.encode('utf-8'), extract_names)
Esempio n. 3
0
def apply_offline_transformations(input_model: str, argv: argparse.Namespace):
    # This variable is only needed by GenerateMappingFile transformation
    # to produce correct mapping
    extract_names = argv.framework in ['tf', 'mxnet', 'kaldi']

    from openvino.offline_transformations_pybind import generate_mapping_file, serialize  # pylint: disable=import-error,no-name-in-module
    from openvino.frontend import FrontEndManager, FrontEnd  # pylint: disable=no-name-in-module,import-error
    from openvino.tools.mo.back.preprocessing import apply_preprocessing  # pylint: disable=no-name-in-module,import-error

    fem = FrontEndManager()

    # We have to separate fe object lifetime from fem to
    # avoid segfault during object destruction. So fe must
    # be destructed before fem object explicitly.
    def read_model(path_to_xml):
        fe = fem.load_by_framework(framework="ir")
        function = fe.convert(fe.load(path_to_xml))
        return function

    func = read_model(input_model + "_tmp.xml")

    # TODO: use ngraph preprocessing (Mean/Scale/ReverseInputChannels) for legacy frontends
    reverse_input_channels = False
    if 'reverse_input_channels' in argv:
        reverse_input_channels = argv.reverse_input_channels
        argv.reverse_input_channels = False
    mean_scale_values = {}
    if 'mean_scale_values' in argv:
        mean_scale_values = argv.mean_scale_values
        argv.mean_scale_values = {}
    scale = None
    if 'scale' in argv:
        scale = argv.scale
        argv.scale = None

    # Apply preprocessing for layouts only
    apply_preprocessing(ov_function=func, argv=argv)

    if 'reverse_input_channels' in argv:
        argv.reverse_input_channels = reverse_input_channels
    if 'mean_scale_values' in argv:
        argv.mean_scale_values = mean_scale_values
    if 'scale' in argv:
        argv.scale = scale

    apply_user_transformations(func, parse_transform(argv.transform))
    apply_moc_transformations(func)

    if "compress_fp16" in argv and argv.compress_fp16:
        compress_model(func)

    serialize(func,
              str(input_model + ".xml").encode('utf-8'),
              (input_model + ".bin").encode('utf-8'))
    path_to_mapping = input_model + ".mapping"
    generate_mapping_file(func, path_to_mapping.encode('utf-8'), extract_names)
Esempio n. 4
0
def test_serialize_pass():
    core = Core()
    xml_path = "serialized_function.xml"
    bin_path = "serialized_function.bin"

    func = get_test_function()

    serialize(func, xml_path, bin_path)

    assert func is not None

    res_func = core.read_model(model=xml_path, weights=bin_path)

    assert func.get_parameters() == res_func.get_parameters()
    assert func.get_ordered_ops() == res_func.get_ordered_ops()

    os.remove(xml_path)
    os.remove(bin_path)
Esempio n. 5
0
def test_Version_ir_v11():
    core = Core()
    xml_path = "./serialized_function.xml"
    bin_path = "./serialized_function.bin"
    shape = [100, 100, 2]
    parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")
    model = ov.opset8.floor(
        ov.opset8.minimum(ov.opset8.abs(parameter_a), parameter_b))
    func = Model(model, [parameter_a, parameter_b], "Model")

    serialize(func, xml_path, bin_path, "IR_V11")
    res_func = core.read_model(model=xml_path, weights=bin_path)

    assert func.get_parameters() == res_func.get_parameters()
    assert func.get_ordered_ops() == res_func.get_ordered_ops()

    os.remove(xml_path)
    os.remove(bin_path)
Esempio n. 6
0
def dump_exec_graph(compiled_model, model_path, weight_path=None):
    if not weight_path:
        weight_path = model_path[:model_path.find(".xml")] + ".bin"
    serialize(compiled_model.get_runtime_model(), model_path, weight_path)
Esempio n. 7
0
def dump_exec_graph(exe_network, model_path, weight_path=None):
    if not weight_path:
        weight_path = model_path[:model_path.find(".xml")] + ".bin"
    serialize(exe_network.get_runtime_model(), model_path, weight_path)