Exemple #1
0
def model_data_type_to_np(model_dtype):
    from modelci.types.bo import DataType

    mapper = {
        DataType.TYPE_INVALID: None,
        DataType.TYPE_BOOL: np.bool,
        DataType.TYPE_UINT8: np.uint8,
        DataType.TYPE_UINT16: np.uint16,
        DataType.TYPE_UINT32: np.uint32,
        DataType.TYPE_UINT64: np.uint64,
        DataType.TYPE_INT8: np.int8,
        DataType.TYPE_INT16: np.int16,
        DataType.TYPE_INT32: np.int32,
        DataType.TYPE_INT64: np.int64,
        DataType.TYPE_FP16: np.float16,
        DataType.TYPE_FP32: np.float32,
        DataType.TYPE_FP64: np.float64,
        DataType.TYPE_STRING: np.dtype(object)
    }

    if isinstance(model_dtype, int):
        model_dtype = DataType(model_dtype)
    elif isinstance(model_dtype, str):
        model_dtype = DataType[model_dtype]
    elif not isinstance(model_dtype, DataType):
        raise TypeError(
            f'model_dtype is expecting one of the type: `int`, `str`, or `DataType` but got {type(model_dtype)}'
        )
    return mapper[model_dtype]
Exemple #2
0
def model_data_type_to_torch(model_dtype):
    from modelci.types.models.common import DataType
    import torch

    mapper = {
        DataType.TYPE_INVALID: None,
        DataType.TYPE_BOOL: torch.bool,
        DataType.TYPE_UINT8: torch.uint8,
        DataType.TYPE_INT8: torch.int8,
        DataType.TYPE_INT16: torch.int16,
        DataType.TYPE_INT32: torch.int32,
        DataType.TYPE_INT64: torch.int64,
        DataType.TYPE_FP16: torch.float16,
        DataType.TYPE_FP32: torch.float32,
        DataType.TYPE_FP64: torch.float64,
    }

    if isinstance(model_dtype, int):
        model_dtype = DataType(model_dtype)
    elif isinstance(model_dtype, str):
        model_dtype = DataType[model_dtype]
    elif not isinstance(model_dtype, DataType):
        raise TypeError(
            f'model_dtype is expecting one of the type: `int`, `str`, or `DataType` but got {type(model_dtype)}'
        )
    return mapper[model_dtype]
Exemple #3
0
    def grpc_decode(cls, buffer: Iterable, meta):
        meta = json.loads(meta)
        shape = meta['shape']
        dtype = model_data_type_to_np(DataType(meta['dtype']))

        decode_pipeline = compose(
            partial(np.reshape, newshape=shape),
            partial(np.fromstring, dtype=dtype),
        )

        buffer = list(map(decode_pipeline, buffer))

        buffer = np.stack(buffer)

        return buffer
Exemple #4
0
def model_data_type_to_onnx(model_dtype):
    mapper = {
        DataType.TYPE_INVALID: onnxconverter_common,
        DataType.TYPE_BOOL: onnxconverter_common.BooleanTensorType,
        DataType.TYPE_INT32: onnxconverter_common.Int32TensorType,
        DataType.TYPE_INT64: onnxconverter_common.Int64TensorType,
        DataType.TYPE_FP32: onnxconverter_common.FloatTensorType,
        DataType.TYPE_FP64: onnxconverter_common.DoubleTensorType,
        DataType.TYPE_STRING: onnxconverter_common.StringType,
    }

    if isinstance(model_dtype, int):
        model_dtype = DataType(model_dtype)
    elif isinstance(model_dtype, str):
        model_dtype = DataType[model_dtype]
    elif not isinstance(model_dtype, DataType):
        raise TypeError(
            f'model_dtype is expecting one of the type: `int`, `str`, or `DataType` but got {type(model_dtype)}'
        )
    return mapper[model_dtype]