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 4 commits
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
36 changes: 35 additions & 1 deletion src/sagemaker/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import csv
import io
import json

import numpy as np
from six import with_metaclass

Expand Down Expand Up @@ -357,3 +356,38 @@ 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 data 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):
try:
dataFile = open(data, "rb")
except Exception:
raise ValueError(f"{data} is not a valid file-path.")
Copy link
Member

Choose a reason for hiding this comment

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

Lets generalise the exception and not conclude not a valid file-path for better debugging.

try:
	dataFile = open(data, "rb")
	dataFileInfo = dataFile.read()
	dataFile.close()
except Exception as e:
         raise ValueError(f"Could not open/read file: {data}. {e.message}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated in the current revision.

dataFileInfo = dataFile.read()
dataFile.close()
return dataFileInfo
Copy link
Contributor

Choose a reason for hiding this comment

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

can you please move this in try block

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