{ "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": [ "