Example #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)
Example #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.")
Example #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.")
Example #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)
    def _test_network(path, source_shape=1):
        assert utils.is_anyint(source_shape) or isinstance(
            source_shape, (list, tuple))

        network = os.path.basename(path.rsplit('.', 1)[0])
        command = """
        ./nnef_tools/convert.py --input-format=tensorflow-pb \\
                                --input-model={path} \\
                                --output-format=nnef \\
                                --output-model=out/nnef/{network}.nnef.tgz \\
                                --compress \\
                                --conversion-info \\
                                --input-shape="{shape}" """.format(
            path=path, network=network, shape=source_shape)
        print(command)
        convert.convert_using_command(command)

        for prefer_nchw in [False, True]:
            print("Prefer NCHW" if prefer_nchw else "Prefer NHWC")

            prefer_nchw_opt = "--prefer-nchw" if prefer_nchw else ""
            prefer_nchw_str = "_prefer_nchw" if prefer_nchw else ""

            command = """
            ./nnef_tools/convert.py --input-format=nnef \\
                                    --input-model=out/nnef/{network}.nnef.tgz \\
                                    --output-format=tensorflow-pb \\
                                    --output-model=out/tensorflow-pb{nchw_str}/{network}.pb \\
                                    --conversion-info \\
                                    {nchw_opt}""".format(
                network=network,
                nchw_str=prefer_nchw_str,
                nchw_opt=prefer_nchw_opt)
            print(command)
            convert.convert_using_command(command)

            command = """
            ./nnef_tools/convert.py --input-format=tensorflow-pb \\
                                    --input-model=out/tensorflow-pb{nchw_str}/{network}.pb \\
                                    --output-format=nnef \\
                                    --output-model=out/nnef2{nchw_str}/{network}.nnef.tgz \\
                                    --compress \\
                                    --conversion-info""".format(
                network=network, nchw_str=prefer_nchw_str)
            print(command)
            convert.convert_using_command(command)

            activation_testing = int(
                os.environ.get('NNEF_ACTIVATION_TESTING', '1'))
            print("Activation testing is",
                  "ON" if activation_testing else "OFF")
            if activation_testing:
                import numpy as np
                import tensorflow as tf
                from nnef_tools import export_activation
                from nnef_tools.activation_export.tensorflow import tf_activation_exporter

                def normalize_shape(shape, default=1):
                    return [
                        int(dim.value) if dim.value is not None else default
                        for dim in shape.dims
                    ]

                tf.reset_default_graph()
                export_activation.tf_set_default_graph_from_pb(path)

                if isinstance(source_shape, (list, tuple)):
                    feed_dict = {
                        placeholder.name: np.random.random(source_shape)
                        for placeholder in get_placeholders()
                    }
                else:
                    feed_dict = {
                        placeholder.name: np.random.random(
                            normalize_shape(placeholder.shape,
                                            default=source_shape))
                        for placeholder in get_placeholders()
                    }

                conv_info_tf_to_nnef = conversion_info.load(
                    os.path.join("out", 'nnef',
                                 network + ".nnef.tgz.conversion.json"))

                tf_activation_exporter.export(
                    output_path=os.path.join("out", 'nnef', network +
                                             ".nnef.tgz.activations"),
                    feed_dict=feed_dict,
                    conversion_info=conv_info_tf_to_nnef,
                    input_output_only=True,
                    verbose=False)

                conv_info_nnef_to_tf = conversion_info.load(
                    os.path.join('out', 'tensorflow-pb' + prefer_nchw_str,
                                 network + ".pb.conversion.json"))

                conv_info_tf_to_tf = conversion_info.compose(
                    conv_info_tf_to_nnef, conv_info_nnef_to_tf)

                feed_dict2 = activation_test.transform_feed_dict(
                    feed_dict, conv_info_tf_to_tf)

                conv_info_tf_to_nnef2 = conversion_info.load(
                    os.path.join('out', 'nnef2' + prefer_nchw_str,
                                 network + ".nnef.tgz.conversion.json"))
                conv_info_nnef_to_nnef = conversion_info.compose(
                    conv_info_nnef_to_tf, conv_info_tf_to_nnef2)

                tf.reset_default_graph()
                export_activation.tf_set_default_graph_from_pb(
                    os.path.join('out', 'tensorflow-pb' + prefer_nchw_str,
                                 network + ".pb"))

                tf_activation_exporter.export(
                    output_path=os.path.join("out", 'nnef2' + prefer_nchw_str,
                                             network +
                                             ".nnef.tgz.activations"),
                    feed_dict=feed_dict2,
                    conversion_info=conv_info_tf_to_nnef2,
                    input_output_only=True,
                    verbose=False)

                activation_test.compare_activation_dirs(
                    os.path.join("out", 'nnef',
                                 network + ".nnef.tgz.activations"),
                    os.path.join("out", 'nnef2' + prefer_nchw_str,
                                 network + ".nnef.tgz.activations"),
                    conv_info_nnef_to_nnef,
                    verbose=False)
    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)
