{ "cells": [ { "cell_type": "code", "execution_count": 10, "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": 11, "metadata": {}, "outputs": [], "source": [ "model_path = \"Pokemon_transfer_learning.keras\"\n", "model = tf.keras.models.load_model(model_path)" ] }, { "cell_type": "code", "execution_count": 12, "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(prediction, 2)\n", " # Separate the probabilities for each class\n", " p_abra = prediction[0][0] # Probability for class 'abra'\n", " p_beedrill = prediction[0][1] # Probability for class 'moltres'\n", " p_sandshrew = prediction[0][2] # Probability for class 'zapdos'\n", " return {'abra': p_abra, 'beedrill': p_beedrill, 'sandshrew': p_sandshrew}" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7862\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 1s/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/abra1.png\", \"images/abra2.jpg\", \"images/abra3.png\", \"images/beedrill1.png\", \"images/beedrill2.png\", \"images/beedrill3.jpg\", \"images/sandshrew1.png\", \"images/sandshrew2.jpg\", \"images/sandshrew3.png\"], \n", " description=\"A simple mlp classification model for image classification using the mnist dataset.\")\n", "iface.launch(share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "venv_new", "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.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }