Ejemplo n.º 1
0
def convert_using_premade_objects(in_filename, out_filename, out_info_filename,
                                  reader, converter, data_format_optimizer,
                                  writer):
    source_graph = reader(in_filename)
    target_graph, conv_info = converter(source_graph)
    opt_info = data_format_optimizer(
        target_graph) if data_format_optimizer is not None else None
    ensure_dirs(os.path.dirname(out_filename))
    write_info = writer(target_graph, out_filename)
    combined_info = conversion_info.compose(conv_info, opt_info, write_info)
    ensure_dirs(os.path.dirname(out_info_filename))
    conversion_info.dump(combined_info, out_info_filename)
Ejemplo n.º 2
0
def convert_nnef_to_tf_pb(
        # Main parameters
        nnef_tgz_or_dir_path,
        output_directory,
        custom_converter_by_op_name=None,  # type: typing.Dict[str, _NNEFToTFConverterFunType]

        # Extra parameters
    with_weights=True,
        verbose=False,
        prefer_nhwc=True,
        overwrite=False,
        optimization_level=None,
        io_transform=None,

        # Module level parameters
        converter_allow_imprecise_image_resize=False,
        converter_allow_imprecise_padding_border=False,
        optimizer_remove_unneeded_copies=None,
        optimizer_remove_inverse_transposes=None,
        optimizer_merge_transforms_into_variables=None,
        optimizer_merge_transforms_into_constants=None,
        extra_parser_configs=None):
    if os.path.exists(output_directory):
        if overwrite:
            shutil.rmtree(output_directory)
        else:
            assert False, "{} exists, delete it or use overwrite=True".format(
                output_directory)

    parser_configs = [
        nnef_to_tf.ParserConfig, NNEFParserConfig.STANDARD_CONFIG
    ]
    if extra_parser_configs:
        parser_configs += extra_parser_configs
    g = nnef_io.read(nnef_tgz_or_dir_path, parser_configs=parser_configs)

    converter = nnef_to_tf.Converter(
        prefer_nhwc=prefer_nhwc,
        enable_imprecise_image_resize=converter_allow_imprecise_image_resize,
        enable_imprecise_padding_border=
        converter_allow_imprecise_padding_border,
        custom_converter_by_op_name=custom_converter_by_op_name)
    h, conv_info = converter(g)
    conversion_info.dump(conv_info, os.path.join(output_directory,
                                                 "step1.json"))

    opt_info = _call_optimizer(
        optimizer=tf_data_format_optimizer,
        graph=h,
        custom_transposable_ops=None,
        io_transform=io_transform,
        verbose=verbose,
        rename_tensors=True,
        optimization_level=optimization_level,
        remove_unneeded_copies=optimizer_remove_unneeded_copies,
        remove_inverse_transposes=optimizer_remove_inverse_transposes,
        merge_transforms_into_variables=
        optimizer_merge_transforms_into_variables,
        merge_transforms_into_constants=
        optimizer_merge_transforms_into_constants)
    conversion_info.dump(opt_info, os.path.join(output_directory,
                                                "step2.json"))

    rename_info = tf_pb_io.write_tf_graph_to_protobuf(graph=h,
                                                      filename=os.path.join(
                                                          output_directory,
                                                          'graph.pb'),
                                                      convert_from_tf_py=True)

    conversion_info.dump(rename_info,
                         os.path.join(output_directory, "step3.json"))

    conv_info = conversion_info.compose(conv_info, opt_info, rename_info)
    conversion_info.dump(conv_info,
                         os.path.join(output_directory, "conversion.json"))

    if verbose:
        print("Done.")
