コード例 #1
0
    def _perform_batch_inference(self, input_data, output_data, **kwargs):
        """Perform batch inference on the given input data.

        Transforms the input data to feed the serving container. It first gathers
        the files from S3 or Local FileSystem. It then splits the files as required
        (Line, RecordIO, None), and finally, it batch them according to the batch
        strategy and limit the request size.

        Args:
            input_data: Input data source.
            output_data: Output data source.
            **kwargs: Additional configuration arguments.
        """
        batch_strategy = kwargs["BatchStrategy"]
        max_payload = int(kwargs["MaxPayloadInMB"])
        data_source, batch_provider = self._prepare_data_transformation(
            input_data, batch_strategy)

        # Output settings
        accept = output_data["Accept"] if "Accept" in output_data else None

        working_dir = self._get_working_directory()
        dataset_dir = data_source.get_root_dir()

        for fn in data_source.get_file_list():

            relative_path = os.path.dirname(os.path.relpath(fn, dataset_dir))
            filename = os.path.basename(fn)
            copy_directory_structure(working_dir, relative_path)
            destination_path = os.path.join(working_dir, relative_path,
                                            filename + ".out")

            with open(destination_path, "wb") as f:
                for item in batch_provider.pad(fn, max_payload):
                    # call the container and add the result to inference.
                    response = self.local_session.sagemaker_runtime_client.invoke_endpoint(
                        item, "", input_data["ContentType"], accept)

                    response_body = response["Body"]
                    data = response_body.read()
                    response_body.close()
                    f.write(data)
                    if "AssembleWith" in output_data and output_data[
                            "AssembleWith"] == "Line":
                        f.write(b"\n")

        move_to_destination(working_dir, output_data["S3OutputPath"],
                            self.name, self.local_session)
        self.container.stop_serving()
コード例 #2
0
    def _perform_batch_inference(self, input_data, output_data, **kwargs):
        # Transform the input data to feed the serving container. We need to first gather the files
        # from S3 or Local FileSystem. Split them as required (Line, RecordIO, None) and finally batch them
        # according to the batch strategy and limit the request size.

        batch_strategy = kwargs['BatchStrategy']
        max_payload = int(kwargs['MaxPayloadInMB'])
        data_source, batch_provider = self._prepare_data_transformation(
            input_data, batch_strategy)

        # Output settings
        accept = output_data['Accept'] if 'Accept' in output_data else None

        working_dir = self._get_working_directory()
        dataset_dir = data_source.get_root_dir()

        for file in data_source.get_file_list():

            relative_path = os.path.dirname(os.path.relpath(file, dataset_dir))
            filename = os.path.basename(file)
            copy_directory_structure(working_dir, relative_path)
            destination_path = os.path.join(working_dir, relative_path,
                                            filename + '.out')

            with open(destination_path, 'wb') as f:
                for item in batch_provider.pad(file, max_payload):
                    # call the container and add the result to inference.
                    response = self.local_session.sagemaker_runtime_client.invoke_endpoint(
                        item, '', input_data['ContentType'], accept)

                    response_body = response['Body']
                    data = response_body.read().strip()
                    response_body.close()
                    f.write(data)
                    if 'AssembleWith' in output_data and output_data[
                            'AssembleWith'] == 'Line':
                        f.write(b'\n')

        move_to_destination(working_dir, output_data['S3OutputPath'],
                            self.name, self.local_session)
        self.container.stop_serving()