|
| 1 | +# Copyright 2024 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Prediction from the Triton server.""" |
| 16 | + |
| 17 | +from typing import Any |
| 18 | +import cv2 |
| 19 | +import numpy as np |
| 20 | +import tritonclient |
| 21 | + |
| 22 | +_API_URL = 'localhost:8000' |
| 23 | +_OUTPUT_KEYS = ( |
| 24 | + 'detection_classes', |
| 25 | + 'detection_masks', |
| 26 | + 'detection_boxes', |
| 27 | + 'image_info', |
| 28 | + 'num_detections', |
| 29 | + 'detection_scores', |
| 30 | +) |
| 31 | + |
| 32 | +# Setting up the Triton client |
| 33 | +_TRITON_CLIENT = tritonclient.http.InferenceServerClient( |
| 34 | + url=_API_URL, network_timeout=1200, connection_timeout=1200 |
| 35 | +) |
| 36 | + |
| 37 | +# Outputs setup based on constants |
| 38 | +_OUTPUTS = [ |
| 39 | + tritonclient.http.InferRequestedOutput(key, binary_data=True) |
| 40 | + for key in _OUTPUT_KEYS |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +def model_input( |
| 45 | + path: str, height: int, width: int |
| 46 | +) -> tritonclient.http.InferInput: |
| 47 | + """Prepares an image for input to a Triton model server. |
| 48 | +
|
| 49 | + It reads it from a path, resizes it, normalizes it, and converts it to the |
| 50 | + format required by the server. |
| 51 | +
|
| 52 | + Args: |
| 53 | + path: The file path to the image that needs to be processed. |
| 54 | + height: The height of the image to be resized. |
| 55 | + width: The width of the image to be resized. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + A Triton inference server input object containing the processed image. |
| 59 | + """ |
| 60 | + original_image = cv2.imread(path) |
| 61 | + image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) |
| 62 | + image_resized = cv2.resize( |
| 63 | + image, (width, height), interpolation=cv2.INTER_AREA |
| 64 | + ) |
| 65 | + expanded_image = np.expand_dims(image_resized, axis=0) |
| 66 | + inputs = tritonclient.http.InferInput( |
| 67 | + 'inputs', expanded_image.shape, datatype='UINT8' |
| 68 | + ) |
| 69 | + inputs.set_data_from_numpy(expanded_image, binary_data=True) |
| 70 | + return inputs, image, image_resized |
| 71 | + |
| 72 | + |
| 73 | +def _query_model( |
| 74 | + client: tritonclient.http.InferenceServerClient, |
| 75 | + model_name: str, |
| 76 | + inputs: tritonclient.http.InferInput, |
| 77 | +) -> tritonclient.http.InferResult: |
| 78 | + """Sends an inference request to the Triton server. |
| 79 | +
|
| 80 | + Args: |
| 81 | + client: The Triton server client. |
| 82 | + model_name: Name of the model for which inference is requested. |
| 83 | + inputs: The input data for inference. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + The result of the inference request. |
| 87 | + """ |
| 88 | + return client.infer(model_name=model_name, inputs=[inputs], outputs=_OUTPUTS) |
| 89 | + |
| 90 | + |
| 91 | +def prediction( |
| 92 | + model_name: str, inputs: tritonclient.http.InferInput |
| 93 | +) -> dict[str, Any]: |
| 94 | + """Model name for prediction. |
| 95 | +
|
| 96 | + Args: |
| 97 | + model_name: Model name in Triton Server. |
| 98 | + inputs: The input data for inference. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + prediction output from the model. |
| 102 | + """ |
| 103 | + result = _query_model(_TRITON_CLIENT, model_name, inputs) |
| 104 | + result_dict = {key: result.as_numpy(key) for key in _OUTPUT_KEYS} |
| 105 | + return result_dict |
0 commit comments