def test_convert_for_regress_invalid_output_example_spec(self): prediction_log = text_format.Parse( """ regress_log { request { input { example_list { examples { features { feature: { key: "regress_input" value: { bytes_list: { value: "feature" } } } } } } } } response { result { regressions { value: 0.7 } } } } """, prediction_log_pb2.PredictionLog()) output_example_spec = text_format.Parse( """ output_columns_spec { } """, bulk_inferrer_pb2.OutputExampleSpec()) with self.assertRaises(ValueError): utils.convert(prediction_log, output_example_spec)
def test_convert_for_predict_invalid_output_example_spec(self): example = text_format.Parse( """ features { feature { key: "predict_input" value: { bytes_list: { value: "feature" } } } }""", tf.train.Example()) prediction_log = text_format.Parse( """ predict_log { request { inputs { key: "%s" value { dtype: DT_STRING tensor_shape { dim { size: 1 } } } } } response { outputs { key: "output_float" value { dtype: DT_FLOAT tensor_shape { dim { size: 1 } dim { size: 2 }} float_val: 0.1 float_val: 0.2 } } outputs { key: "output_bytes" value { dtype: DT_STRING tensor_shape { dim { size: 1 }} string_val: "prediction" } } } } """ % (utils.INPUT_KEY), prediction_log_pb2.PredictionLog()) # The ending quote cannot be recognized correctly when `string_val` field # is directly set with a serialized string quoted in the text format. prediction_log.predict_log.request.inputs[ utils.INPUT_KEY].string_val.append(example.SerializeToString()) output_example_spec = text_format.Parse( """ output_columns_spec { } """, bulk_inferrer_pb2.OutputExampleSpec()) with self.assertRaises(ValueError): utils.convert(prediction_log, output_example_spec)
def test_convert_for_regress(self): prediction_log = text_format.Parse( """ regress_log { request { input { example_list { examples { features { feature: { key: "regress_input" value: { bytes_list: { value: "feature" } } } } } } } } response { result { regressions { value: 0.7 } } } } """, prediction_log_pb2.PredictionLog()) output_example_spec = text_format.Parse( """ output_columns_spec { regress_output { value_column: 'regress_value' } } """, bulk_inferrer_pb2.OutputExampleSpec()) expected_example = text_format.Parse( """ features { feature: { key: "regress_input" value: { bytes_list: { value: "feature" } } } feature: { key: "regress_value" value: { float_list: { value: 0.7 } } } } """, tf.train.Example()) self.assertProtoEquals( expected_example, utils.convert(prediction_log, output_example_spec))
def convert_to_dict(prediction_log: prediction_log_pb2.PredictionLog, output_example_spec: _OutputExampleSpecType) \ -> tf.train.Example: """Converts given `prediction_log` to a `tf.train.Example`. Args: prediction_log: The input prediction log. output_example_spec: The spec for how to map prediction results to columns in example. Returns: A `tf.train.Example` converted from the given prediction_log. Raises: ValueError: If the inference type or signature name in spec does not match that in prediction_log. """ tf_example = convert(prediction_log=prediction_log, output_example_spec=output_example_spec) return tf_example
def test_convert_for_multi_inference(self): prediction_log = text_format.Parse( """ multi_inference_log { request { input { example_list { examples { features { feature: { key: "input" value: { bytes_list: { value: "feature" } } } } } } } } response { results { model_spec { signature_name: 'classification' } classification_result { classifications { classes { label: '1' score: 0.6 } classes { label: '0' score: 0.4 } } } } results { model_spec { signature_name: 'regression' } regression_result { regressions { value: 0.7 } } } } } """, prediction_log_pb2.PredictionLog()) output_example_spec = text_format.Parse( """ output_columns_spec { signature_name: 'classification' classify_output { label_column: 'classify_label' score_column: 'classify_score' } } output_columns_spec { signature_name: 'regression' regress_output { value_column: 'regress_value' } } """, bulk_inferrer_pb2.OutputExampleSpec()) expected_example = text_format.Parse( """ features { feature: { key: "input" value: { bytes_list: { value: "feature" } } } feature: { key: "classify_label" value: { bytes_list: { value: "1" value: "0"} } } feature: { key: "classify_score" value: { float_list: { value: 0.6 value: 0.4} } } feature: { key: "regress_value" value: { float_list: { value: 0.7} } } } """, tf.train.Example()) self.assertProtoEquals( expected_example, utils.convert(prediction_log, output_example_spec))