|
13 | 13 | """Placeholder docstring""" |
14 | 14 | from __future__ import absolute_import |
15 | 15 |
|
16 | | -import io |
17 | 16 | import logging |
18 | 17 | import struct |
19 | 18 | import sys |
20 | 19 |
|
21 | 20 | import numpy as np |
22 | 21 |
|
23 | 22 | from sagemaker.amazon.record_pb2 import Record |
24 | | -from sagemaker.deprecations import deprecated_class |
25 | | -from sagemaker.deserializers import SimpleBaseDeserializer |
26 | | -from sagemaker.serializers import SimpleBaseSerializer |
27 | 23 | from sagemaker.utils import DeferredError |
28 | 24 |
|
29 | 25 |
|
30 | | -class RecordSerializer(SimpleBaseSerializer): |
31 | | - """Serialize a NumPy array for an inference request.""" |
32 | | - |
33 | | - def __init__(self, content_type="application/x-recordio-protobuf"): |
34 | | - """Initialize a ``RecordSerializer`` instance. |
35 | | -
|
36 | | - Args: |
37 | | - content_type (str): The MIME type to signal to the inference endpoint when sending |
38 | | - request data (default: "application/x-recordio-protobuf"). |
39 | | - """ |
40 | | - super(RecordSerializer, self).__init__(content_type=content_type) |
41 | | - |
42 | | - def serialize(self, data): |
43 | | - """Serialize a NumPy array into a buffer containing RecordIO records. |
44 | | -
|
45 | | - Args: |
46 | | - data (numpy.ndarray): The data to serialize. |
47 | | -
|
48 | | - Returns: |
49 | | - io.BytesIO: A buffer containing the data serialized as records. |
50 | | - """ |
51 | | - if len(data.shape) == 1: |
52 | | - data = data.reshape(1, data.shape[0]) |
53 | | - |
54 | | - if len(data.shape) != 2: |
55 | | - raise ValueError( |
56 | | - "Expected a 1D or 2D array, but got a %dD array instead." % len(data.shape) |
57 | | - ) |
58 | | - |
59 | | - buffer = io.BytesIO() |
60 | | - write_numpy_to_dense_tensor(buffer, data) |
61 | | - buffer.seek(0) |
62 | | - |
63 | | - return buffer |
64 | | - |
65 | | - |
66 | | -class RecordDeserializer(SimpleBaseDeserializer): |
67 | | - """Deserialize RecordIO Protobuf data from an inference endpoint.""" |
68 | | - |
69 | | - def __init__(self, accept="application/x-recordio-protobuf"): |
70 | | - """Initialize a ``RecordDeserializer`` instance. |
71 | | -
|
72 | | - Args: |
73 | | - accept (union[str, tuple[str]]): The MIME type (or tuple of allowable MIME types) that |
74 | | - is expected from the inference endpoint (default: |
75 | | - "application/x-recordio-protobuf"). |
76 | | - """ |
77 | | - super(RecordDeserializer, self).__init__(accept=accept) |
78 | | - |
79 | | - def deserialize(self, data, content_type): |
80 | | - """Deserialize RecordIO Protobuf data from an inference endpoint. |
81 | | -
|
82 | | - Args: |
83 | | - data (object): The protobuf message to deserialize. |
84 | | - content_type (str): The MIME type of the data. |
85 | | - Returns: |
86 | | - list: A list of records. |
87 | | - """ |
88 | | - try: |
89 | | - return read_records(data) |
90 | | - finally: |
91 | | - data.close() |
92 | | - |
93 | | - |
94 | 26 | def _write_feature_tensor(resolved_type, record, vector): |
95 | 27 | """Placeholder Docstring""" |
96 | 28 | if resolved_type == "Int32": |
@@ -288,7 +220,3 @@ def _resolve_type(dtype): |
288 | 220 | if dtype == np.dtype("float32"): |
289 | 221 | return "Float32" |
290 | 222 | raise ValueError("Unsupported dtype {} on array".format(dtype)) |
291 | | - |
292 | | - |
293 | | -numpy_to_record_serializer = deprecated_class(RecordSerializer, "numpy_to_record_serializer") |
294 | | -record_deserializer = deprecated_class(RecordDeserializer, "record_deserializer") |
0 commit comments