Пример #1
0
def ArrayToDatum(arr):
    """Converts NumPy array to DatumProto.

  Supports arrays of types:
    - float16 (it is converted into a float32 in DatumProto)
    - float32
    - float64 (it is converted into a float32 in DatumProto)
    - uint8 (it is converted into a uint32 in DatumProto)
    - uint16 (it is converted into a uint32 in DatumProto)
    - uint32
    - uint64 (it is converted into a uint32 in DatumProto)

  Args:
    arr: NumPy array of arbitrary shape.

  Returns:
    datum: DatumProto object.

  Raises:
    ValueError: If array type is unsupported.
  """
    datum = datum_pb2.DatumProto()
    if arr.dtype in ('float16', 'float32', 'float64'):
        datum.float_list.value.extend(arr.astype('float32').flat)
    elif arr.dtype in ('uint8', 'uint16', 'uint32', 'uint64'):
        datum.uint32_list.value.extend(arr.astype('uint32').flat)
    else:
        raise ValueError('Unsupported array type: %s' % arr.dtype)

    datum.shape.dim.extend(arr.shape)
    return datum
Пример #2
0
def ParseFromString(string):
    """Converts serialized DatumProto string to numpy array.

  Args:
    string: Serialized DatumProto string.

  Returns:
    Numpy array.
  """
    datum = datum_pb2.DatumProto()
    datum.ParseFromString(string)
    return DatumToArray(datum)
Пример #3
0
def ArrayToDatum(arr):
    """Converts numpy array to DatumProto.

  Args:
    arr: Numpy array of arbitrary shape.

  Returns:
    datum: DatumProto object.
  """
    datum = datum_pb2.DatumProto()
    datum.float_list.value.extend(arr.astype(float).flat)
    datum.shape.dim.extend(arr.shape)
    return datum