{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7865\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": 6, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:6 out of the last 7 calls to .one_step_on_data_distributed at 0x315091670> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 608ms/step\n", "[[0.49759743 0.50240254]]\n" ] } ], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "from PIL import Image\n", "import numpy as np\n", "\n", "# Load the pre-trained model\n", "model = tf.keras.models.load_model('dogs-vs-cats-model_transferlearning.keras')\n", "\n", "def classify_image(img):\n", " \n", " if isinstance(img, np.ndarray):\n", " img = Image.fromarray(img.astype('uint8'), 'RGB')\n", "\n", " # Preprocess the image to fit the model's input requirements\n", " img = img.resize((150, 150)) # Resize the image using PIL, which is intended here\n", " img_array = np.array(img) # Convert the resized PIL image to a numpy array\n", " img_array = img_array / 255.0 # Normalize pixel values to [0, 1]\n", " img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to fit model input shape\n", "\n", " # Make prediction\n", " prediction = model.predict(img_array)\n", "\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}\n", "\n", " print(prediction)\n", "\n", " probabilities = tf.sigmoid(prediction).numpy() # Convert tensor to numpy array if using \n", "\n", " # Formatting the probabilities\n", " class_names = ['Cat', 'Dog']\n", " results = {class_names[i]: float(prediction[0][i]) for i in range(2)} # Convert each probability to float\n", " \n", " return results\n", "\n", "# Create Gradio interface\n", "iface = gr.Interface(fn=classify_image,\n", " inputs=gr.Image(),\n", " outputs=gr.Label(num_top_classes=2),\n", " title=\"Cat vs Dog Classifier\",\n", " description=\"Upload an image of a cat or dog to classify.\")\n", "\n", "# Launch the application\n", "iface.launch()\n" ] } ], "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 }