Пример #1
0
    def _build_param_ops(self, onnx_graph, ugraph, op_types_cnt,
                         tensor_names_map):
        """Find all tensors in initialization list in onnx_graph, normally constants

    Note that this method will update op_types_cnt and tensor_names_map **inplace**
    """
        # find Const ops
        params_dict = {}
        # FIXME: avoid using internal api of other library
        dict_items = _onnx_initializer_to_input_dict_items(
            onnx_graph.initializer)
        for name, tf_tensor in dict_items:
            params_dict[name] = AttrValueConverter.GenericType(
                value_name='value',
                value=TensorProtoConverter.get_generic_value(
                    tf_tensor.op.get_attr('value')))
        # build Const ops
        for tensor_name, tensor_value in params_dict.items():
            cnt = op_types_cnt['Const']
            node_name = self._format_node_name(tensor_name, 'Const', cnt)
            op_types_cnt['Const'] += 1
            tensor_names_map[tensor_name] = TensorInfo(
                name=self._format_tensor_name('', node_name, 0),
                op_name=node_name,
                dtype=tensor_value.value.dtype,
                shape=list(tensor_value.value.np_array.shape),
                ugraph=ugraph)
            OperationInfo(name=node_name,
                          input_tensors=[],
                          output_tensors=[tensor_names_map[tensor_name]],
                          op_type='Const',
                          lib_name='onnx',
                          ugraph=ugraph,
                          op_attr={'value': tensor_value})
Пример #2
0
 def parse(self, txt_file, output_nodes=None):
     graph_name, _ = os.path.splitext(
         os.path.basename(txt_file)
     )
     if output_nodes is None:
         output_nodes = []
     add_all_nodes = not output_nodes
     ugraph = uTensorGraph(name=graph_name, output_nodes=output_nodes, lib_name='txtlib')
     with open(txt_file, 'r') as fid:
         for line in fid:
             try:
                 op_name, value = line.split(' ', maxsplit=1)
             except Exception:
                 raise ValueError('invalid line: {}'.format(line))
             value = np.array(eval(value))
             out_tensor = TensorInfo(
                 '{}:0'.format(op_name),
                 op_name,
                 dtype=value.dtype,
                 shape=list(value.shape),
                 ugraph=ugraph
             )
             op_info = OperationInfo(
                 name=op_name,
                 lib_name='txtlib',
                 ugraph=ugraph,
                 input_tensors=[],
                 output_tensors=[out_tensor],
                 op_type='Const',
                 op_attr={
                     "value": AttrValueConverter.GenericType(
                         value_name="tensor",
                         value=GenericTensorConverterMixin.GenericType(
                             np_array=value
                         ),
                     )
                 }
             )
             if add_all_nodes:
                 ugraph.output_nodes.append(op_name)
     topologic_order_graph(ugraph)
     return ugraph
Пример #3
0
    def _build_param_ops(self, fb_model, ugraph, tensor_names_map):
        """Const tensors are identified by buffer_index == 0. These tensors are converted to Const Op and added to ugraph
    """
        subgraph = self._get_tflm_get_subgraph(fb_model)

        for idx in range(0, subgraph.TensorsLength()):
            tensor = subgraph.Tensors(idx)
            buffer_index = tensor.Buffer()

            # buffer_index == 0 if intermediate
            if buffer_index == 0:
                continue

            node_name = re.sub(r':\d+', '',
                               tensor_names_map[idx].name) + "_Const"
            dtype = tensor_names_map[idx].dtype

            buffer_array = fb_model.Buffers(buffer_index).DataAsNumpy()
            if isinstance(buffer_array, int):
                continue  # somehow, sometimes, the buffer contains no data, likely to be an intermediate tensor
            buffer_content = fb_model.Buffers(buffer_index).DataAsNumpy().view(
                dtype).reshape(tensor_names_map[idx].shape)

            OperationInfo(
                name=node_name,
                input_tensors=[],
                output_tensors=[tensor_names_map[idx]],
                op_type="Const",
                lib_name="tflm",
                ugraph=ugraph,
                op_attr={
                    "value":
                    AttrValueConverter.GenericType(
                        value_name="tensor",
                        value=GenericTensorConverterMixin.GenericType(
                            np_array=buffer_content),
                    )
                },
            )

            self._set_tensor_node(idx, node_name, tensor_names_map)
Пример #4
0
def test_placeholder_value(placeholder_attr_value):
    generic = AttrValueConverter.get_generic_value(placeholder_attr_value)
    assert isinstance(generic, AttrValueConverter.__utensor_generic_type__)
    tf_proto = AttrValueConverter.get_tf_value(generic)
    assert isinstance(tf_proto, AttrValueConverter.__tfproto_type__)