def make_const_node(data: Tensor, name: str = None) -> NodeDef:
    """
    Create a TF graph node containing a constant value.
    The resulting node is equivalent to using `tf.constant` on the
    default graph.

    Args:
        data: Numpy-array containing the data, shape, and datatype
        name: Optional name of the node

    Returns:
        Graph node for adding to a TF Graph instance
    """
    dtype = as_dtype(data.dtype).as_datatype_enum
    tensor_content = data.tobytes()
    tensor_dim = [TensorShapeProto.Dim(size=size) for size in data.shape]
    tensor_shape = TensorShapeProto(dim=tensor_dim)
    tensor_proto = TensorProto(tensor_content=tensor_content,
                               tensor_shape=tensor_shape,
                               dtype=dtype)
    node_def = NodeDef(op='Const',
                       name=name or 'Const',
                       attr={
                           'value': AttrValue(tensor=tensor_proto),
                           'dtype': AttrValue(type=dtype)
                       })
    return node_def
示例#2
0
def test_extract_shape():
    shape = (3, 1, 4)
    tensor_proto = TensorProto(
        dtype=types_pb2.DT_FLOAT,
        tensor_shape=TensorShapeProto(
            dim=[TensorShapeProto.Dim(size=n) for n in shape]),
    )

    result = extract_shape(tensor_proto)

    assert result == (3, 1, 4)
示例#3
0
def ndarray_to_tensor_proto(ndarray: np.ndarray) -> TensorProto:
    dtype = DataType(ndarray.dtype.type)
    proto = TensorProto(
        dtype=dtype.enum,
        tensor_shape=TensorShapeProto(
            dim=[TensorShapeProto.Dim(size=d) for d in ndarray.shape]),
    )
    proto = write_values_to_tensor_proto(tensor_proto=proto,
                                         values=ndarray.ravel(),
                                         dtype=dtype)
    return proto
示例#4
0
def test_write_values_to_tensor_proto_on_numerics():
    dtype = DataType(types_pb2.DT_FLOAT)
    array = np.array([0.314, 0.159, 0.268])
    tensor_proto = TensorProto(
        dtype=dtype.enum,
        tensor_shape=TensorShapeProto(
            dim=[TensorShapeProto.Dim(size=len(array))]))

    write_values_to_tensor_proto(tensor_proto, array.ravel(), dtype)

    np.testing.assert_almost_equal(tensor_proto.float_val,
                                   [0.314, 0.159, 0.268])
示例#5
0
 def _create_const_node(self, name, value):
     tensor_content = value.tobytes()
     dt = tf.as_dtype(value.dtype).as_datatype_enum
     tensor_shape = TensorShapeProto(
         dim=[TensorShapeProto.Dim(size=s) for s in value.shape])
     tensor_proto = TensorProto(tensor_content=tensor_content,
                                tensor_shape=tensor_shape,
                                dtype=dt)
     node = tf.compat.v1.NodeDef(name=name, op='Const',
                                 attr={'value': tf.compat.v1.AttrValue(tensor=tensor_proto),
                                       'dtype': tf.compat.v1.AttrValue(type=dt)})
     return node
示例#6
0
def test_write_values_to_tensor_proto_on_strings():
    dtype = DataType(types_pb2.DT_STRING)
    array = np.array(["Ceci", "n'est", "pas", "une", "string"])
    tensor_proto = TensorProto(
        dtype=dtype.enum,
        tensor_shape=TensorShapeProto(
            dim=[TensorShapeProto.Dim(size=len(array))]))

    write_values_to_tensor_proto(tensor_proto, array.ravel(), dtype)

    assert tensor_proto.string_val == [
        b"Ceci", b"n'est", b"pas", b"une", b"string"
    ]
示例#7
0
 def _create_const_node(self, name, value):
     for i in range(len(value)):
         fixed_name = name
         if i > 0:
             original_name = name + ":" + str(i)
             fixed_name = original_name.replace(":", "_")
             self._rename_node_dict[original_name] = fixed_name
         tensor_content = value[i].tobytes()
         dt = tf.as_dtype(value[i].dtype).as_datatype_enum
         tensor_shape = TensorShapeProto(
             dim=[TensorShapeProto.Dim(size=s) for s in value[i].shape])
         tensor_proto = TensorProto(tensor_content=tensor_content,
                                    tensor_shape=tensor_shape,
                                    dtype=dt)
         node = tf.compat.v1.NodeDef(
             name=fixed_name,
             op='Const',
             attr={
                 'value': tf.compat.v1.AttrValue(tensor=tensor_proto),
                 'dtype': tf.compat.v1.AttrValue(type=dt)
             })
         self.op_dict[fixed_name] = Operator(node)
def _gen_transpose_def(name, perm, input_node):
    perm_node = tf.NodeDef(name='{}/perm'.format(name), op='Const')
    perm_node.attr['dtype'].type = tf.int32.as_datatype_enum
    tensor_def = perm_node.attr['value'].tensor
    tensor_def.dtype = tf.int32.as_datatype_enum
    tensor_def.tensor_shape.dim.append(TensorShapeProto.Dim(size=4))
    tensor_def.tensor_content = np.array(perm, dtype=np.int32).tobytes()
    transpose_node = tf.NodeDef(name=name, op='Transpose')
    transpose_node.input.append(input_node.name)
    transpose_node.input.append(perm_node.name)
    transpose_node.attr['T'].type = input_node.attr['T'].type
    transpose_node.attr['Tperm'].type = tf.int32.as_datatype_enum
    return transpose_node, perm_node
def create_const_for_anchor_generator():
    """Creates a 'Const' node as an input to 'MultipleGridAnchorGenerator'
    Note the 'MultipleGridAnchorGenerator' TRT plugin node requires a
    [1.0, 1.0] array as input.
    Reference: https://stackoverflow.com/a/56296195/7596504
    """
    import numpy as np
    import tensorflow as tf
    from tensorflow.core.framework.tensor_pb2 import TensorProto
    from tensorflow.core.framework.tensor_shape_pb2 import TensorShapeProto

    value = np.array([1.0, 1.0], dtype=np.float32)
    dt = tf.as_dtype(value.dtype).as_datatype_enum
    tensor_shape = TensorShapeProto(
        dim=[TensorShapeProto.Dim(size=s) for s in value.shape])
    tensor_proto = TensorProto(tensor_content=value.tobytes(),
                               tensor_shape=tensor_shape,
                               dtype=dt)
    return tf.NodeDef(name='const_for_anchors',
                      op='Const',
                      attr={
                          'value': tf.AttrValue(tensor=tensor_proto),
                          'dtype': tf.AttrValue(type=dt)
                      })