File size: 4,369 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
{
 "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": [
       "<div><iframe src=\"http://127.0.0.1:7865/\" 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": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:6 out of the last 7 calls to <function TensorFlowTrainer.make_predict_function.<locals>.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
}