mirror of
https://github.com/hanzo-ml/kserve.git
synced 2026-10-30 20:53:49 +00:00
* Initial implementation of inference client Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Add tests Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Use Inference client for e2e tests Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Upgrade pytest-asyncio to 0.23.4 Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix mutable object initialization in default parameters Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix graph e2e tests Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix pmml test Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Add explain, support dict response, use inference client for internal requests Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix inference graph test and grpc headers Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Remove v1 datamodels Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Introduce protocol in client config Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Support inference graph Signed-off-by: Sivanantham Chinnaiyan <[email protected]> remove logging configs Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Update default timeout to 60 seconds Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Add retry config for grpc client Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix infer model_name parameter Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Add tests for graph endpoint Signed-off-by: Sivanantham Chinnaiyan <[email protected]> debug Signed-off-by: Sivanantham Chinnaiyan <[email protected]> fix http client param mismatch Signed-off-by: Sivanantham Chinnaiyan <[email protected]> skip graph test Signed-off-by: Sivanantham Chinnaiyan <[email protected]> fix timeout in grpc client Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix url construction Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix explain Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * configure logger for e2e tests Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Rebase master Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Rebase master Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix grpc retry config Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Increase request timeout Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * configure logger for e2e tests Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Rebase master Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Rebase master Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Fix grpc retry config Signed-off-by: Sivanantham Chinnaiyan <[email protected]> Increase request timeout Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Rebase Signed-off-by: Sivanantham Chinnaiyan <[email protected]> * Use fixtures for rest client Signed-off-by: Sivanantham Chinnaiyan <[email protected]> --------- Signed-off-by: Sivanantham Chinnaiyan <[email protected]>
99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
# Copyright 2022 The KServe Authors.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import argparse
|
|
import io
|
|
from typing import Dict
|
|
|
|
import torch
|
|
from kserve import InferRequest, Model, ModelServer, logging, model_server
|
|
from kserve.utils.utils import generate_uuid
|
|
from PIL import Image
|
|
from torchvision import models, transforms
|
|
|
|
|
|
# This custom predictor example implements the custom model following KServe v2 inference gPPC protocol,
|
|
# the input can be raw image bytes or image tensor which is pre-processed by transformer
|
|
# and then passed to predictor, the output is the prediction response.
|
|
class AlexNetModel(Model):
|
|
def __init__(self, name: str):
|
|
super().__init__(name)
|
|
self.name = name
|
|
self.load()
|
|
self.model = None
|
|
self.ready = False
|
|
|
|
def load(self):
|
|
self.model = models.alexnet(pretrained=True, progress=False)
|
|
self.model.eval()
|
|
self.ready = True
|
|
|
|
def preprocess(
|
|
self, payload: InferRequest, headers: Dict[str, str] = None
|
|
) -> torch.Tensor:
|
|
req = payload.inputs[0]
|
|
if req.datatype == "BYTES":
|
|
input_image = Image.open(io.BytesIO(req.data[0]))
|
|
preprocess = transforms.Compose(
|
|
[
|
|
transforms.Resize(256),
|
|
transforms.CenterCrop(224),
|
|
transforms.ToTensor(),
|
|
transforms.Normalize(
|
|
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
|
|
),
|
|
]
|
|
)
|
|
|
|
input_tensor = preprocess(input_image)
|
|
return input_tensor.unsqueeze(0)
|
|
elif req.datatype == "FP32":
|
|
np_array = payload.inputs[0].as_numpy()
|
|
return torch.Tensor(np_array)
|
|
|
|
def predict(
|
|
self, input_tensor: torch.Tensor, headers: Dict[str, str] = None
|
|
) -> Dict:
|
|
output = self.model(input_tensor)
|
|
torch.nn.functional.softmax(output, dim=1)
|
|
values, top_5 = torch.topk(output, 5)
|
|
result = values.flatten().tolist()
|
|
id = generate_uuid()
|
|
response = {
|
|
"id": id,
|
|
"model_name": "custom-model",
|
|
"outputs": [
|
|
{
|
|
"contents": {
|
|
"fp32_contents": result,
|
|
},
|
|
"datatype": "FP32",
|
|
"name": "output-0",
|
|
"shape": list(values.shape),
|
|
}
|
|
],
|
|
}
|
|
return response
|
|
|
|
|
|
parser = argparse.ArgumentParser(parents=[model_server.parser])
|
|
args, _ = parser.parse_known_args()
|
|
|
|
if __name__ == "__main__":
|
|
if args.configure_logging:
|
|
logging.configure_logging(args.log_config_file)
|
|
model = AlexNetModel("custom-model")
|
|
model.load()
|
|
ModelServer().start([model])
|