def export(prototxt_path, caffemodel_path, input_sources, conversion_info_path,
           output_path):
    conv_info = conversion_info.load(conversion_info_path)
    tensor_info_by_caffe_name = {t.source_name: t for t in conv_info.tensors}
    np.random.seed(0)
    net = caffe.Net(prototxt_path, caffemodel_path, caffe.TEST)
    net.forward(
        **input_sources.create_feed_dict(_get_input_dtypes_and_shapes(net)))
    has_error = False

    for k, v in six.iteritems(net.blobs):
        tensor_info = tensor_info_by_caffe_name.get(k)
        if tensor_info is not None:
            filename = os.path.join(output_path,
                                    tensor_info.target_name + ".dat")
            arr = v.data.copy()
            if np.isnan(arr).any():
                print("Error: '{}' has nan's".format(tensor_info.target_name))
                has_error = True
            elif not np.isfinite(arr).all():
                print("Error: '{}' has inf's".format(tensor_info.target_name))
                has_error = True
            try:
                for transform in tensor_info.transforms:
                    arr = transform.apply_np(arr)
                write_nnef_tensor(filename, np.asarray(arr, order='C'))
            except ValueError as e:
                print("Error: Can not export '{}': {}".format(
                    tensor_info.target_name, e))
    if has_error:
        raise utils.NNEFToolsException("There were errors!")
示例#2
0
def main():
    try:
        args = get_args(sys.argv)

        if not args.output:
            if sys.stdout.isatty():
                raise utils.NNEFToolsException("No output provided.")
            utils.set_stdout_to_binary()

        if args.dtype is None:
            distribution = args.params[0]
            if distribution == 'binomial':
                args.dtype = "int32"
            elif distribution == 'bernoulli':
                args.dtype = "bool"
            else:
                args.dtype = "float32"

        args.params[1:] = [float(param) for param in args.params[1:]]

        if args.seed != -1:
            np.random.seed(args.seed)

        random_input = RandomInput(*args.params)
        arr = create_input(random_input,
                           np_dtype=np.dtype(args.dtype),
                           shape=args.shape)

        if args.output:
            write_nnef_tensor(args.output, arr)
        else:
            nnef.write_tensor(sys.stdout, arr)
    except Exception as e:
        print('Error: {}'.format(e), file=sys.stderr)
        exit(1)
示例#3
0
def main():
    try:
        args = get_args(sys.argv)

        if not args.output:
            if sys.stdout.isatty():
                raise utils.NNEFToolsException("No output provided.")
            utils.set_stdout_to_binary()

        image_input = ImageInput([
            os.path.join(path, '*') if os.path.isdir(path) else path
            for path in args.input
        ],
                                 color_format=args.color,
                                 data_format=args.format,
                                 range=args.range,
                                 norm=[args.mean, args.std])
        shape = None
        if args.size is not None:
            shape = [
                1, 3, args.size[1], args.size[0]
            ] if args.format == 'NCHW' else [1, args.size[1], args.size[0], 3]
        arr = create_input(image_input,
                           np_dtype=np.dtype(args.dtype),
                           shape=shape,
                           allow_bigger_batch=True)

        if args.output:
            write_nnef_tensor(args.output, arr)
        else:
            nnef.write_tensor(sys.stdout, arr)
    except Exception as e:
        print('Error: {}'.format(e), file=sys.stderr)
        exit(1)
示例#4
0
    def __call__(self, nnef_tensor, torch_tensor):
        # type: (NNEFTensor, torch.Tensor)->None

        if nnef_tensor.name in self.tensor_names_to_export:
            array = to_numpy_array(torch_tensor, nnef_dtype=nnef_tensor.dtype)

            if self.output_directory is not None:
                nnef_io.write_nnef_tensor(filename=os.path.join(
                    self.output_directory, nnef_tensor.name + ".dat"),
                                          array=array)
            else:
                nnef.write_tensor(sys.stdout, array)
示例#5
0
def main():
    try:
        args = get_args(sys.argv)

        if not args.output:
            if sys.stdout.isatty():
                raise utils.NNEFToolsException("No output provided.")
            utils.set_stdout_to_binary()

        args.params = InputSources(args.params)

        if args.seed != -1:
            np.random.seed(args.seed)

        parser_configs = NNEFParserConfig.load_configs(args.custom_operations,
                                                       load_standard=True)
        reader = nnef_io.Reader(parser_configs=parser_configs,
                                input_shape=args.shape)

        # read without weights
        graph = reader(
            os.path.join(args.network, 'graph.nnef') if os.path.
            isdir(args.network) else args.network)

        inputs = tuple(
            args.params.create_input(name=input.name,
                                     np_dtype=input.get_numpy_dtype(),
                                     shape=input.shape,
                                     allow_bigger_batch=True)
            for input in graph.inputs)

        if args.output:
            for tensor, array in zip(graph.inputs, inputs):
                nnef_io.write_nnef_tensor(
                    os.path.join(args.output, tensor.name + '.dat'), array)
        else:
            for array in inputs:
                nnef.write_tensor(sys.stdout, array)
    except Exception as e:
        print('Error: {}'.format(e), file=sys.stderr)
        exit(1)
