Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .DS_Store +0 -0
- .gitattributes +18 -0
- README.md +2 -8
- app.ipynb +187 -0
- app.py +46 -0
- app_cat_dog.ipynb +118 -0
- app_pokemon.ipynb +120 -0
- dogs-vs-cats-model_transferlearning.keras +3 -0
- dogs-vs-cats/.DS_Store +0 -0
- dogs-vs-cats/train/.DS_Store +0 -0
- dogs-vs-cats/train/cat/Abyssinian_127_jpg.rf.b2553880d73a78e26e396fe7775f31be.jpg +0 -0
- dogs-vs-cats/train/cat/Abyssinian_131_jpg.rf.3aeee0801499f335e3a59b51189e1351.jpg +0 -0
- dogs-vs-cats/train/cat/Abyssinian_149_jpg.rf.b6b4a72a93f8bc2b239b18c5d181190c.jpg +0 -0
- dogs-vs-cats/train/cat/Abyssinian_15_jpg.rf.7bc44eeb9865e35cb1d5f9ef4f53b381.jpg +0 -0
- dogs-vs-cats/train/cat/Abyssinian_167_jpg.rf.1d367cb1cf82c1e5ebda79fda928cee5.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_103_jpg.rf.bb684501a53abf381a6bf07f9cd2243c.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_109_jpg.rf.aed42b8605ef92b579992d02e2e2cc4c.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_116_jpg.rf.8c6280ad977c6c749da4d35156e49286.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_13_jpg.rf.7c75131d257406a75d8d676dd9b5f5f7.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_145_jpg.rf.3dadbfbb39437890fc249f7edb37cb2f.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_150_jpg.rf.05c93a40014062c5ae66bae50a857aca.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_165_jpg.rf.31185846cf5892c6e7e3f02784e04fef.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_167_jpg.rf.1542d3551f036a577880f22719c2fd08.jpg +0 -0
- dogs-vs-cats/train/cat/Bengal_174_jpg.rf.bbac947432fbff310b6be201bd9a3de7.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_105_jpg.rf.2f96b7dd47bb15df4def745602f5434d.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_135_jpg.rf.d9cdc64e747de2078f60a4761fdc534a.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_144_jpg.rf.369cb41f763175f21c7cfd77e77509ec.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_151_jpg.rf.179ae9238f29eda8d63af4da89c48e05.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_157_jpg.rf.cbf2d211aab989a6f26ef5b5cef5e63e.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_159_jpg.rf.3ba8a57e52d925aa33e85822004c591d.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_160_jpg.rf.dc1ca8f5f9cfd2aa585a7c024074ce7f.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_166_jpg.rf.ac8785ee575600493ffac45e2b0d1342.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_173_jpg.rf.b8656131a2f2fda3413154a7d01ad834.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_186_jpg.rf.6c15b2f2afe3c230ded5e5d04d6affe5.jpg +0 -0
- dogs-vs-cats/train/cat/Birman_189_jpg.rf.29a9d3b4f764fc9e2cc6b78fbe5d30da.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_122_jpg.rf.218d47f5275b4de8706a9a56e6276113.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_125_jpg.rf.c91236394fb2adb244ba8866b53dbd43.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_127_jpg.rf.4c9fde3d1b3fc52a20cd4b2b82b98856.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_140_jpg.rf.15757f698af74453f34ee58604ea6afc.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_146_jpg.rf.288e58c8b226aa5b7c6ad4442f7fda5c.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_153_jpg.rf.2da5201d989106a5b1b4025aec3349b4.jpg +0 -0
- dogs-vs-cats/train/cat/Bombay_18_jpg.rf.68c07a9d844cc01b8bf5582d7d3c0e01.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_101_jpg.rf.afa7cf2990a151279e674e2077fa306f.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_109_jpg.rf.97fcd6a9c7f0d54b3d39d2d709237287.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_154_jpg.rf.8e78c62b5e103771db2893338e5937da.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_164_jpg.rf.8d27185e8344301592da98df828ab073.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_166_jpg.rf.d18846beff566700c2e16e295bb701e4.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_177_jpg.rf.5efbf04740eeac07cdd83e5773d24221.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_187_jpg.rf.24d36402bce6b82092dc93876c65bb71.jpg +0 -0
- dogs-vs-cats/train/cat/British_Shorthair_193_jpg.rf.ab376d75fb05acc1ec59f06228ebe5bb.jpg +0 -0
.DS_Store
ADDED
Binary file (8.2 kB). View file
|
|
.gitattributes
CHANGED
@@ -33,3 +33,21 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
dogs-vs-cats-model_transferlearning.keras filter=lfs diff=lfs merge=lfs -text
|
37 |
+
images/dog1.jpeg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
pokemon_transferlearning.keras filter=lfs diff=lfs merge=lfs -text
|
39 |
+
pokemons/train/Charmeleon/00000027.png filter=lfs diff=lfs merge=lfs -text
|
40 |
+
pokemons/train/Charmeleon/00000207.png filter=lfs diff=lfs merge=lfs -text
|
41 |
+
pokemons/train/Hitmonchan/00000015.png filter=lfs diff=lfs merge=lfs -text
|
42 |
+
pokemons/train/Hitmonchan/00000025.png filter=lfs diff=lfs merge=lfs -text
|
43 |
+
pokemons/train/Hitmonchan/00000038.gif filter=lfs diff=lfs merge=lfs -text
|
44 |
+
pokemons/train/Hitmonchan/00000099.gif filter=lfs diff=lfs merge=lfs -text
|
45 |
+
pokemons/train/Hitmonchan/00000111.png filter=lfs diff=lfs merge=lfs -text
|
46 |
+
pokemons/train/Hitmonchan/00000169.png filter=lfs diff=lfs merge=lfs -text
|
47 |
+
pokemons/train/Pikachu/00000031.png filter=lfs diff=lfs merge=lfs -text
|
48 |
+
pokemons/train/Pikachu/00000034.png filter=lfs diff=lfs merge=lfs -text
|
49 |
+
pokemons/train/Pikachu/00000122.png filter=lfs diff=lfs merge=lfs -text
|
50 |
+
pokemons/val/Pikachu/00000154.jpg filter=lfs diff=lfs merge=lfs -text
|
51 |
+
pokemons/val/Pikachu/00000188.gif filter=lfs diff=lfs merge=lfs -text
|
52 |
+
pokemons/val/Pikachu/00000212.png filter=lfs diff=lfs merge=lfs -text
|
53 |
+
pokemons/val/Pikachu/00000225.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: purple
|
5 |
-
colorTo: pink
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.27.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: pokemon1
|
3 |
+
app_file: app.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.27.0
|
|
|
|
|
6 |
---
|
|
|
|
app.ipynb
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stderr",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"/Users/loic/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020\n",
|
13 |
+
" warnings.warn(\n",
|
14 |
+
"/Users/loic/Library/Python/3.9/lib/python/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
15 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
16 |
+
]
|
17 |
+
}
|
18 |
+
],
|
19 |
+
"source": [
|
20 |
+
"import gradio as gr\n",
|
21 |
+
"import tensorflow as tf\n",
|
22 |
+
"import numpy as np\n",
|
23 |
+
"from PIL import Image"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": 2,
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"model_path = \"dogs-vs-cats-model_transferlearning.keras\"\n",
|
33 |
+
"model = tf.keras.models.load_model(model_path)"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 9,
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [],
|
41 |
+
"source": [
|
42 |
+
"# Define the core prediction function\n",
|
43 |
+
"def predict_cat_dog(image):\n",
|
44 |
+
" # Preprocess image\n",
|
45 |
+
" print(type(image))\n",
|
46 |
+
" image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image\n",
|
47 |
+
" image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale\n",
|
48 |
+
" image = np.array(image)\n",
|
49 |
+
" image = np.expand_dims(image, axis=0) # same as image[None, ...]\n",
|
50 |
+
" \n",
|
51 |
+
" # Predict\n",
|
52 |
+
" prediction = model.predict(image)\n",
|
53 |
+
" \n",
|
54 |
+
" # Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability\n",
|
55 |
+
" # we could also change the output layer to dense(1, activation='sigmoid')\n",
|
56 |
+
" prediction = np.round(float(tf.sigmoid(prediction)[0]), 2)\n",
|
57 |
+
" p_cat = (1 - prediction)\n",
|
58 |
+
" p_dog = prediction\n",
|
59 |
+
" return {'cat': p_cat, 'dog': p_dog}"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 3,
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [
|
67 |
+
{
|
68 |
+
"data": {
|
69 |
+
"text/plain": [
|
70 |
+
"<tf.Tensor: shape=(), dtype=float32, numpy=0.6341356>"
|
71 |
+
]
|
72 |
+
},
|
73 |
+
"execution_count": 3,
|
74 |
+
"metadata": {},
|
75 |
+
"output_type": "execute_result"
|
76 |
+
}
|
77 |
+
],
|
78 |
+
"source": [
|
79 |
+
"tf.sigmoid(0.55)"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"execution_count": 10,
|
85 |
+
"metadata": {},
|
86 |
+
"outputs": [
|
87 |
+
{
|
88 |
+
"name": "stdout",
|
89 |
+
"output_type": "stream",
|
90 |
+
"text": [
|
91 |
+
"Running on local URL: http://127.0.0.1:7863\n",
|
92 |
+
"\n",
|
93 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"data": {
|
98 |
+
"text/html": [
|
99 |
+
"<div><iframe src=\"http://127.0.0.1:7863/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
100 |
+
],
|
101 |
+
"text/plain": [
|
102 |
+
"<IPython.core.display.HTML object>"
|
103 |
+
]
|
104 |
+
},
|
105 |
+
"metadata": {},
|
106 |
+
"output_type": "display_data"
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"data": {
|
110 |
+
"text/plain": []
|
111 |
+
},
|
112 |
+
"execution_count": 10,
|
113 |
+
"metadata": {},
|
114 |
+
"output_type": "execute_result"
|
115 |
+
},
|
116 |
+
{
|
117 |
+
"name": "stdout",
|
118 |
+
"output_type": "stream",
|
119 |
+
"text": [
|
120 |
+
"<class 'numpy.ndarray'>\n",
|
121 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 32ms/step\n"
|
122 |
+
]
|
123 |
+
},
|
124 |
+
{
|
125 |
+
"name": "stderr",
|
126 |
+
"output_type": "stream",
|
127 |
+
"text": [
|
128 |
+
"Traceback (most recent call last):\n",
|
129 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/gradio/queueing.py\", line 527, in process_events\n",
|
130 |
+
" response = await route_utils.call_process_api(\n",
|
131 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/gradio/route_utils.py\", line 261, in call_process_api\n",
|
132 |
+
" output = await app.get_blocks().process_api(\n",
|
133 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/gradio/blocks.py\", line 1786, in process_api\n",
|
134 |
+
" result = await self.call_function(\n",
|
135 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/gradio/blocks.py\", line 1338, in call_function\n",
|
136 |
+
" prediction = await anyio.to_thread.run_sync(\n",
|
137 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/anyio/to_thread.py\", line 56, in run_sync\n",
|
138 |
+
" return await get_async_backend().run_sync_in_worker_thread(\n",
|
139 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/anyio/_backends/_asyncio.py\", line 2144, in run_sync_in_worker_thread\n",
|
140 |
+
" return await future\n",
|
141 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/anyio/_backends/_asyncio.py\", line 851, in run\n",
|
142 |
+
" result = context.run(func, *args)\n",
|
143 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/gradio/utils.py\", line 759, in wrapper\n",
|
144 |
+
" response = f(*args, **kwargs)\n",
|
145 |
+
" File \"/var/folders/vr/l64rqhls46j_2hyn4pdl0m880000gn/T/ipykernel_56385/4113486017.py\", line 15, in predict_cat_dog\n",
|
146 |
+
" prediction = np.round(float(tf.sigmoid(prediction)[0]), 2)\n",
|
147 |
+
" File \"/Users/loic/Library/Python/3.9/lib/python/site-packages/tensorflow/python/framework/ops.py\", line 307, in __float__\n",
|
148 |
+
" return float(self._numpy())\n",
|
149 |
+
"TypeError: only length-1 arrays can be converted to Python scalars\n"
|
150 |
+
]
|
151 |
+
}
|
152 |
+
],
|
153 |
+
"source": [
|
154 |
+
"# Create the Gradio interface\n",
|
155 |
+
"input_image = gr.Image()\n",
|
156 |
+
"iface = gr.Interface(\n",
|
157 |
+
" fn=predict_cat_dog,\n",
|
158 |
+
" inputs=input_image, \n",
|
159 |
+
" outputs=gr.Label(),\n",
|
160 |
+
" examples=[\"images/cat1.jpeg\", \"images/cat2.jpeg\", \"images/cat3.jpeg\", \"images/cat4.jpeg\", \"images/dog1.jpeg\", \"images/dog2.jpeg\", \"images/dog3.jpeg\"], \n",
|
161 |
+
" description=\"A simple mlp classification model for image classification using the mnist dataset.\")\n",
|
162 |
+
"iface.launch()"
|
163 |
+
]
|
164 |
+
}
|
165 |
+
],
|
166 |
+
"metadata": {
|
167 |
+
"kernelspec": {
|
168 |
+
"display_name": "venv_new",
|
169 |
+
"language": "python",
|
170 |
+
"name": "python3"
|
171 |
+
},
|
172 |
+
"language_info": {
|
173 |
+
"codemirror_mode": {
|
174 |
+
"name": "ipython",
|
175 |
+
"version": 3
|
176 |
+
},
|
177 |
+
"file_extension": ".py",
|
178 |
+
"mimetype": "text/x-python",
|
179 |
+
"name": "python",
|
180 |
+
"nbconvert_exporter": "python",
|
181 |
+
"pygments_lexer": "ipython3",
|
182 |
+
"version": "3.9.6"
|
183 |
+
}
|
184 |
+
},
|
185 |
+
"nbformat": 4,
|
186 |
+
"nbformat_minor": 2
|
187 |
+
}
|
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load the pre-trained model
|
7 |
+
model = tf.keras.models.load_model('pokemon_transferlearning.keras')
|
8 |
+
|
9 |
+
def classify_image(img):
|
10 |
+
|
11 |
+
if isinstance(img, np.ndarray):
|
12 |
+
img = Image.fromarray(img.astype('uint8'), 'RGB')
|
13 |
+
|
14 |
+
# Preprocess the image to fit the model's input requirements
|
15 |
+
img = img.resize((150, 150)) # Resize the image using PIL, which is intended here
|
16 |
+
img_array = np.array(img) # Convert the resized PIL image to a numpy array
|
17 |
+
img_array = img_array / 255.0 # Normalize pixel values to [0, 1]
|
18 |
+
img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to fit model input shape
|
19 |
+
|
20 |
+
# Make prediction
|
21 |
+
prediction = model.predict(img_array)
|
22 |
+
|
23 |
+
# prediction = np.round(float(tf.sigmoid(prediction)), 2)
|
24 |
+
# p_cat = (1 - prediction)
|
25 |
+
# p_dog = prediction
|
26 |
+
# return {'cat': p_cat, 'dog': p_dog}
|
27 |
+
|
28 |
+
print(prediction)
|
29 |
+
|
30 |
+
probabilities = tf.sigmoid(prediction).numpy() # Convert tensor to numpy array if using
|
31 |
+
|
32 |
+
# Formatting the probabilities
|
33 |
+
class_names = ['Hitchoman', 'Pikachu', 'Charmeleon']
|
34 |
+
results = {class_names[i]: float(prediction[0][i]) for i in range(3)} # Convert each probability to float
|
35 |
+
|
36 |
+
return results
|
37 |
+
|
38 |
+
# Create Gradio interface
|
39 |
+
iface = gr.Interface(fn=classify_image,
|
40 |
+
inputs=gr.Image(),
|
41 |
+
outputs=gr.Label(num_top_classes=3),
|
42 |
+
title="Pokemon Classifier",
|
43 |
+
description="Upload an image of a pokemon classify.")
|
44 |
+
|
45 |
+
# Launch the application
|
46 |
+
iface.launch(share=True)
|
app_cat_dog.ipynb
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 6,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Running on local URL: http://127.0.0.1:7865\n",
|
13 |
+
"\n",
|
14 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"data": {
|
19 |
+
"text/html": [
|
20 |
+
"<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>"
|
21 |
+
],
|
22 |
+
"text/plain": [
|
23 |
+
"<IPython.core.display.HTML object>"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
"metadata": {},
|
27 |
+
"output_type": "display_data"
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"data": {
|
31 |
+
"text/plain": []
|
32 |
+
},
|
33 |
+
"execution_count": 6,
|
34 |
+
"metadata": {},
|
35 |
+
"output_type": "execute_result"
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"name": "stdout",
|
39 |
+
"output_type": "stream",
|
40 |
+
"text": [
|
41 |
+
"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",
|
42 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 608ms/step\n",
|
43 |
+
"[[0.49759743 0.50240254]]\n"
|
44 |
+
]
|
45 |
+
}
|
46 |
+
],
|
47 |
+
"source": [
|
48 |
+
"import gradio as gr\n",
|
49 |
+
"import tensorflow as tf\n",
|
50 |
+
"from PIL import Image\n",
|
51 |
+
"import numpy as np\n",
|
52 |
+
"\n",
|
53 |
+
"# Load the pre-trained model\n",
|
54 |
+
"model = tf.keras.models.load_model('dogs-vs-cats-model_transferlearning.keras')\n",
|
55 |
+
"\n",
|
56 |
+
"def classify_image(img):\n",
|
57 |
+
" \n",
|
58 |
+
" if isinstance(img, np.ndarray):\n",
|
59 |
+
" img = Image.fromarray(img.astype('uint8'), 'RGB')\n",
|
60 |
+
"\n",
|
61 |
+
" # Preprocess the image to fit the model's input requirements\n",
|
62 |
+
" img = img.resize((150, 150)) # Resize the image using PIL, which is intended here\n",
|
63 |
+
" img_array = np.array(img) # Convert the resized PIL image to a numpy array\n",
|
64 |
+
" img_array = img_array / 255.0 # Normalize pixel values to [0, 1]\n",
|
65 |
+
" img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to fit model input shape\n",
|
66 |
+
"\n",
|
67 |
+
" # Make prediction\n",
|
68 |
+
" prediction = model.predict(img_array)\n",
|
69 |
+
"\n",
|
70 |
+
" # prediction = np.round(float(tf.sigmoid(prediction)), 2)\n",
|
71 |
+
" # p_cat = (1 - prediction)\n",
|
72 |
+
" # p_dog = prediction\n",
|
73 |
+
" # return {'cat': p_cat, 'dog': p_dog}\n",
|
74 |
+
"\n",
|
75 |
+
" print(prediction)\n",
|
76 |
+
"\n",
|
77 |
+
" probabilities = tf.sigmoid(prediction).numpy() # Convert tensor to numpy array if using \n",
|
78 |
+
"\n",
|
79 |
+
" # Formatting the probabilities\n",
|
80 |
+
" class_names = ['Cat', 'Dog']\n",
|
81 |
+
" results = {class_names[i]: float(prediction[0][i]) for i in range(2)} # Convert each probability to float\n",
|
82 |
+
" \n",
|
83 |
+
" return results\n",
|
84 |
+
"\n",
|
85 |
+
"# Create Gradio interface\n",
|
86 |
+
"iface = gr.Interface(fn=classify_image,\n",
|
87 |
+
" inputs=gr.Image(),\n",
|
88 |
+
" outputs=gr.Label(num_top_classes=2),\n",
|
89 |
+
" title=\"Cat vs Dog Classifier\",\n",
|
90 |
+
" description=\"Upload an image of a cat or dog to classify.\")\n",
|
91 |
+
"\n",
|
92 |
+
"# Launch the application\n",
|
93 |
+
"iface.launch()\n"
|
94 |
+
]
|
95 |
+
}
|
96 |
+
],
|
97 |
+
"metadata": {
|
98 |
+
"kernelspec": {
|
99 |
+
"display_name": "Python 3",
|
100 |
+
"language": "python",
|
101 |
+
"name": "python3"
|
102 |
+
},
|
103 |
+
"language_info": {
|
104 |
+
"codemirror_mode": {
|
105 |
+
"name": "ipython",
|
106 |
+
"version": 3
|
107 |
+
},
|
108 |
+
"file_extension": ".py",
|
109 |
+
"mimetype": "text/x-python",
|
110 |
+
"name": "python",
|
111 |
+
"nbconvert_exporter": "python",
|
112 |
+
"pygments_lexer": "ipython3",
|
113 |
+
"version": "3.9.6"
|
114 |
+
}
|
115 |
+
},
|
116 |
+
"nbformat": 4,
|
117 |
+
"nbformat_minor": 2
|
118 |
+
}
|
app_pokemon.ipynb
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 3,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Running on local URL: http://127.0.0.1:7862\n",
|
13 |
+
"Running on public URL: https://59007d69f4a41b96f2.gradio.live\n",
|
14 |
+
"\n",
|
15 |
+
"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"
|
16 |
+
]
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"data": {
|
20 |
+
"text/html": [
|
21 |
+
"<div><iframe src=\"https://59007d69f4a41b96f2.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
22 |
+
],
|
23 |
+
"text/plain": [
|
24 |
+
"<IPython.core.display.HTML object>"
|
25 |
+
]
|
26 |
+
},
|
27 |
+
"metadata": {},
|
28 |
+
"output_type": "display_data"
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"data": {
|
32 |
+
"text/plain": []
|
33 |
+
},
|
34 |
+
"execution_count": 3,
|
35 |
+
"metadata": {},
|
36 |
+
"output_type": "execute_result"
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"name": "stdout",
|
40 |
+
"output_type": "stream",
|
41 |
+
"text": [
|
42 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 628ms/step\n",
|
43 |
+
"[[0.33698466 0.33483982 0.32817554]]\n",
|
44 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 35ms/step\n",
|
45 |
+
"[[0.33667472 0.33442825 0.32889706]]\n"
|
46 |
+
]
|
47 |
+
}
|
48 |
+
],
|
49 |
+
"source": [
|
50 |
+
"import gradio as gr\n",
|
51 |
+
"import tensorflow as tf\n",
|
52 |
+
"from PIL import Image\n",
|
53 |
+
"import numpy as np\n",
|
54 |
+
"\n",
|
55 |
+
"# Load the pre-trained model\n",
|
56 |
+
"model = tf.keras.models.load_model('pokemon_transferlearning.keras')\n",
|
57 |
+
"\n",
|
58 |
+
"def classify_image(img):\n",
|
59 |
+
" \n",
|
60 |
+
" if isinstance(img, np.ndarray):\n",
|
61 |
+
" img = Image.fromarray(img.astype('uint8'), 'RGB')\n",
|
62 |
+
"\n",
|
63 |
+
" # Preprocess the image to fit the model's input requirements\n",
|
64 |
+
" img = img.resize((150, 150)) # Resize the image using PIL, which is intended here\n",
|
65 |
+
" img_array = np.array(img) # Convert the resized PIL image to a numpy array\n",
|
66 |
+
" img_array = img_array / 255.0 # Normalize pixel values to [0, 1]\n",
|
67 |
+
" img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to fit model input shape\n",
|
68 |
+
"\n",
|
69 |
+
" # Make prediction\n",
|
70 |
+
" prediction = model.predict(img_array)\n",
|
71 |
+
"\n",
|
72 |
+
" # prediction = np.round(float(tf.sigmoid(prediction)), 2)\n",
|
73 |
+
" # p_cat = (1 - prediction)\n",
|
74 |
+
" # p_dog = prediction\n",
|
75 |
+
" # return {'cat': p_cat, 'dog': p_dog}\n",
|
76 |
+
"\n",
|
77 |
+
" print(prediction)\n",
|
78 |
+
"\n",
|
79 |
+
" probabilities = tf.sigmoid(prediction).numpy() # Convert tensor to numpy array if using \n",
|
80 |
+
"\n",
|
81 |
+
" # Formatting the probabilities\n",
|
82 |
+
" class_names = ['Hitchoman', 'Pikachu', 'Charmeleon']\n",
|
83 |
+
" results = {class_names[i]: float(prediction[0][i]) for i in range(3)} # Convert each probability to float\n",
|
84 |
+
" \n",
|
85 |
+
" return results\n",
|
86 |
+
"\n",
|
87 |
+
"# Create Gradio interface\n",
|
88 |
+
"iface = gr.Interface(fn=classify_image,\n",
|
89 |
+
" inputs=gr.Image(),\n",
|
90 |
+
" outputs=gr.Label(num_top_classes=3),\n",
|
91 |
+
" title=\"Pokemon Classifier\",\n",
|
92 |
+
" description=\"Upload an image of a pokemon to classify.\")\n",
|
93 |
+
"\n",
|
94 |
+
"# Launch the application\n",
|
95 |
+
"iface.launch(share=True)\n"
|
96 |
+
]
|
97 |
+
}
|
98 |
+
],
|
99 |
+
"metadata": {
|
100 |
+
"kernelspec": {
|
101 |
+
"display_name": "Python 3",
|
102 |
+
"language": "python",
|
103 |
+
"name": "python3"
|
104 |
+
},
|
105 |
+
"language_info": {
|
106 |
+
"codemirror_mode": {
|
107 |
+
"name": "ipython",
|
108 |
+
"version": 3
|
109 |
+
},
|
110 |
+
"file_extension": ".py",
|
111 |
+
"mimetype": "text/x-python",
|
112 |
+
"name": "python",
|
113 |
+
"nbconvert_exporter": "python",
|
114 |
+
"pygments_lexer": "ipython3",
|
115 |
+
"version": "3.9.6"
|
116 |
+
}
|
117 |
+
},
|
118 |
+
"nbformat": 4,
|
119 |
+
"nbformat_minor": 2
|
120 |
+
}
|
dogs-vs-cats-model_transferlearning.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be78409d31d21d15c94cdeca9546556a421e9d5f5535f5b71cb185c3a2a95205
|
3 |
+
size 83967496
|
dogs-vs-cats/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
dogs-vs-cats/train/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
dogs-vs-cats/train/cat/Abyssinian_127_jpg.rf.b2553880d73a78e26e396fe7775f31be.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Abyssinian_131_jpg.rf.3aeee0801499f335e3a59b51189e1351.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Abyssinian_149_jpg.rf.b6b4a72a93f8bc2b239b18c5d181190c.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Abyssinian_15_jpg.rf.7bc44eeb9865e35cb1d5f9ef4f53b381.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Abyssinian_167_jpg.rf.1d367cb1cf82c1e5ebda79fda928cee5.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_103_jpg.rf.bb684501a53abf381a6bf07f9cd2243c.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_109_jpg.rf.aed42b8605ef92b579992d02e2e2cc4c.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_116_jpg.rf.8c6280ad977c6c749da4d35156e49286.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_13_jpg.rf.7c75131d257406a75d8d676dd9b5f5f7.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_145_jpg.rf.3dadbfbb39437890fc249f7edb37cb2f.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_150_jpg.rf.05c93a40014062c5ae66bae50a857aca.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_165_jpg.rf.31185846cf5892c6e7e3f02784e04fef.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_167_jpg.rf.1542d3551f036a577880f22719c2fd08.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bengal_174_jpg.rf.bbac947432fbff310b6be201bd9a3de7.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_105_jpg.rf.2f96b7dd47bb15df4def745602f5434d.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_135_jpg.rf.d9cdc64e747de2078f60a4761fdc534a.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_144_jpg.rf.369cb41f763175f21c7cfd77e77509ec.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_151_jpg.rf.179ae9238f29eda8d63af4da89c48e05.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_157_jpg.rf.cbf2d211aab989a6f26ef5b5cef5e63e.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_159_jpg.rf.3ba8a57e52d925aa33e85822004c591d.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_160_jpg.rf.dc1ca8f5f9cfd2aa585a7c024074ce7f.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_166_jpg.rf.ac8785ee575600493ffac45e2b0d1342.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_173_jpg.rf.b8656131a2f2fda3413154a7d01ad834.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_186_jpg.rf.6c15b2f2afe3c230ded5e5d04d6affe5.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Birman_189_jpg.rf.29a9d3b4f764fc9e2cc6b78fbe5d30da.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_122_jpg.rf.218d47f5275b4de8706a9a56e6276113.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_125_jpg.rf.c91236394fb2adb244ba8866b53dbd43.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_127_jpg.rf.4c9fde3d1b3fc52a20cd4b2b82b98856.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_140_jpg.rf.15757f698af74453f34ee58604ea6afc.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_146_jpg.rf.288e58c8b226aa5b7c6ad4442f7fda5c.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_153_jpg.rf.2da5201d989106a5b1b4025aec3349b4.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/Bombay_18_jpg.rf.68c07a9d844cc01b8bf5582d7d3c0e01.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_101_jpg.rf.afa7cf2990a151279e674e2077fa306f.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_109_jpg.rf.97fcd6a9c7f0d54b3d39d2d709237287.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_154_jpg.rf.8e78c62b5e103771db2893338e5937da.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_164_jpg.rf.8d27185e8344301592da98df828ab073.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_166_jpg.rf.d18846beff566700c2e16e295bb701e4.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_177_jpg.rf.5efbf04740eeac07cdd83e5773d24221.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_187_jpg.rf.24d36402bce6b82092dc93876c65bb71.jpg
ADDED
![]() |
dogs-vs-cats/train/cat/British_Shorthair_193_jpg.rf.ab376d75fb05acc1ec59f06228ebe5bb.jpg
ADDED
![]() |