-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
59 lines (47 loc) · 1.94 KB
/
Copy pathclient.py
File metadata and controls
59 lines (47 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import tritonclient.grpc as grpcclient
from tritonclient.utils import np_to_triton_dtype
import numpy as np
from PIL import Image
import requests
import io
# Initialize the client
client = grpcclient.InferenceServerClient(url="localhost:8001")
# Define image preprocessing function
def preprocess_image(image):
# Resize
image = image.resize((224, 224), Image.BICUBIC)
# Convert to numpy array and normalize to 0-1
img_array = np.array(image).astype(np.float32) / 255.0
# Normalize using ImageNet stats
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
img_array = (img_array - mean) / std
# Transpose from (H, W, C) to (C, H, W)
img_array = img_array.transpose(2, 0, 1)
return img_array.astype(np.float32)
# Download and preprocess the image
image_url = "https://developer.download.nvidia.com/assets/Clara/monai/samples/cxr_00026451_030.jpg"
response = requests.get(image_url)
image = Image.open(io.BytesIO(response.content)).convert('RGB')
image_tensor = preprocess_image(image)
# Add batch dimension and rearrange dimensions to match expected shape [batch, height, width, channels]
image_tensor = np.expand_dims(image_tensor, axis=0) # Add batch dimension # Rearrange to [batch, height, width, channels]
# Create input tensor
input_tensor = grpcclient.InferInput(
name="image",
shape=image_tensor.shape,
datatype=np_to_triton_dtype(image_tensor.dtype)
)
input_tensor.set_data_from_numpy(image_tensor)
# Send request
response = client.infer(
model_name="chest_xray",
inputs=[input_tensor]
)
# Get predictions
output = response.as_numpy("predictions")
labels = ["atelectasis", "cardiomegaly", "pleural effusion", "infiltration",
"lung mass", "lung nodule", "pneumonia", "pneumothorax", "consolidation",
"edema", "emphysema", "fibrosis", "pleural thicken", "hernia"]
for label, pred in zip(labels, output[0]):
print(f"{label}: {pred:.4f}")