Not able to read Sagemaker Semantic Segmentation Model Batch Transformation Output file

Viewed 206

Currently I have deployed a Semantic Segmentation model and an endpoint with which I am able to invoke and get inferences. Now, I am getting inferences for each image at a time.

Now I want to try batch of images at a time using Batch Transform job. It worked perfectly fine but the images that is created is an .out file and I'm not able to open that file using any of the viz library like matplotlib imread, PIL Image and openCV imread. It all says not an image.

Just wanted to understand what is the .out file ? and if it is an segmented mask image which is typically the output of a semantic segmentation model then how can I read that file.

My code for Batch Transformation:

from sagemaker.predictor import RealTimePredictor, csv_serializer, csv_deserializer

class Predictor(RealTimePredictor):
    
    def __init__(self, endpoint_name, sagemaker_session=None):
        super(Predictor, self).__init__(
            endpoint_name, sagemaker_session, csv_serializer, csv_deserializer
        )

ss_model = sagemaker.model.Model(role =role, image=training_image, model_data = model, predictor_cls=Predictor, sagemaker_session=sess)

transformer = ss_model.transformer(instance_count=1, instance_type='ml.c4.xlarge', output_path=batch_output_data)

transformer.transform(data=batch_input_data, data_type='S3Prefix', content_type='image/png', split_type='None')

transformer.wait()
2 Answers

As the official doc say:

The SageMaker semantic segmentation algorithm provides a fine-grained, pixel-level approach to developing computer vision applications. It tags every pixel in an image with a class label from a predefined set of classes.

...

Because the semantic segmentation algorithm classifies every pixel in an image, it also provides information about the shapes of the objects contained in the image. The segmentation output is represented as a grayscale image, called a segmentation mask. A segmentation mask is a grayscale image with the same shape as the input image.

So, what the .out file has are pixel arrays (assigned class for each pixel). You need to deserialize the file:

from PIL import Image
import numpy as np
import io
from boto3.session import Session

session = Session(
    aws_access_key_id=KEY_ID, aws_secret_access_key=ACCESS_KEY, region_name=REGION_NAME
)
s3 = session.resource("s3")
out_file = io.BytesIO()
s3_object = s3.Object(BUCKET, PATH)
s3_object.download_fileobj(out_file)
out_file.seek(0)

mask = np.array(Image.open(out_file))

Also, I found a class ImageDeserializer in this doc to do the job with a stream of data. Maybe you can adapt it to your file because it read a stream of bytes returned from an inference endpoint (batch transform put this data in the .out file).

what is the .out file?

.out file essentially is the stream(content) in your http response HttpEntity(including header and body). It matches the order of the records in your input file.

To be more specific, for each batch transform, SageMaker splits your input file into records, construct multiple http requests and send them to the model server. The inference result is included in the http response. SageMaker upload the inference results to s3 with ".out" suffix.

Related