Skip to content

Instantly share code, notes, and snippets.

@kamil-andrzejewski
Created October 13, 2022 13:07
Show Gist options
  • Save kamil-andrzejewski/34365fc74fa99b0f3dc963eee6d326e0 to your computer and use it in GitHub Desktop.
Save kamil-andrzejewski/34365fc74fa99b0f3dc963eee6d326e0 to your computer and use it in GitHub Desktop.
Poplar Triton Backend blog post - client script content
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