示例#6
0
def generate_weights(g, nnef_path, output_dir, input_sources):
    # type: (NNEFGraph, str, str, InputSources)->bool
    did_write = False
    warned = False
    for tensor in g.tensors:
        if tensor.is_variable:
            dat_path = os.path.join(output_dir, tensor.label + '.dat')
            if os.path.exists(dat_path):
                if not warned:
                    print("Warning: leaving existing weights unchanged".format(
                        tensor.name, tensor.label),
                          file=sys.stderr)
                    warned = True
            else:
                array = input_sources.create_input(
                    tensor.name,
                    np_dtype=tensor.get_numpy_dtype(),
                    shape=tensor.shape)
                nnef_io.write_nnef_tensor(dat_path, array)
                did_write = True
    if not os.path.isdir(nnef_path):
        shutil.copy(nnef_path, os.path.join(output_dir, 'graph.nnef'))
        did_write = True
    return did_write
示例#7
0
def run_using_argv(argv):
    try:
        args = get_args(argv)
        write_outputs = args.output_names is None or args.output_names

        if args.input is None:
            if sys.stdin.isatty():
                raise utils.NNEFToolsException("No input provided!")
            utils.set_stdin_to_binary()

        if write_outputs:
            if args.output is None:
                if sys.stdout.isatty():
                    raise utils.NNEFToolsException("No output provided!")
                utils.set_stdout_to_binary()

        parent_dir_of_input_model = os.path.dirname(
            utils.path_without_trailing_separator(args.network))
        tmp_dir = None

        if args.network.endswith('.tgz'):
            nnef_path = tmp_dir = tempfile.mkdtemp(
                prefix="nnef_", dir=parent_dir_of_input_model)
            utils.tgz_extract(args.network, nnef_path)
        else:
            nnef_path = args.network

        try:
            parser_configs = NNEFParserConfig.load_configs(
                args.custom_operations, load_standard=True)

            # read without weights
            reader = nnef_io.Reader(parser_configs=parser_configs,
                                    infer_shapes=False)
            graph = reader(
                os.path.join(nnef_path, 'graph.nnef') if os.path.
                isdir(nnef_path) else nnef_path)

            if args.input is None:
                inputs = tuple(
                    nnef.read_tensor(sys.stdin)
                    for _ in range(len(graph.inputs)))
            elif len(args.input) == 1 and os.path.isdir(args.input[0]):
                inputs = tuple(
                    nnef_io.read_nnef_tensor(
                        os.path.join(args.input[0], tensor.name + '.dat'))
                    for tensor in graph.inputs)
            else:
                inputs = tuple(
                    nnef_io.read_nnef_tensor(path) for path in args.input)

            reader = nnef_io.Reader(parser_configs=parser_configs,
                                    input_shape=tuple(
                                        list(input.shape) for input in inputs))

            graph = reader(nnef_path)

            tensor_hooks = []

            stats_hook = None
            if args.stats:
                stats_hook = backend.StatisticsHook()
                tensor_hooks.append(stats_hook)

            if write_outputs and args.output_names is not None:
                if '*' in args.output_names:
                    tensor_hooks.append(
                        backend.ActivationExportHook(
                            tensor_names=[
                                t.name for t in graph.tensors
                                if not t.is_constant and not t.is_variable
                            ],
                            output_directory=args.output))
                else:
                    tensor_hooks.append(
                        backend.ActivationExportHook(
                            tensor_names=args.output_names,
                            output_directory=args.output))

            if args.permissive:
                backend.try_to_fix_unsupported_attributes(graph)

            outputs = backend.run(nnef_graph=graph,
                                  inputs=inputs,
                                  device=args.device,
                                  custom_operations=get_custom_runners(
                                      args.custom_operations),
                                  tensor_hooks=tensor_hooks)

            if write_outputs and args.output_names is None:
                if args.output is None:
                    for array in outputs:
                        nnef.write_tensor(sys.stdout, array)
                else:
                    for tensor, array in zip(graph.outputs, outputs):
                        nnef_io.write_nnef_tensor(
                            os.path.join(args.output, tensor.name + '.dat'),
                            array)

            if stats_hook:
                if args.stats.endswith('/') or args.stats.endswith('\\'):
                    stats_path = os.path.join(nnef_path, args.stats,
                                              'graph.stats')
                else:
                    stats_path = os.path.join(nnef_path, args.stats)
                stats_hook.save_statistics(stats_path)

            if tmp_dir and (args.stats and _is_inside(nnef_path, args.stats)):
                if args.network.endswith('.tgz'):
                    print("Info: Changing input archive", file=sys.stderr)
                    shutil.move(args.network,
                                args.network + '.nnef-tools-backup')
                    utils.tgz_compress(dir_path=nnef_path,
                                       file_path=args.network)
                    os.remove(args.network + '.nnef-tools-backup')
                else:
                    output_path = args.network.rsplit('.', 1)[0] + '.nnef.tgz'
                    backup_path = output_path + '.nnef-tools-backup'
                    if os.path.exists(output_path):
                        shutil.move(output_path, backup_path)
                    utils.tgz_compress(dir_path=nnef_path,
                                       file_path=output_path)
                    if os.path.exists(backup_path):
                        os.remove(backup_path)
        finally:
            if tmp_dir:
                shutil.rmtree(tmp_dir)
    except utils.NNEFToolsException as e:
        print("Error: " + str(e), file=sys.stderr)
        exit(1)
    except nnef.Error as e:
        print("Error: " + str(e), file=sys.stderr)
        exit(1)
