4

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()
min2bro
  • 4,509
  • 5
  • 29
  • 55
  • Were you able to figure this out? – Fried Noodles Apr 14 '21 at 10:53
  • No, I am using inference on single image only and running that inside a loop for batch, Are you facing the same issue too? – min2bro Apr 14 '21 at 11:04
  • Yep facing the same issue. How did you do the inference for one image only? I saw your other post but im getting the same error "Parameter 'head.psp.conv1.0.weight' is missing in file 'model_new/model_algo-1'," I can't figure out how to get the output in "png" and not ".out" – Fried Noodles Apr 14 '21 at 11:24
  • As per documentation ".out" files are CSV-type resultant files with data and pridicted labels as well. Try opening and Investigating with a text editing application – Avinash Karhana Apr 29 '21 at 19:26

2 Answers2

0

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).

Franco Morero
  • 549
  • 5
  • 20
-1

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.