Ejemplo n.º 3
0
def convert_tf_pb_to_nnef(
        # Main parameters
        file_name,
        output_directory,
        network_name,
        source_shapes=None,
        source_dtypes=None,
        compress=False,

        # Extra parameters
        verbose=False,
        allow_extensions=False,
        allow_gradients=False,
        optimization_level=None,
        io_transform=None,
        activation_export_feed_dict=None,
        activation_export_io_only=False,
        overwrite=False,

        # Module level parameters
        converter_allow_imprecise_image_resize=False,
        optimizer_remove_unneeded_copies=None,
        optimizer_remove_inverse_transposes=None,
        optimizer_merge_transforms_into_variables=None,
        optimizer_merge_transforms_into_constants=None):
    # type: (...)->None

    assert utils.is_identifier(network_name), \
        "Network name must be None or a valid python identifier"

    if os.path.exists(output_directory):
        if overwrite:
            shutil.rmtree(output_directory)
        else:
            assert False, "{} exists, delete it or use overwrite=True".format(
                output_directory)

    g = tf_pb_io.read_tf_graph_from_protobuf(file_name)

    tf_pb_to_tf_py.evaluate_and_convert(tf_graph=g,
                                        source_shapes=source_shapes,
                                        source_dtypes=source_dtypes)

    converter = tf_to_nnef.Converter(
        enable_imprecise_image_resize=converter_allow_imprecise_image_resize)

    h, conv_info = converter(g)
    h.name = network_name

    conversion_info.dump(conv_info, os.path.join(output_directory,
                                                 "step1.json"))

    nnef_io.write(
        h,
        os.path.join(
            output_directory,
            network_name + ("_not_optimized.nnef.tgz"
                            if compress else "_not_optimized_nnef")))

    opt_info = _call_optimizer(
        optimizer=nnef_data_format_optimizer,
        graph=h,
        custom_transposable_ops=None,
        io_transform=io_transform,
        verbose=verbose,
        rename_tensors=True,
        optimization_level=optimization_level,
        remove_unneeded_copies=optimizer_remove_unneeded_copies,
        remove_inverse_transposes=optimizer_remove_inverse_transposes,
        merge_transforms_into_variables=
        optimizer_merge_transforms_into_variables,
        merge_transforms_into_constants=
        optimizer_merge_transforms_into_constants)

    conversion_info.dump(opt_info, os.path.join(output_directory,
                                                "step2.json"))
    conv_info = conversion_info.compose(conv_info, opt_info)
    conversion_info.dump(conv_info,
                         os.path.join(output_directory, "conversion.json"))

    nnef_io.write(
        h,
        os.path.join(output_directory,
                     network_name + (".nnef.tgz" if compress else "_nnef")))

    if activation_export_feed_dict:
        _load_graph(file_name)
        tf_activation_exporter.export(
            output_path=os.path.join(output_directory, "activations"),
            feed_dict=activation_export_feed_dict,
            conversion_info=conv_info,
            verbose=verbose,
            input_output_only=activation_export_io_only)

    if verbose:
        print("Done.")
