def _onnx_graph_to_tensorflow_rep(cls, graph_def, opset, strict): """ Convert ONNX graph to TensorflowRep. :param graph_def: ONNX GraphProto object. :param opset: ONNX OperatorSetIdProto list. :param strict: whether to enforce semantic equivalence between the original model and the converted tensorflow model. :return: TensorflowRep object. """ handlers = cls._get_handlers(opset) # initializer: TensorProtos representing the values to initialize # a given tensor. # initialized: A list of names of the initialized tensors. if graph_def.initializer: initialized = {init.name for init in graph_def.initializer} else: initialized = set() module = BackendTFModule(handlers, opset, strict, graph_def, cls) signatures = dict() for value_info in graph_def.input: if value_info.name in initialized: continue shape = list(d.dim_value if ( d.dim_value > 0 and d.dim_param == "") else None for d in value_info.type.tensor_type.shape.dim) value_info_name = value_info.name.replace( ":", "_tf_") + "_" + get_unique_suffix( ) if ":" in value_info.name else value_info.name tf_spec = tf.TensorSpec( shape, data_type.onnx2tf(value_info.type.tensor_type.elem_type), value_info_name) signatures[value_info.name] = tf_spec tf_rep = TensorflowRep() tf_rep.inputs = [ value_info.name for value_info in graph_def.input if value_info.name not in initialized ] tf_rep.outputs = [value_info.name for value_info in graph_def.output] module.outputs = tf_rep.outputs tf_rep.tf_module = module tf_rep.signatures = signatures return tf_rep
def _onnx_graph_to_tensorflow_rep(cls, graph_def, opset, strict, **kwargs): """ Convert ONNX graph to TensorflowRep. :param graph_def: ONNX GraphProto object. :param opset: ONNX OperatorSetIdProto list. :param strict: whether to enforce semantic equivalence between the original model and the converted tensorflow model. :kwargs: additional arguements to generate tensor_dict for model debugging :return: TensorflowRep object. """ # To generate tensor_dict or not, default is False gen_tensor_dict = kwargs[ 'gen_tensor_dict'] if 'gen_tensor_dict' in kwargs else False # User provided input tensors, in the case the model inputs have unknown shapes input_tensor_dict = kwargs[ 'input_tensor_dict'] if 'input_tensor_dict' in kwargs else dict() handlers = cls._get_handlers(opset) # initializer: TensorProtos representing the values to initialize # a given tensor. # initialized: A list of names of the initialized tensors. if graph_def.initializer: initialized = {init.name for init in graph_def.initializer} else: initialized = set() input_dict = dict() module = BackendTFModule(handlers, opset, strict, graph_def, cls) signatures = dict() for value_info in graph_def.input: if value_info.name in initialized: continue shape = list(d.dim_value if ( d.dim_value > 0 and d.dim_param == "") else None for d in value_info.type.tensor_type.shape.dim) value_info_name = value_info.name.replace( ":", "_tf_") + "_" + get_unique_suffix( ) if ":" in value_info.name else value_info.name tf_spec = tf.TensorSpec( shape, data_type.onnx2tf(value_info.type.tensor_type.elem_type), value_info_name) signatures[value_info.name] = tf_spec if gen_tensor_dict: x = tf.constant( 0, dtype=data_type.onnx2tf( value_info.type.tensor_type.elem_type), name=value_info_name, shape=shape ) if value_info.name not in input_tensor_dict else input_tensor_dict[ value_info.name] input_dict[value_info.name] = x tf_rep = TensorflowRep() tf_rep.inputs = [ value_info.name for value_info in graph_def.input if value_info.name not in initialized ] tf_rep.outputs = [value_info.name for value_info in graph_def.output] module.outputs = tf_rep.outputs tf_rep.tf_module = module tf_rep.signatures = signatures tf_rep.tensor_dict = module.gen_tensor_dict( input_dict) if gen_tensor_dict else None tf_rep.onnx_op_list = cls._get_onnx_op_list(graph_def) return tf_rep
def _onnx_graph_to_tensorflow_rep(cls, graph_def, opset, strict, **kwargs): """ Convert ONNX graph to TensorflowRep. :param graph_def: ONNX GraphProto object. :param opset: ONNX OperatorSetIdProto list. :param strict: whether to enforce semantic equivalence between the original model and the converted tensorflow model. :kwargs: additional arguements to generate tensor_dict for model debugging :return: TensorflowRep object. """ # To generate tensor_dict or not, default is False gen_tensor_dict = kwargs[ 'gen_tensor_dict'] if 'gen_tensor_dict' in kwargs else False # User provided input tensors, in the case the model inputs have unknown shapes input_tensor_dict = kwargs[ 'input_tensor_dict'] if 'input_tensor_dict' in kwargs else dict() training_mode = kwargs[ 'training_mode'] if 'training_mode' in kwargs else False handlers = cls._get_handlers(opset) # initializer: TensorProtos representing the values to initialize # a given tensor. # initialized: A list of names of the initialized tensors. if graph_def.initializer: initialized = {init.name for init in graph_def.initializer} else: initialized = set() input_dict = dict() module = BackendTFModule(handlers, opset, strict, graph_def, cls) signatures = dict() tf_rep_graph = tf.Graph() with tf_rep_graph.as_default(): for value_info in graph_def.input: if value_info.name in initialized: continue shape = list(d.dim_value if ( d.dim_value > 0 and d.dim_param == "") else None for d in value_info.type.tensor_type.shape.dim) value_info_name = value_info.name.replace( ":", "_tf_") + "_" + get_unique_suffix( ) if ":" in value_info.name else value_info.name tf_spec = tf.TensorSpec( shape, data_type.onnx2tf(value_info.type.tensor_type.elem_type), value_info_name) signatures[value_info.name] = tf_spec if gen_tensor_dict or training_mode: x = tf.compat.v1.placeholder( data_type.onnx2tf( value_info.type.tensor_type.elem_type), name=value_info_name, shape=shape ) if value_info.name not in input_tensor_dict else input_tensor_dict[ value_info.name] input_dict[value_info.name] = x if gen_tensor_dict or training_mode: input_dict_items = cls._onnx_initializer_to_input_dict_items( graph_def.initializer, training_mode=True) tensor_dict = dict(input_dict) tensor_dict.update(input_dict_items) tensor_dict[ training_flag_name] = tf.compat.v1.placeholder_with_default( False, shape=[]) for node in graph_def.node: onnx_node = OnnxNode(node) output_ops = cls._onnx_node_to_tensorflow_op(onnx_node, tensor_dict, handlers, opset=opset, strict=strict) curr_node_output_map = dict( zip(onnx_node.outputs, output_ops)) tensor_dict.update(curr_node_output_map) tf_rep = TensorflowRep() tf_rep.inputs = [ value_info.name for value_info in graph_def.input if value_info.name not in initialized ] tf_rep.outputs = [value_info.name for value_info in graph_def.output] module.outputs = tf_rep.outputs tf_rep.tf_module = module tf_rep.signatures = signatures if gen_tensor_dict or training_mode: tf_rep.tensor_dict = tensor_dict if training_mode: tf_rep.graph = tf_rep_graph tf_rep.onnx_op_list = cls._get_onnx_op_list(graph_def) return tf_rep