Created
October 13, 2022 13:07
-
-
Save kamil-andrzejewski/34365fc74fa99b0f3dc963eee6d326e0 to your computer and use it in GitHub Desktop.
Poplar Triton Backend blog post - client script content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy | |
from torchvision import transforms | |
from PIL import Image | |
import tritonclient.grpc | |
# Prepare tritonclient object needed to send a request to the server. | |
infer_client = tritonclient.grpc.InferenceServerClient("localhost:8001") | |
input = [tritonclient.grpc.InferInput("digit_image", [1, 1, 28, 28], "FP32")] | |
img = None | |
# Loop over all files in the "img" directory. | |
# Let's assume the directory contains images | |
# that can be used to send a request to the server. | |
for entry in os.scandir("./img"): | |
try: | |
img = Image.open(entry.path) | |
except IOError: | |
continue | |
# Preprocess images data. | |
img_tensor = transforms.ToTensor()(img) | |
img_tensor = transforms.Normalize((0.1307,), (0.3081,)).forward(img_tensor) | |
img_tensor = img_tensor[None, :] | |
input[0].set_data_from_numpy(img_tensor.numpy()) | |
# Send the request. | |
response = infer_client.infer(model_name="mnist", inputs=input) | |
# Postprocess response data. | |
prediction_result = numpy.argmax(response.as_numpy("output_labels")) | |
# Print prediction result. | |
print(f'Predicted digit for file \"{entry.name}\": {prediction_result}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment