示例#1
0
def test_encode(content_type):
    mock_encoder = Mock()
    with patch.dict(encoder._encoder_map, {content_type: mock_encoder},
                    clear=True):
        encoder.encode(42, content_type)

        mock_encoder.assert_called_once_with(42)
示例#2
0
 def default_output_fn(self, prediction, accept):
     """Return encoded prediction for the response.
     Args:
         prediction (obj): prediction returned by predict_fn .
         accept (str): accept content-type expected by the client.
     Returns:
         encoded response for MMS to return to client
     """
     accept_type = accept.lower()
     try:
         if accept_type == content_types.CSV or accept_type == 'csv':
             if SAGEMAKER_BATCH:
                 return_data = "\n".join(map(str, prediction.tolist())) + '\n'
             else:
                 # FIXME: this is invalid CSV and is only retained for backwards compatibility
                 return_data = ",".join(map(str, prediction.tolist()))
             encoded_prediction = return_data.encode("utf-8")
         elif accept_type == content_types.JSON or accept_type == 'json':
             encoded_prediction = encoder.encode(prediction, accept_type)
         else:
             raise ValueError("{} is not an accepted Accept type. Please choose one of the following:"
                              " ['{}', '{}'].".format(accept, content_types.CSV, content_types.JSON))
     except Exception as e:
         raise UnsupportedMediaTypeInferenceError(
             "Encoding to accept type {} failed with exception: {}".format(accept,
                                                                           e))
     return encoded_prediction
示例#3
0
def output_fn(prediction, accept):
    if type(prediction) == torch.Tensor:
        prediction = prediction.detach().cpu().numpy().tolist()
    encoded_prediction = encoder.encode(prediction, accept)
    if accept == content_types.CSV:
        encoded_prediction = encoded_prediction.encode("utf-8")

    return encoded_prediction
    def output_fn(self, prediction, accept):
        """A default output_fn for PyTorch. Serializes predictions from predict_fn to JSON, CSV or NPY format.

        Args:
            prediction: a prediction result from predict_fn
            accept: type which the output data needs to be serialized

        Returns: output data serialized
        """
        return encoder.encode(prediction, accept)
示例#5
0
    def default_output_fn(self, prediction, accept):
        """Function responsible for serializing the prediction result to the desired accept type.

        Args:
            prediction (obj): prediction result returned by the predict_fn.
            accept (str): accept header expected by the client.

        Returns:
            obj: prediction data.

        """
        return encoder.encode(prediction, accept), accept
示例#6
0
 def default_output_fn(prediction, accept):
     """Function responsible to serialize the prediction for the response.
     Args:
         prediction (obj): prediction returned by predict_fn .
         accept (str): accept content-type expected by the client.
     Returns:
         (worker.Response): a Flask response object with the following args:
             * Args:
                 response: the serialized data to return
                 accept: the content-type that the data was transformed to.
     """
     return encoder.encode(prediction, accept), accept
示例#7
0
    def default_output_fn(self, prediction, accept):
        """A default output_fn for PyTorch. Serializes predictions from predict_fn to JSON, CSV or NPY format.
        Args:
            prediction: a prediction result from predict_fn
            accept: type which the output data needs to be serialized
        Returns: output data serialized
        """
        if type(prediction) == torch.Tensor:
            prediction = prediction.detach().cpu().numpy().tolist()
        encoded_prediction = encoder.encode(prediction, accept)
        if accept == content_types.CSV:
            encoded_prediction = encoded_prediction.encode("utf-8")

        return encoded_prediction
示例#8
0
    def default_output_fn(self, prediction, accept):  # pylint: disable=no-self-use
        """Function responsible for serializing the prediction result to the desired accept type.

        Args:
            prediction (obj): prediction result returned by the predict_fn.
            accept (str): accept header expected by the client.

        Returns:
            obj: prediction data.

        """
        for content_type in utils.parse_accept(accept):
            if content_type in encoder.SUPPORTED_CONTENT_TYPES:
                return encoder.encode(prediction, content_type), content_type
        raise errors.UnsupportedFormatError(accept)
示例#9
0
    def default_output_fn(self, prediction, accept):
        """Serialize the prediction into a response.

        Args:
            prediction (mxnet.nd.array): an MXNet NDArray that is the result of a prediction
            accept (str): the accept content type expected by the client

        Returns:
            obj: prediction data.

        Raises:
            sagemaker_inference.errors.UnsupportedFormatError: if an unsupported content type is used.

        """
        if accept in self.VALID_CONTENT_TYPES:
            return encoder.encode(prediction.asnumpy().tolist(), accept)
        else:
            raise errors.UnsupportedFormatError(accept)
示例#10
0
    def default_output_fn(self, prediction, accept):
        """A default output_fn for TensorFlow. Serializes predictions from predict_fn to JSON, CSV or NPY format.

        Args:
            prediction: a prediction result from predict_fn
            accept: type which the output data needs to be serialized

        Returns: output data serialized
        """

        prediction = np.array(prediction).squeeze()
        prediction[prediction > 0.5] = 1
        prediction[prediction <= 0.5] = 0

        # rescaling
        prediction = (prediction * 255).astype(np.uint8)

        return encoder.encode(prediction, accept)
示例#11
0
    def default_output_fn(self, prediction, accept):
        """
        Gets the prediction output and format it to be returned to the user

        Parameters
        ----------            
        prediction    : pd.DataFrame
                        Predicted dataset
        accept        : string
                        Output type

        Returns
        -------
        CSV : CSV file
        """
        logging.info('Saving')
        if accept != "text/csv":
            raise Exception("Invalid accept: %s" % accept)
        return encoder.encode(prediction, accept)
    def default_output_fn(self, prediction, accept):
        """A default output_fn for PyTorch. Serializes predictions from predict_fn to JSON, CSV or NPY format.

        Args:
            prediction: a prediction result from predict_fn
            accept: type which the output data needs to be serialized

        Returns: output data serialized
        """
        if type(prediction) == torch.Tensor:
            prediction = prediction.detach().cpu().numpy().tolist()

        for content_type in utils.parse_accept(accept):
            if content_type in encoder.SUPPORTED_CONTENT_TYPES:
                encoded_prediction = encoder.encode(prediction, content_type)
                if content_type == content_types.CSV:
                    encoded_prediction = encoded_prediction.encode("utf-8")
                return encoded_prediction

        raise errors.UnsupportedFormatError(accept)
示例#13
0
 def default_output_fn(self, prediction, accept):
     if accept != "text/csv":
         raise Exception("Invalid accept: %s" % accept)
     return encoder.encode(prediction, accept)
示例#14
0
文件: predict.py 项目: josolnik/MLND
def output_fn(prediction_output, accept):
    print('Serializing the generated output.')
    if accept == CONTENT_TYPE_OUTPUT:
        return encoder.encode(prediction_output, accept)
    raise Exception('Requested unsupported ContentType in Accept: ' + accept)
示例#15
0
def test_encode_error():
    with pytest.raises(errors.UnsupportedFormatError):
        encoder.encode(42, content_types.OCTET_STREAM)
示例#16
0
def output_fn(prediction, content_type):
    res = [{"probabilities": result["probabilities"], "top_n_grams": result["top_n_grams"]} for result in prediction]
    return encoder.encode(res, content_type)