Ejemplo n.º 4
0
    def _test(self,
              fun,
              cmp=True,
              custom_tf_to_nnef_converters="",
              custom_nnef_to_tf_converters="",
              test_module="tests.activation.tf_py_layer_test_cases"):
        out_dir = os.path.join("out", fun.__name__)
        try:
            tf.reset_default_graph()
            tf.set_random_seed(0)

            network_outputs = fun()
            feed_dict = get_feed_dict()
            checkpoint_path = os.path.join("out", fun.__name__,
                                           "orig_checkpoint",
                                           fun.__name__ + ".ckpt")
            checkpoint_path = save_random_checkpoint(network_outputs,
                                                     checkpoint_path,
                                                     feed_dict)

            tf.reset_default_graph()
            tf.set_random_seed(0)

            compress_nnef = False
            command = """
                ./nnef_tools/convert.py --input-framework=tensorflow-py \\
                                        --output-framework=nnef \\
                                        --input-model={module}.{network}{checkpoint} \\
                                        --output-directory=out/{network}/nnef \\
                                        --custom-converters="{custom}" \\
                                        --permissive \\
                                        --io-transformation=SMART_TF_NHWC_TO_NCHW \\
                                        {compress}
                """.format(checkpoint=':' +
                           checkpoint_path if checkpoint_path else "",
                           network=fun.__name__,
                           custom=custom_tf_to_nnef_converters,
                           compress="--compress" if compress_nnef else "",
                           module=test_module)

            convert.convert_using_command(command)

            open(os.path.join("out", fun.__name__, "__init__.py"), "w").close()

            tf.reset_default_graph()
            tf.set_random_seed(0)
            fun()
            conv_info = conversion_info.load(
                os.path.join("out", fun.__name__, "nnef", "conversion.json"))
            tf_activation_exporter.export(output_path=os.path.join(
                "out", fun.__name__, "nnef", "activations"),
                                          feed_dict=feed_dict,
                                          conversion_info=conv_info,
                                          checkpoint_path=checkpoint_path,
                                          verbose=False)

            prefer_nhwc_options = [True]
            if tf_has_cuda_gpu():
                prefer_nhwc_options += [False]
            for prefer_nhwc in prefer_nhwc_options:
                print("Converting to TensorFlow {}".format(
                    "NHWC" if prefer_nhwc else "NCHW"))
                data_format_str = ("nhwc" if prefer_nhwc else "nchw")
                tf_output_dir = os.path.join("out", fun.__name__,
                                             "tf_" + data_format_str)
                command = """
                    ./nnef_tools/convert.py --input-framework=nnef \\
                                            --output-framework=tensorflow-py \\
                                            --input-model=out/{network}/nnef/model{tgz} \\
                                            --output-directory={output} \\
                                            --io-transformation=SMART_NCHW_TO_TF_NHWC \\
                                            --custom-converters="{custom}" \\
                                            --permissive
                    """.format(network=fun.__name__,
                               custom=custom_nnef_to_tf_converters,
                               tgz=".nnef.tgz" if compress_nnef else "",
                               output=tf_output_dir)
                convert.convert_using_command(command)

                open(os.path.join(tf_output_dir, "__init__.py"), "w").close()
                open(os.path.join(tf_output_dir, "model", "__init__.py"),
                     "w").close()

                with open(os.path.join(tf_output_dir, "model",
                                       "model.py")) as f:
                    tf_src = f.read()

                # noinspection PyProtectedMember
                new_net_fun = tf_py_io._tfsource_to_function(
                    tf_src, fun.__name__)

                conv_info_tf_to_nnef = conversion_info.load(
                    os.path.join(out_dir, "nnef", "conversion.json"))
                conv_info_nnef_to_tf = conversion_info.load(
                    os.path.join(tf_output_dir, "conversion.json"))
                conv_info_tf_to_tf = conversion_info.compose(
                    conv_info_tf_to_nnef, conv_info_nnef_to_tf)

                conversion_info.dump(
                    conv_info_tf_to_tf,
                    os.path.join(tf_output_dir, "conv_info_tf_to_tf.json"))

                feed_dict2 = activation_test.transform_feed_dict(
                    feed_dict, conv_info_tf_to_tf)
                nnef2_out_dir = os.path.join(out_dir,
                                             "nnef_from_tf_" + data_format_str)

                tf.reset_default_graph()
                tf.set_random_seed(0)

                command = """
                    ./nnef_tools/convert.py --input-framework=tensorflow-py \\
                                            --output-framework=nnef \\
                                            --input-model={input}{checkpoint} \\
                                            --output-directory={output} \\
                                            --custom-converters="{custom}" \\
                                            --permissive \\
                                            --io-transformation=SMART_TF_NHWC_TO_NCHW \\
                                            {compress}
                    """.format(checkpoint=(
                    ':' +
                    (os.path.join(tf_output_dir, "model", "checkpoint",
                                  "model.ckpt") if checkpoint_path else "")),
                               input=tf_output_dir.replace('/', '.') +
                               ".model.model." + fun.__name__,
                               custom=custom_tf_to_nnef_converters,
                               compress="--compress" if compress_nnef else "",
                               output=nnef2_out_dir)

                convert.convert_using_command(command)

                conv_info_tf_to_nnef2 = conversion_info.load(
                    os.path.join(out_dir, "nnef_from_tf_" + data_format_str,
                                 "conversion.json"))
                conv_info_nnef_to_nnef = conversion_info.compose(
                    conv_info_nnef_to_tf, conv_info_tf_to_nnef2)
                conversion_info.dump(
                    conv_info_nnef_to_nnef,
                    os.path.join(nnef2_out_dir, "conv_info_nnef_to_nnef.json"))

                tf.reset_default_graph()
                tf.set_random_seed(0)
                new_net_fun()
                tf_activation_exporter.export(
                    output_path=os.path.join(nnef2_out_dir, "activations"),
                    feed_dict=feed_dict2,
                    conversion_info=conv_info_tf_to_nnef2,
                    checkpoint_path=(os.path.join(tf_output_dir, "model",
                                                  "checkpoint", "model.ckpt")
                                     if checkpoint_path else None),
                    verbose=False)

                if cmp:
                    activation_test.compare_activation_dirs(
                        os.path.join(out_dir, "nnef", "activations"),
                        os.path.join(out_dir,
                                     "nnef_from_tf_" + data_format_str,
                                     "activations"),
                        conv_info_nnef_to_nnef,
                        verbose=False)
        finally:
            if DELETE_DATS_AND_CHECKPOINTS:
                dat_files = recursive_glob(out_dir, "*.dat")
                checkpoints = recursive_glob(out_dir, "*ckpt*")
                for file_name in set(dat_files + checkpoints):
                    os.remove(file_name)
