def transform(input_): if isinstance(input_, NNEFTensor): if input_.is_constant and input_.rank == 0: return input_.data[0] else: return nnef.Identifier(input_.name) return input_
def _results_to_result_names(results): results2 = [] for r in results: if isinstance(r, (list, tuple)): results2.append(_results_to_result_names(r)) else: results2.append( nnef.Identifier(utils.ensure_not_unicode_in_python2(r.name))) if isinstance(results, tuple): return tuple(results2) else: return results2
def _print(graph, file, extensions, fragments, version_custom_ops, annotate_shapes): assert graph.is_sorted(), "graph must be topologically sorted" assert all(tensor.name is not None or (tensor.producer is None and tensor.data is not None) for tensor in graph.tensors), \ "all tensors must have names" assert all(all(s is not None for s in op.attribs['shape']) for op in graph.operations if op.type == 'external'), \ "external ops must not contain undefined shapes" print(nnef.format_version((1, 0)), file=file) if len(extensions): print(file=file) print(nnef.format_extensions(extensions), file=file) if fragments: print(file=file) print(fragments, file=file) print(file=file) graph_name = as_str(graph.name) if graph.name is not None else "G" graph_inputs = [as_str(item.name) for item in graph.inputs] graph_outputs = [as_str(item.name) for item in graph.outputs] print("graph {}({}) -> ({})".format(graph_name, ', '.join(graph_inputs), ', '.join(graph_outputs)), file=file) print("{", file=file) versions = {} for op in graph.operations: assert all(isinstance(item, Tensor) for item in op.outputs) inputs = ((from_numpy(item.data) if item.producer is None else nnef.Identifier(as_str(item.name))) if isinstance( item, Tensor) else item for item in op.inputs) inputs = tuple(inputs) if isinstance(op.inputs, tuple) else (list(inputs), ) outputs = (nnef.Identifier(as_str(item.name)) for item in op.outputs) outputs = tuple(outputs) if isinstance(op.outputs, tuple) else (list(outputs), ) attribs = { as_str(key): value for key, value in six.iteritems(op.attribs) } name = _next_version( op.type, versions ) if op.type not in nnef.StandardOperations and version_custom_ops else op.type dtype = attribs.get('dtype') if dtype is not None: dtype = _nnef_dtype(dtype) del attribs['dtype'] for key, value in six.iteritems(attribs): if isinstance(value, (type, np.dtype)): attribs[key] = _nnef_dtype(value) invocation = nnef.format_invocation(name=name, dtype=dtype, attribs=attribs, inputs=inputs, outputs=outputs) annotation = " # " + ", ".join(_nnef_dtype(output.dtype) + str(output.shape) for output in op.outputs) \ if annotate_shapes else '' print(" {};{}".format(invocation, annotation), file=file) print("}", file=file)
def transform(result_): assert isinstance( result_, NNEFTensor ), "Results must be NNEF tensors, or lists/tuples of that." return nnef.Identifier(result_.name)
def transform(arg): if isinstance(arg, dog.DataNode): return nnef.Identifier( utils.ensure_not_unicode_in_python2(arg.name)) else: return utils.ensure_not_unicode_in_python2(arg)
import nnef import numpy as np from collections import OrderedDict input = nnef.Tensor('input', dtype='scalar') filter = nnef.Tensor('filter', dtype='scalar', data=np.random.randn(32, 3, 5, 5)) output = nnef.Tensor('output', dtype='scalar') external = nnef.Operation('external', attribs={'shape': [1, 3, 224, 224]}, inputs=OrderedDict(), outputs=OrderedDict([('output', nnef.Identifier('input'))])) variable = nnef.Operation('variable', attribs={ 'shape': [32, 3, 5, 5], 'label': 'conv/filter' }, inputs=OrderedDict(), outputs=OrderedDict([('output', nnef.Identifier('filter'))])) conv = nnef.Operation('conv', attribs={}, inputs=OrderedDict([('input', nnef.Identifier('input')), ('filter', nnef.Identifier('filter')) ]), outputs=OrderedDict([('output', nnef.Identifier('output'))]))