{ "cells": [ { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "import numpy as np\n", "from PIL import Image" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Requirement already satisfied: tensorflow in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (2.16.1)\n", "Requirement already satisfied: tensorflow-datasets in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (4.9.3)\n", "Requirement already satisfied: gradio in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (4.27.0)\n", "Requirement already satisfied: pillow in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (10.2.0)\n", "Requirement already satisfied: matplotlib in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (3.8.3)\n", "Requirement already satisfied: absl-py>=1.0.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (1.4.0)\n", "Requirement already satisfied: astunparse>=1.6.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (1.6.3)\n", "Requirement already satisfied: flatbuffers>=23.5.26 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (24.3.25)\n", "Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (0.5.4)\n", "Requirement already satisfied: google-pasta>=0.1.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (0.2.0)\n", "Requirement already satisfied: h5py>=3.10.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (3.11.0)\n", "Requirement already satisfied: libclang>=13.0.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (18.1.1)\n", "Requirement already satisfied: ml-dtypes~=0.3.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (0.3.2)\n", "Requirement already satisfied: opt-einsum>=2.3.2 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (3.3.0)\n", "Requirement already satisfied: packaging in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (23.2)\n", "Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (3.20.3)\n", "Requirement already satisfied: requests<3,>=2.21.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (2.31.0)\n", "Requirement already satisfied: setuptools in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages (from tensorflow) (58.0.4)\n", "Requirement already satisfied: six>=1.12.0 in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages (from tensorflow) (1.15.0)\n", "Requirement already satisfied: termcolor>=1.1.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (2.4.0)\n", "Requirement already satisfied: typing-extensions>=3.6.6 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (4.9.0)\n", "Requirement already satisfied: wrapt>=1.11.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (1.16.0)\n", "Requirement already satisfied: grpcio<2.0,>=1.24.3 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (1.62.2)\n", "Requirement already satisfied: tensorboard<2.17,>=2.16 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (2.16.2)\n", "Requirement already satisfied: keras>=3.0.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (3.2.1)\n", "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (0.36.0)\n", "Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow) (1.26.4)\n", "Requirement already satisfied: array-record in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (0.4.1)\n", "Requirement already satisfied: click in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (8.1.7)\n", "Requirement already satisfied: dm-tree in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (0.1.8)\n", "Requirement already satisfied: etils>=0.9.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from etils[enp,epath,etree]>=0.9.0->tensorflow-datasets) (1.5.2)\n", "Requirement already satisfied: promise in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (2.3)\n", "Requirement already satisfied: psutil in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (5.9.6)\n", "Requirement already satisfied: tensorflow-metadata in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (1.14.0)\n", "Requirement already satisfied: toml in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (0.10.2)\n", "Requirement already satisfied: tqdm in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-datasets) (4.66.2)\n", "Requirement already satisfied: aiofiles<24.0,>=22.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (23.2.1)\n", "Requirement already satisfied: altair<6.0,>=4.2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (5.3.0)\n", "Requirement already satisfied: fastapi in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.110.2)\n", "Requirement already satisfied: ffmpy in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.3.2)\n", "Requirement already satisfied: gradio-client==0.15.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.15.1)\n", "Requirement already satisfied: httpx>=0.24.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.27.0)\n", "Requirement already satisfied: huggingface-hub>=0.19.3 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.22.2)\n", "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (6.1.1)\n", "Requirement already satisfied: jinja2<4.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (3.1.3)\n", "Requirement already satisfied: markupsafe~=2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (2.1.5)\n", "Requirement already satisfied: orjson~=3.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (3.10.1)\n", "Requirement already satisfied: pandas<3.0,>=1.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (2.2.0)\n", "Requirement already satisfied: pydantic>=2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (2.7.0)\n", "Requirement already satisfied: pydub in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.25.1)\n", "Requirement already satisfied: python-multipart>=0.0.9 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.0.9)\n", "Requirement already satisfied: pyyaml<7.0,>=5.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (6.0.1)\n", "Requirement already satisfied: ruff>=0.2.2 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.4.1)\n", "Requirement already satisfied: semantic-version~=2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (2.10.0)\n", "Requirement already satisfied: tomlkit==0.12.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.12.0)\n", "Requirement already satisfied: typer<1.0,>=0.12 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.12.3)\n", "Requirement already satisfied: urllib3~=2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (2.2.1)\n", "Requirement already satisfied: uvicorn>=0.14.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio) (0.29.0)\n", "Requirement already satisfied: fsspec in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio-client==0.15.1->gradio) (2024.3.1)\n", "Requirement already satisfied: websockets<12.0,>=10.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from gradio-client==0.15.1->gradio) (11.0.3)\n", "Requirement already satisfied: contourpy>=1.0.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from matplotlib) (1.2.0)\n", "Requirement already satisfied: cycler>=0.10 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from matplotlib) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from matplotlib) (4.49.0)\n", "Requirement already satisfied: kiwisolver>=1.3.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from matplotlib) (1.4.5)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from matplotlib) (3.1.1)\n", "Requirement already satisfied: python-dateutil>=2.7 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from matplotlib) (2.8.2)\n", "Requirement already satisfied: jsonschema>=3.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from altair<6.0,>=4.2.0->gradio) (4.21.1)\n", "Requirement already satisfied: toolz in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from altair<6.0,>=4.2.0->gradio) (0.12.1)\n", "Requirement already satisfied: wheel<1.0,>=0.23.0 in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages (from astunparse>=1.6.0->tensorflow) (0.37.0)\n", "Requirement already satisfied: zipp in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from etils[enp,epath,etree]>=0.9.0->tensorflow-datasets) (3.17.0)\n", "Requirement already satisfied: anyio in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from httpx>=0.24.1->gradio) (4.3.0)\n", "Requirement already satisfied: certifi in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from httpx>=0.24.1->gradio) (2024.2.2)\n", "Requirement already satisfied: httpcore==1.* in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from httpx>=0.24.1->gradio) (1.0.5)\n", "Requirement already satisfied: idna in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from httpx>=0.24.1->gradio) (3.7)\n", "Requirement already satisfied: sniffio in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from httpx>=0.24.1->gradio) (1.3.1)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n", "Requirement already satisfied: filelock in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from huggingface-hub>=0.19.3->gradio) (3.13.4)\n", "Requirement already satisfied: rich in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from keras>=3.0.0->tensorflow) (13.7.1)\n", "Requirement already satisfied: namex in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from keras>=3.0.0->tensorflow) (0.0.8)\n", "Requirement already satisfied: optree in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from keras>=3.0.0->tensorflow) (0.11.0)\n", "Requirement already satisfied: pytz>=2020.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n", "Requirement already satisfied: tzdata>=2022.7 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from pydantic>=2.0->gradio) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.18.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from pydantic>=2.0->gradio) (2.18.1)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from requests<3,>=2.21.0->tensorflow) (3.3.2)\n", "Requirement already satisfied: markdown>=2.6.8 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorboard<2.17,>=2.16->tensorflow) (3.6)\n", "Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorboard<2.17,>=2.16->tensorflow) (0.7.2)\n", "Requirement already satisfied: werkzeug>=1.0.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorboard<2.17,>=2.16->tensorflow) (3.0.2)\n", "Requirement already satisfied: shellingham>=1.3.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n", "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from fastapi->gradio) (0.37.2)\n", "Requirement already satisfied: googleapis-common-protos<2,>=1.52.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from tensorflow-metadata->tensorflow-datasets) (1.63.0)\n", "Requirement already satisfied: attrs>=22.2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.2.0)\n", "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.12.1)\n", "Requirement already satisfied: referencing>=0.28.4 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.34.0)\n", "Requirement already satisfied: rpds-py>=0.7.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.18.0)\n", "Requirement already satisfied: importlib-metadata>=4.4 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from markdown>=2.6.8->tensorboard<2.17,>=2.16->tensorflow) (7.0.0)\n", "Requirement already satisfied: markdown-it-py>=2.2.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from rich->keras>=3.0.0->tensorflow) (3.0.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from rich->keras>=3.0.0->tensorflow) (2.17.2)\n", "Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from anyio->httpx>=0.24.1->gradio) (1.2.0)\n", "Requirement already satisfied: mdurl~=0.1 in /Users/chiarazbinden/Library/Python/3.9/lib/python/site-packages (from markdown-it-py>=2.2.0->rich->keras>=3.0.0->tensorflow) (0.1.2)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install tensorflow tensorflow-datasets gradio pillow matplotlib" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "model_path = \"pokemon-model_transferlearning.keras\"\n", "model = tf.keras.models.load_model(model_path)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# Define the core prediction function\n", "def predict_pokemon(image):\n", " # Preprocess image\n", " print(type(image))\n", " image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image\n", " image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale\n", " image = np.array(image)\n", " image = np.expand_dims(image, axis=0) # same as image[None, ...]\n", " \n", " # Predict\n", " prediction = model.predict(image)\n", " \n", " # Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability\n", " # we could also change the output layer to dense(1, activation='sigmoid')\n", " prediction = np.round(float(tf.sigmoid(prediction)), 2)\n", " p_cat = (1 - prediction)\n", " p_dog = prediction\n", " return {'cat': p_cat, 'dog': p_dog}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "from PIL import Image\n", "import numpy as np\n", "import tensorflow as tf\n", "\n", "# Define the core prediction function\n", "def predict_pokemon(image):\n", " # Preprocess image\n", " image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image\n", " image = image.resize((150, 150)) # Resize the image to 150x150\n", " image = np.array(image)\n", " image = np.expand_dims(image, axis=0) # Add batch dimension\n", " \n", " # Predict\n", " prediction = model.predict(image)\n", " \n", " # Apply softmax to get probabilities for each class\n", " probabilities = tf.nn.softmax(prediction)\n", " \n", " # Map probabilities to Pokemon classes\n", " pokemon_classes = ['Articuno', 'Bulbasaur', 'Charmander'] \n", " probabilities_dict = {pokemon_class: round(float(probability), 2) for pokemon_class, probability in zip(pokemon_classes, probabilities[0])}\n", " \n", " return probabilities_dict\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7863\n", "IMPORTANT: You are using gradio version 4.27.0, however version 4.29.0 is available, please upgrade.\n", "--------\n", "Running on public URL: https://a1f42e2a7f46b68a92.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 1s/step\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 79ms/step\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 68ms/step\n" ] } ], "source": [ "# Create the Gradio interface\n", "input_image = gr.Image()\n", "iface = gr.Interface(\n", " fn=predict_pokemon,\n", " inputs=input_image, \n", " outputs=gr.Label(),\n", " examples=[\"images/cat1.jpeg\", \"images/cat2.jpeg\", \"images/cat3.jpeg\", \"images/cat4.jpeg\", \"images/dog1.jpeg\", \"images/dog2.jpeg\", \"images/dog3.jpeg\"], \n", " description=\"A simple mlp classification model for image classification using the mnist dataset.\")\n", "iface.launch(share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.6" } }, "nbformat": 4, "nbformat_minor": 2 }