-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Notebook that demonstrates how to invoke the KServe Inference Service. NOTE: Do not forget to create an Authorization Policy for the Inference Service. See here: kubeflow/manifests#2811 Signed-off-by: Dimitris Poulopoulos <dimitris.a.poulopoulos@gmail.com>
- Loading branch information
1 parent
28f413c
commit a136885
Showing
1 changed file
with
132 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "67f190cf-9caf-4da7-adc7-048065698f6d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import h5py\n", | ||
"import requests\n", | ||
"import pandas as pd\n", | ||
"\n", | ||
"from PIL import Image\n", | ||
"from io import BytesIO\n", | ||
"from torchvision import transforms" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b1447be3-51e3-4900-ad6e-5c48ce79bae2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data_path = \"data\"\n", | ||
"\n", | ||
"path_test_df = os.path.join(data_path, \"test-metadata.csv\")\n", | ||
"path_test_hdf5 = os.path.join(data_path, \"test-image.hdf5\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "01d6b115-c9ad-4ad4-a966-a58f09d5b9de", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"test_df = pd.read_csv(path_test_df)\n", | ||
"isic_id = test_df.isic_id.values.tolist()\n", | ||
"\n", | ||
"hdf5_img = h5py.File(path_test_hdf5, 'r')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "681ae630-d372-440d-a29e-5a68c209943d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"example_image = Image.open(BytesIO(hdf5_img[isic_id[0]][()]))\n", | ||
"example_image.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "53e38ac0-bd5f-4c46-a098-f7735c275d8e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def prepare_request_body(img: Image) -> dict:\n", | ||
" transformations = transforms.Compose([\n", | ||
" transforms.Resize((224, 224)),\n", | ||
" transforms.ToTensor(),\n", | ||
" transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", | ||
" ])\n", | ||
" \n", | ||
" image = transformations(img)[None, :]\n", | ||
" \n", | ||
" body = {\n", | ||
" \"inputs\": [\n", | ||
" {\n", | ||
" \"name\": \"input.1\",\n", | ||
" \"shape\": [1, 3, 224, 224],\n", | ||
" \"datatype\": \"FP32\",\n", | ||
" \"data\": image.tolist()\n", | ||
" }\n", | ||
" ]\n", | ||
" }\n", | ||
" \n", | ||
" return body" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "eb1562d1-a944-4b18-b392-5217662e44c1", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"URL = \"http://skin-cancer-detection.kubeflow-user-example-com.svc.cluster.local/v2/models/skin_cancer_detection/infer\"\n", | ||
"response = requests.post(URL, json=prepare_request_body(example_image))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0f2ad89c-74ad-478c-9fc0-612c6ae129e6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if response.status_code == 200:\n", | ||
" prob = response.json()[\"outputs\"][0][\"data\"][0]\n", | ||
" print(f\"The probability that the lesion is malignant is {prob:.3f}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |