File size: 4,078 Bytes
32621ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7862\n",
"Running on public URL: https://59007d69f4a41b96f2.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": [
"<div><iframe src=\"https://59007d69f4a41b96f2.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 3,
"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 628ms/step\n",
"[[0.33698466 0.33483982 0.32817554]]\n",
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 35ms/step\n",
"[[0.33667472 0.33442825 0.32889706]]\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('pokemon_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 = ['Hitchoman', 'Pikachu', 'Charmeleon']\n",
" results = {class_names[i]: float(prediction[0][i]) for i in range(3)} # 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=3),\n",
" title=\"Pokemon Classifier\",\n",
" description=\"Upload an image of a pokemon to classify.\")\n",
"\n",
"# Launch the application\n",
"iface.launch(share=True)\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
}
|