Beispiel #1
0
def main():
    np.set_printoptions(precision=3,
                        suppress=True,
                        floatmode='fixed',
                        threshold=sys.maxsize)
    args = get_args()

    if args.tensor_path is None:
        utils.set_stdin_to_binary()

        if sys.stdin.isatty():
            print("No input provided!", file=sys.stderr)
            exit(1)

        tensor = nnef.read_tensor(sys.stdin)
    else:
        tensor = read_nnef_tensor(args.tensor_path)

    print('Shape:', list(tensor.shape))

    if int(args.k) <= 0:
        print(tensor)
    else:
        axis = int(args.axis)
        k = min(int(args.k), tensor.shape[axis])

        if axis >= len(tensor.shape) or axis < -len(tensor.shape):
            print("axis={} is outside the supported range for this tensor.".
                  format(axis))
            exit(1)

        values, indices = topk(tensor, axis=axis, k=k)
        values = np.squeeze(values)
        indices = np.squeeze(indices)
        print("TopK({}, k={}, axis={}):\nValues:\n{}\nIndices:\n{}".format(
            tensor_name(args.tensor_path) if args.tensor_path is not None else
            "stdin", k, axis, values, indices))
Beispiel #2
0
def _are_activations_close(activation_pair,
                           verbose=False,
                           allowed_bad_pixel_ratio=0.0):
    # type: (_ActivationPair, bool, float)->bool

    arr1 = read_nnef_tensor(activation_pair.from_)  # type:np.ndarray
    arr2 = read_nnef_tensor(activation_pair.to)  # type: np.ndarray
    for transform in activation_pair.transforms:
        arr1 = transform.apply_np(arr1)

    if arr1.dtype != arr2.dtype:
        print("Warning: different dtypes: {} != {} for {}, {}".format(
            arr1.dtype.name, arr2.dtype.name, activation_pair.short_from,
            activation_pair.short_to))

    if not _are_np_dtypes_compatible_in_nnef(arr1.dtype, arr2.dtype):
        print("Error: incompatible dtypes: {} != {} for {}, {}".format(
            arr1.dtype.name, arr2.dtype.name, activation_pair.short_from,
            activation_pair.short_to))
        return False
    elif arr1.shape != arr2.shape:
        print("Error: shape error {} != {} for {}, {}".format(
            arr1.shape, arr2.shape, activation_pair.short_from,
            activation_pair.short_to))
        return False
    else:
        if arr1.dtype == np.dtype(np.bool_):
            differences = (arr1 != arr2).astype(np.int8).sum(dtype=np.int64)
            if differences > 0:  # Maybe too strict
                print("Error: Bool tensors different at {} places: {}, {}".
                      format(differences, activation_pair.short_from,
                             activation_pair.short_to))
                return False
            return True
        else:
            error_rate1 = np.abs(arr2 - arr1) / np.maximum(np.abs(arr1), 1e-32)
            error_rate2 = np.abs(arr2 - arr1) / np.maximum(np.abs(arr2), 1e-32)
            error_rate = np.maximum(error_rate1, error_rate2)
            max_error_rate1 = np.max(error_rate1)
            max_error_rate2 = np.max(error_rate2)
            max_error_rate = max(max_error_rate1, max_error_rate2)
            max_diff = np.max(np.abs(arr2 - arr1))
            max_value = min(np.max(np.abs(arr1)), np.max(np.abs(arr2)))

            err_thresh = 1e-4

            if max_value < err_thresh:
                print("Info: max value: {} for {}, {}".format(
                    max_value, activation_pair.short_from,
                    activation_pair.short_to))

            bad_pixel_ratio = np.count_nonzero(
                error_rate > err_thresh) / error_rate.size
            if max_error_rate > err_thresh and max_diff > err_thresh:
                if bad_pixel_ratio > allowed_bad_pixel_ratio:
                    print("Error: max error rate: {} max diff: {} for {}, {}".
                          format(max_error_rate, max_diff,
                                 activation_pair.short_from,
                                 activation_pair.short_to))
                    print("Info: Bad pixel ratio: {}%".format(100 *
                                                              bad_pixel_ratio))
                    # print(arr1, arr2)
                    return False
                else:
                    print(
                        "Warning: max error rate: {} max diff: {} for {}, {}".
                        format(max_error_rate, max_diff,
                               activation_pair.short_from,
                               activation_pair.short_to))
                    print("Info: Bad pixel ratio: {}%".format(100 *
                                                              bad_pixel_ratio))
                    # print(arr1, arr2)
                    return True
            elif max_diff != 0 or verbose:
                print(
                    "Info: max error rate: {} max diff: {} for {}, {}".format(
                        max_error_rate, max_diff, activation_pair.short_from,
                        activation_pair.short_to))
            return True
Beispiel #3
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)