Ejemplo n.º 5
0
    def _test(self,
              fun,
              cmp=True,
              custom_tf_to_nnef_converters="",
              custom_nnef_to_tf_converters="",
              test_module="nnef_tests.conversion.tf_py_layer_test_cases"):

        activation_testing = int(os.environ.get('NNEF_ACTIVATION_TESTING',
                                                '1'))
        print("Activation testing is", "ON" if activation_testing else "OFF")

        out_dir = os.path.join("out", fun.__name__)
        try:
            tf.reset_default_graph()
            tf.set_random_seed(0)

            network_outputs = fun()
            feed_dict = get_feed_dict()
            checkpoint_path = os.path.join("out", fun.__name__,
                                           "orig_checkpoint",
                                           fun.__name__ + ".ckpt")
            checkpoint_path = save_random_checkpoint(network_outputs,
                                                     checkpoint_path,
                                                     feed_dict)

            tf.reset_default_graph()
            tf.set_random_seed(0)

            compress_nnef = False
            command = """
                ./nnef_tools/convert.py --input-format tensorflow-py \\
                                        --output-format nnef \\
                                        --input-model {module}.{network} {checkpoint} \\
                                        --output-model out/{network}/{network}.nnef{tgz} \\
                                        --custom-converters {custom} \\
                                        --permissive \\
                                        --io-transformation SMART_TF_NHWC_TO_NCHW \\
                                        --conversion-info \\
                                        {compress}
                """.format(
                checkpoint=checkpoint_path if checkpoint_path else "",
                network=fun.__name__,
                custom=" ".join(custom_tf_to_nnef_converters),
                compress="--compress" if compress_nnef else "",
                module=test_module,
                tgz=".tgz" if compress_nnef else "")

            convert.convert_using_command(command)

            if activation_testing:
                tf.reset_default_graph()
                tf.set_random_seed(0)
                fun()
                conv_info = conversion_info.load(
                    os.path.join("out", fun.__name__,
                                 fun.__name__ + ".nnef.conversion.json"))

                tf_activation_exporter.export(output_path=os.path.join(
                    "out", fun.__name__, fun.__name__ + ".nnef",
                    "activations"),
                                              feed_dict=feed_dict,
                                              conversion_info=conv_info,
                                              checkpoint_path=checkpoint_path,
                                              verbose=False)

            prefer_nhwc_options = [True]
            if tf_has_cuda_gpu():
                prefer_nhwc_options += [False]
            for prefer_nhwc in prefer_nhwc_options:
                print("Converting to TensorFlow {}".format(
                    "NHWC" if prefer_nhwc else "NCHW"))
                data_format_str = ("nhwc" if prefer_nhwc else "nchw")
                tf_output_path = os.path.join(
                    "out", fun.__name__,
                    fun.__name__ + '_' + data_format_str + '.py')
                command = """
                    ./nnef_tools/convert.py --input-format nnef \\
                                            --output-format tensorflow-py \\
                                            --input-model out/{network}/{network}.nnef{tgz} \\
                                            --output-model {output} \\
                                            --io-transformation SMART_NCHW_TO_TF_NHWC \\
                                            --custom-converters {custom} \\
                                            --permissive \\
                                            --conversion-info
                    """.format(network=fun.__name__,
                               custom=" ".join(custom_nnef_to_tf_converters),
                               tgz=".nnef.tgz" if compress_nnef else "",
                               output=tf_output_path)
                convert.convert_using_command(command)

                with open(os.path.join(tf_output_path), 'r') as f:
                    tf_src = f.read()

                # noinspection PyProtectedMember
                new_net_fun = tf_py_io._tfsource_to_function(
                    tf_src, fun.__name__)

                conv_info_tf_to_nnef = conversion_info.load(
                    os.path.join("out", fun.__name__,
                                 fun.__name__ + ".nnef.conversion.json"))
                conv_info_nnef_to_tf = conversion_info.load(
                    os.path.join(tf_output_path + ".conversion.json"))
                conv_info_tf_to_tf = conversion_info.compose(
                    conv_info_tf_to_nnef, conv_info_nnef_to_tf)

                conversion_info.dump(
                    conv_info_tf_to_tf,
                    os.path.join(tf_output_path + ".conv_info_tf_to_tf.json"))

                feed_dict2 = activation_test.transform_feed_dict(
                    feed_dict, conv_info_tf_to_tf)
                nnef2_out_dir = os.path.join(tf_output_path + ".nnef")

                tf.reset_default_graph()
                tf.set_random_seed(0)

                command = """
                    ./nnef_tools/convert.py --input-format tensorflow-py \\
                                            --output-format nnef \\
                                            --input-model {input} {checkpoint} \\
                                            --output-model {output} \\
                                            --custom-converters {custom} \\
                                            --permissive \\
                                            --io-transformation SMART_TF_NHWC_TO_NCHW \\
                                            --conversion-info \\
                                            {compress}
                    """.format(
                    checkpoint=(os.path.join(tf_output_path + ".checkpoint")
                                if checkpoint_path else ""),
                    input=tf_output_path.replace('/', '.')[:-len('.py')] +
                    "." + fun.__name__,
                    custom=" ".join(custom_tf_to_nnef_converters),
                    compress="--compress" if compress_nnef else "",
                    output=nnef2_out_dir)

                convert.convert_using_command(command)

                conv_info_tf_to_nnef2 = conversion_info.load(
                    nnef2_out_dir + ".conversion.json")
                conv_info_nnef_to_nnef = conversion_info.compose(
                    conv_info_nnef_to_tf, conv_info_tf_to_nnef2)
                conversion_info.dump(
                    conv_info_nnef_to_nnef,
                    os.path.join(nnef2_out_dir +
                                 ".conv_info_nnef_to_nnef.json"))

                if activation_testing:
                    tf.reset_default_graph()
                    tf.set_random_seed(0)
                    new_net_fun()
                    tf_activation_exporter.export(
                        output_path=os.path.join(nnef2_out_dir, "activations"),
                        feed_dict=feed_dict2,
                        conversion_info=conv_info_tf_to_nnef2,
                        checkpoint_path=(os.path.join(tf_output_path +
                                                      ".checkpoint")
                                         if checkpoint_path else None),
                        verbose=False)

                    if cmp:
                        activation_test.compare_activation_dirs(
                            os.path.join("out", fun.__name__,
                                         fun.__name__ + ".nnef",
                                         "activations"),
                            os.path.join(nnef2_out_dir, "activations"),
                            conv_info_nnef_to_nnef,
                            verbose=False)
        finally:
            if self.delete_dats_and_checkpoints:
                dat_files = utils.recursive_glob(out_dir, "*.dat")
                checkpoints = utils.recursive_glob(out_dir, "*ckpt*")
                for file_name in set(dat_files + checkpoints):
                    os.remove(file_name)