diff --git a/03.client.ipynb b/03.client.ipynb new file mode 100755 index 0000000..1564e80 --- /dev/null +++ b/03.client.ipynb @@ -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 +} diff --git a/requirements.txt b/requirements.txt index 352da9d..58a881f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,6 @@ -kfp[kubernetes]==2.8.0 \ No newline at end of file +kfp[kubernetes]==2.8.0 +torch==2.3.1 +torchvision==0.18.1 +pandas==2.0.3 +pillow==10.4.0 +h5py==3.11.0 \ No newline at end of file