sagemaker.core.deserializers.base#
Implements base methods for deserializing data returned from an inference endpoint.
Classes
Abstract base class for creation of new deserializers. |
|
|
Deserialize a stream of bytes into a bytes object. |
|
Deserialize a stream of bytes into a list of lists. |
|
Deserialize JSON data from an inference endpoint into a Python object. |
|
Deserialize JSON lines data from an inference endpoint. |
|
Deserialize a stream of data in .npy, .npz or UTF-8 CSV/JSON format to a numpy array. |
|
Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe. |
|
Deserialize RecordIO Protobuf data from an inference endpoint. |
|
Abstract base class for creation of new deserializers. |
|
Directly return the data and content-type received from an inference endpoint. |
|
Deserialize data from an inference endpoint into a decoded string. |
|
Deserialize stream to torch.Tensor. |
- class sagemaker.core.deserializers.base.BaseDeserializer[source]#
Bases:
ABCAbstract base class for creation of new deserializers.
Provides a skeleton for customization requiring the overriding of the method deserialize and the class attribute ACCEPT.
- abstract property ACCEPT#
The content types that are expected from the inference endpoint.
- abstract deserialize(stream, content_type)[source]#
Deserialize data received from an inference endpoint.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
The data deserialized into an object.
- Return type:
object
- class sagemaker.core.deserializers.base.BytesDeserializer(accept='*/*')[source]#
Bases:
SimpleBaseDeserializerDeserialize a stream of bytes into a bytes object.
- class sagemaker.core.deserializers.base.CSVDeserializer(encoding='utf-8', accept='text/csv')[source]#
Bases:
SimpleBaseDeserializerDeserialize a stream of bytes into a list of lists.
Consider using :class:~`sagemaker.deserializers.NumpyDeserializer` or :class:~`sagemaker.deserializers.PandasDeserializer` instead, if you’d like to convert text/csv responses directly into other data types.
- deserialize(stream, content_type)[source]#
Deserialize data from an inference endpoint into a list of lists.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
- The data deserialized into a list of lists representing the
contents of a CSV file.
- Return type:
list
- class sagemaker.core.deserializers.base.JSONDeserializer(accept='application/json')[source]#
Bases:
SimpleBaseDeserializerDeserialize JSON data from an inference endpoint into a Python object.
- deserialize(stream, content_type)[source]#
Deserialize JSON data from an inference endpoint into a Python object.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
The JSON-formatted data deserialized into a Python object.
- Return type:
object
- class sagemaker.core.deserializers.base.JSONLinesDeserializer(accept='application/jsonlines')[source]#
Bases:
SimpleBaseDeserializerDeserialize JSON lines data from an inference endpoint.
- deserialize(stream, content_type)[source]#
Deserialize JSON lines data from an inference endpoint.
See https://docs.python.org/3/library/json.html#py-to-json-table to understand how JSON values are converted to Python objects.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
A list of JSON serializable objects.
- Return type:
list
- class sagemaker.core.deserializers.base.NumpyDeserializer(dtype=None, accept='application/x-npy', allow_pickle=False)[source]#
Bases:
SimpleBaseDeserializerDeserialize a stream of data in .npy, .npz or UTF-8 CSV/JSON format to a numpy array.
Note that when using application/x-npz archive format, the result will usually be a dictionary-like object containing multiple arrays (as per
numpy.load()) - instead of a single array.- deserialize(stream, content_type)[source]#
Deserialize data from an inference endpoint into a NumPy array.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
The data deserialized into a NumPy array.
- Return type:
numpy.ndarray
- class sagemaker.core.deserializers.base.PandasDeserializer(accept=('text/csv', 'application/json'))[source]#
Bases:
SimpleBaseDeserializerDeserialize CSV or JSON data from an inference endpoint into a pandas dataframe.
- deserialize(stream, content_type)[source]#
Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe.
If the data is JSON, the data should be formatted in the ‘columns’ orient. See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
The data deserialized into a pandas DataFrame.
- Return type:
pandas.DataFrame
- class sagemaker.core.deserializers.base.RecordDeserializer(accept='application/x-recordio-protobuf')[source]#
Bases:
SimpleBaseDeserializerDeserialize RecordIO Protobuf data from an inference endpoint.
- class sagemaker.core.deserializers.base.SimpleBaseDeserializer(accept='*/*')[source]#
Bases:
BaseDeserializerAbstract base class for creation of new deserializers.
This class extends the API of :class:~`sagemaker.deserializers.BaseDeserializer` with more user-friendly options for setting the ACCEPT content type header, in situations where it can be provided at init and freely updated.
- property ACCEPT#
The tuple of possible content types that are expected from the inference endpoint.
- class sagemaker.core.deserializers.base.StreamDeserializer(accept='*/*')[source]#
Bases:
SimpleBaseDeserializerDirectly return the data and content-type received from an inference endpoint.
It is the user’s responsibility to close the data stream once they’re done reading it.
- deserialize(stream, content_type)[source]#
Returns a stream of the response body and the MIME type of the data.
- Parameters:
stream (botocore.response.StreamingBody) – A stream of bytes.
content_type (str) – The MIME type of the data.
- Returns:
A two-tuple containing the stream and content-type.
- Return type:
tuple
- class sagemaker.core.deserializers.base.StringDeserializer(encoding='UTF-8', accept='application/json')[source]#
Bases:
SimpleBaseDeserializerDeserialize data from an inference endpoint into a decoded string.
- deserialize(stream, content_type)[source]#
Deserialize data from an inference endpoint into a decoded string.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
The data deserialized into a decoded string.
- Return type:
str
- class sagemaker.core.deserializers.base.TorchTensorDeserializer(accept='tensor/pt')[source]#
Bases:
SimpleBaseDeserializerDeserialize stream to torch.Tensor.
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
The data deserialized into a torch Tensor.
- Return type:
torch.Tensor
- deserialize(stream, content_type='tensor/pt')[source]#
Deserialize streamed data to TorchTensor
See https://pytorch.org/docs/stable/generated/torch.from_numpy.html
- Parameters:
stream (botocore.response.StreamingBody) – Data to be deserialized.
content_type (str) – The MIME type of the data.
- Returns:
A list of TorchTensor serializable objects.
- Return type:
list