def get_shape_for(name): if isinstance(source_shapes, dict) and name in source_shapes: return source_shapes[name] elif isinstance(source_shapes, list): return list(source_shapes) elif utils.is_anyint(source_shapes): return utils.anyint_to_int(source_shapes) return None
def get_shape_for(name): if isinstance(input_shape, dict) and name in input_shape: return input_shape[name] elif isinstance(input_shape, list): return list(input_shape) elif utils.is_anyint(input_shape): return utils.anyint_to_int(input_shape) return None
def _get_attribute(field, value, graph): if field == 'i' or field == 'f' or field == 'b' or field == 'placeholder': if utils.is_anyint(value): return utils.anyint_to_int(value) return value elif field == 's': return utils.anystr_to_str(value.decode()) elif field == 'shape': return _get_shape(value) elif field == 'type': return _get_dtype(value) elif field == 'tensor': return _get_tensor(value, graph) elif field == 'func': return _get_func(value) elif field == 'list': field, items = _get_nonempty_items(value, fields=['i', 'f', 'b', 's', 'shape', 'type', 'tensor', 'func']) if items is None: return [] return [_get_attribute(field, item, graph) for item in items] assert False
def _get_random_feed_dict(input_name_shape_dtypes): feed_dict = {} for input_name, input_shape, input_dtype in input_name_shape_dtypes: if utils.is_anyint(input_dtype): input_dtype = dtype_id_to_name(input_dtype) if input_dtype == 'FLOAT': feed_dict[input_name] = np.random.random(input_shape).astype( np.float32) - 0.5 elif input_dtype == 'INT8': feed_dict[input_name] = np.random.randint( -128, 128, input_shape, np.int8) elif input_dtype == 'UINT8': feed_dict[input_name] = np.random.randint( 0, 256, input_shape, np.uint8) elif input_dtype == 'INT32': feed_dict[input_name] = np.random.randint( 0, 1000, input_shape, np.int32) elif input_dtype == 'BOOL': feed_dict[input_name] = np.random.random(input_shape) > 0.5 else: assert False return feed_dict
def _normalize_types(arg): if utils.is_anyint(arg): return utils.anyint_to_int(arg) elif utils.is_anystr(arg): return utils.anystr_to_str(arg) elif isinstance(arg, np.ndarray): return arg.tolist() elif isinstance(arg, tf.TensorShape): if arg.dims is None: return None return [None if dim is None else int(dim) for dim in arg.as_list()] elif isinstance(arg, tf.Dimension): return arg.value elif isinstance(arg, tf.DType): return arg.name elif isinstance( arg, (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64, np.float16, np.float32, np.float64, np.bool_)): return arg.item() else: return arg
def fix_types(list_): # type: (typing.Any)->typing.Any if isinstance(list_, list) and len(list_) >= 1 and utils.is_anyint(list_[0]): list_ = [utils.anyint_to_int(i) for i in list_] return list_
def _get_shape(shape_proto): return [utils.anyint_to_int(dim.size) if utils.is_anyint(dim.size) else dim.size for dim in shape_proto.dim]
def _test_network(self, path, source_shape=1, ignore_extra_outputs=False): assert utils.is_anyint(source_shape) or isinstance( source_shape, (list, tuple)) network = os.path.basename(path.rsplit('/', 2)[1]) command = """ ./nnef_tools/convert.py --input-format=tensorflow-pb \\ --input-model={path} \\ --output-format=nnef \\ --output-model=out/nnef/{network}.nnef.tgz \\ --compress \\ --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 \\ {nchw_opt}""".format( network=network, nchw_str=prefer_nchw_str, nchw_opt=prefer_nchw_opt) 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 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() self._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() } old_names = [ placeholder.name for placeholder in get_placeholders() ] outputs = self._run_tfpb(path, feed_dict) tf.reset_default_graph() path2 = os.path.join('out', 'tensorflow-pb' + prefer_nchw_str, network + ".pb") self._set_default_graph_from_pb(path2) feed_dict2 = { placeholder.name: feed_dict[old_names[i]] for i, placeholder in enumerate(get_placeholders()) } outputs2 = self._run_tfpb(path2, feed_dict2) if ignore_extra_outputs: outputs2 = outputs2[-len(outputs):] self.assertTrue(len(outputs) == len(outputs2)) for a, b in zip(outputs, outputs2): if a.dtype == np.bool: self.assertTrue(np.all(a == b)) else: self.assertTrue(np.all(np.isfinite(a))) self.assertTrue(np.all(np.isfinite(b))) self.assertTrue(np.allclose(a, b, atol=1e-5))
def _get_graph(graph_proto): graph = ONNXGraph(name=_fixstr(_get_field(graph_proto, 'name'))) if graph.name and not (graph.name[0].isalpha() or graph.name[0] == '_'): graph.name = 'graph_' + graph.name tensors_by_name = {} for node in graph_proto.node: for tensor_name in node.output: tensor_name = _fixstr(tensor_name) if tensor_name not in tensors_by_name: tensors_by_name[tensor_name] = ONNXTensor(graph=graph, name=tensor_name) for value_info in graph_proto.input: tensor_name = _fixstr(value_info.name) if tensor_name not in tensors_by_name: tensors_by_name[tensor_name] = ONNXTensor(graph=graph, name=tensor_name) for value_info in graph_proto.output: tensor_name = _fixstr(value_info.name) if tensor_name not in tensors_by_name: tensors_by_name[tensor_name] = ONNXTensor(graph=graph, name=tensor_name) for value_info in graph_proto.value_info: tensor_name = _fixstr(value_info.name) if tensor_name not in tensors_by_name: tensors_by_name[tensor_name] = ONNXTensor(graph=graph, name=tensor_name) for tensor_proto in graph_proto.initializer: tensor_name = _fixstr(tensor_proto.name) if tensor_name not in tensors_by_name: tensors_by_name[tensor_name] = ONNXTensor(graph=graph, name=tensor_name) const_or_var_names = { _fixstr(model_proto.name) for model_proto in graph_proto.initializer } input_names = [ _fixstr(value_info.name) for value_info in graph_proto.input if _fixstr(value_info.name) not in const_or_var_names ] output_names = [ _fixstr(value_info.name) for value_info in graph_proto.output ] graph.inputs = [tensors_by_name[name] for name in input_names] graph.outputs = [tensors_by_name[name] for name in output_names] for value_info in graph_proto.input: name, shape, dtype, doc_string = _get_value_info(value_info) tensor = tensors_by_name[name] tensor.shape, tensor.dtype = shape, dtype for value_info in graph_proto.output: name, shape, dtype, doc_string = _get_value_info(value_info) tensor = tensors_by_name[name] tensor.shape, tensor.dtype = shape, dtype for value_info in graph_proto.value_info: name, shape, dtype, doc_string = _get_value_info(value_info) tensor = tensors_by_name[name] tensor.shape, tensor.dtype = shape, dtype for tensor_proto in graph_proto.initializer: name, shape, dtype, data, doc_string = _get_tensor(tensor_proto) tensor = tensors_by_name[name] tensor.shape, tensor.dtype, tensor.data = shape, dtype, data for node in graph_proto.node: if _fixstr(node.op_type) == 'Constant': inputs, outputs, name, domain, op_type, attributes, doc_string = _get_node( node) if len(outputs) != 1: raise ParseException( 'Constant must have one output, we have: {}'.format( len(outputs))) tensor = tensors_by_name[outputs[0]] _name, tensor.shape, tensor.dtype, tensor.data, _doc_string = attributes[ 'value'] if not tensor.shape: tensor.data = tensor.data.flatten().tolist() if utils.is_anyint(tensor.data[0]): tensor.data[0] = utils.anyint_to_int(tensor.data[0]) else: inputs, outputs, name, domain, op_type, attributes, doc_string = _get_node( node) if op_type == 'ConstantOfShape': if 'value' in attributes: _tensor_name, _tensor_shape, tensor_dtype, tensor_data, _tensor_doc_string = attributes[ 'value'] attributes['dtype'] = tensor_dtype attributes['value'] = (utils.anyint_to_int( tensor_data.item()) if 'INT' in tensor_dtype else tensor_data.item()) else: attributes['dtype'] = 'FLOAT' attributes['value'] = 0.0 elif op_type == 'ConstantFill': attributes['dtype'] = _get_dtype(attributes['dtype']) ONNXOperation(graph=graph, name=op_type, inputs=tuple([ tensors_by_name[name] if name else ONNXTensor.create_null(graph) for name in inputs ]), outputs=tuple([ tensors_by_name[name] if name else ONNXTensor.create_null(graph) for name in outputs ]), attribs=attributes) return graph
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)