Example #7
0
def test_tf_pb(filename, source_shapes, feed_dict, prefer_nhwc, network_name,
               export_io_only, delete_after_each):
    dir = os.path.join('out',
                       network_name + ('_nhwc' if prefer_nhwc else '_nchw'))

    if os.path.exists(dir):
        shutil.rmtree(dir)

    tf_pb_all_in_one.convert_tf_pb_to_nnef(
        file_name=filename,
        output_directory=os.path.join(dir, 'nnef'),
        network_name=network_name,
        optimization_level=tf_pb_all_in_one.OptimizationLevel.FULL,
        io_transform=tf_pb_all_in_one.IOTransform.SMART_TF_NHWC_TO_NCHW,
        source_shapes=source_shapes,
        activation_export_feed_dict=feed_dict,
        activation_export_io_only=export_io_only)

    conv_info1 = conversion_info.load(
        os.path.join(dir, 'nnef', 'conversion.json'))

    tf_pb_all_in_one.convert_nnef_to_tf_pb(
        nnef_tgz_or_dir_path=os.path.join(dir, 'nnef', network_name + '_nnef'),
        output_directory=os.path.join(dir, 'tf_pb'),
        optimization_level=tf_pb_all_in_one.OptimizationLevel.FULL,
        io_transform=(tf_pb_all_in_one.IOTransform.SMART_NCHW_TO_TF_NHWC
                      if prefer_nhwc else
                      tf_pb_all_in_one.IOTransform.SMART_NCHW_TO_TF_NCHW),
        prefer_nhwc=prefer_nhwc)
    conv_info2 = conversion_info.load(
        os.path.join(dir, 'tf_pb', 'conversion.json'))

    feed_dict2 = (activation_test.transform_feed_dict(
        feed_dict=feed_dict,
        conv_info=conversion_info.compose(conv_info1, conv_info2))
                  if feed_dict is not None else None)

    tf_pb_all_in_one.convert_tf_pb_to_nnef(
        file_name=os.path.join(dir, 'tf_pb', 'graph.pb'),
        output_directory=os.path.join(dir, 'nnef2'),
        network_name=network_name,
        optimization_level=tf_pb_all_in_one.OptimizationLevel.FULL,
        io_transform=(tf_pb_all_in_one.IOTransform.SMART_TF_NHWC_TO_NCHW
                      if prefer_nhwc else
                      tf_pb_all_in_one.IOTransform.SMART_TF_NCHW_TO_NCHW),
        activation_export_feed_dict=feed_dict2,
        activation_export_io_only=export_io_only)

    conv_info3 = conversion_info.load(
        os.path.join(dir, 'nnef2', 'conversion.json'))

    if feed_dict is not None:
        activation_test.compare_activation_dirs(
            os.path.join(dir, 'nnef', 'activations'),
            os.path.join(dir, 'nnef2', 'activations'),
            conv_info=conversion_info.compose(conv_info2, conv_info3),
            verbose=False,
            allowed_bad_pixel_ratio=0.01
            if not prefer_nhwc and not export_io_only else 0.0)

    if delete_after_each:
        shutil.rmtree(dir)