Skip to content

feature: Data Serializer #2956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/sagemaker/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import csv
import io
import json

import os
import numpy as np
from six import with_metaclass

Expand Down Expand Up @@ -357,3 +357,35 @@ def serialize(self, data):
return data.read()

raise ValueError("Unable to handle input format: %s" % type(data))


class DataSerializer(SimpleBaseSerializer):
"""Serialize data in any file by extracting raw bytes from the file."""

def __init__(self, content_type="file-path/raw-bytes"):
"""Initialize a ``DataSerializer`` instance.

Args:
content_type (str): The MIME type to signal to the inference endpoint when sending
request data (default: "file-path/raw-bytes").
"""
super(DataSerializer, self).__init__(content_type=content_type)

def serialize(self, data):
"""Serialize file of various formats to a raw bytes.

Args:
data (object): Data to be serialized. The data can be a string,
representing file-path or the raw bytes from a file.
Returns:
raw-bytes: The data serialized as a raw-bytes from the input.
"""
if isinstance(data, str):
if not os.path.exists(data):
raise ValueError(f"{data} is not a valid file path.")
image = open(data, "rb")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please close file here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Exception handling

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in current revision.

return image.read()
if isinstance(data, bytes):
return data

raise ValueError(f"Object of type {type(data)} is not Data serializable.")
Binary file added tests/data/cuteCat.raw
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/unit/sagemaker/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SparseMatrixSerializer,
JSONLinesSerializer,
LibSVMSerializer,
DataSerializer,
)
from tests.unit import DATA_DIR

Expand Down Expand Up @@ -331,3 +332,26 @@ def test_libsvm_serializer_file_like(libsvm_serializer):
libsvm_file.seek(0)
result = libsvm_serializer.serialize(libsvm_file)
assert result == validation_data


@pytest.fixture
def data_serializer():
return DataSerializer()


def test_data_serializer_raw(data_serializer):
input_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.jpg")
with open(input_image_file_path, "rb") as image:
input_image = image.read()
input_image_data = data_serializer.serialize(input_image)
validation_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.raw")
validation_image_data = open(validation_image_file_path, "rb").read()
assert input_image_data == validation_image_data


def test_data_serializer_file_like(data_serializer):
input_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.jpg")
validation_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.raw")
input_image_data = data_serializer.serialize(input_image_file_path)
validation_image_data = open(validation_image_file_path, "rb").read()
assert input_image_data == validation_image_data