示例#8
0
def export(
        output_path,  # type: str
        feed_dict,  # type: typing.Dict[typing.Any, np.ndarray]
        conversion_info,  # type: ConversionInfo
        graph=None,  # type: typing.Optional[tf.Graph]
        checkpoint_path=None,  # type: str
        tensors_per_iter=25,  # type: int
        verbose=False,  # type: bool
        init_variables=False,  # type: bool
        input_output_only=False,  # type: bool
):
    # type: (...)->None

    if graph is None:
        graph = tf.get_default_graph()

    has_error = False
    tensor_infos = []

    for tensor_info in conversion_info.tensors:
        if tensor_info.is_variable or (input_output_only
                                       and not tensor_info.is_input
                                       and not tensor_info.is_output):
            continue
        try:
            tensor = graph.get_tensor_by_name(tensor_info.source_name)

            if not graph.is_fetchable(tensor):
                print("Warning: Tensor is not fetchable: {}".format(
                    tensor_info.source_name))
            elif not isinstance(tensor, tf.Tensor):
                print("Warning: Not a tensor: {}".format(
                    tensor_info.source_name))
            else:
                tensor_infos.append(
                    _TensorInfo(internal_name=tensor_info.source_name,
                                external_name=tensor_info.target_name,
                                transforms=tensor_info.transforms,
                                tensor=tensor,
                                target_shape=tensor_info.target_shape))
        except KeyError:
            print("Warning: Tensor not found: {}".format(
                tensor_info.source_name))

    with tf.Session() as sess:
        saver = tf.train.Saver() if checkpoint_path else None
        start = 0
        while start < len(tensor_infos):
            tensors = [
                info.tensor
                for info in tensor_infos[start:start + tensors_per_iter]
            ]

            if init_variables:
                sess.run(tf.global_variables_initializer())
            if checkpoint_path is not None:
                if os.path.isdir(checkpoint_path):
                    saver.restore(sess,
                                  tf.train.latest_checkpoint(checkpoint_path))
                else:
                    saver.restore(sess, checkpoint_path)
            values = sess.run(tensors, feed_dict)

            for i, arr in enumerate(values):
                info = tensor_infos[start + i]
                filename = os.path.join(output_path,
                                        info.external_name + ".dat")

                if np.isnan(arr).any():
                    print("Error: '{}' has nan's".format(info.external_name))
                    has_error = True
                elif not np.isfinite(arr).all():
                    print("Error: '{}' has inf's".format(info.external_name))
                    has_error = True
                try:
                    for transform in info.transforms:
                        arr = transform.apply_np(arr)
                    write_nnef_tensor(filename, np.asarray(arr, order='C'))
                except ValueError as e:
                    print("Error: Can not export '{}': {}".format(
                        info.external_name, e))

            start += len(tensors)

            if verbose:
                print("Info: Exported {}/{}".format(start, len(tensor_infos)))
    if has_error:
        raise utils.NNEFToolsException("There were errors!")