Upload 4 files
Browse filesAdd: keras file, app.ipynb
- .gitattributes +1 -0
- Abra-vs-Pikachu-vs-Zubat-model_transferlearning.keras +3 -0
- app.ipynb +141 -0
- my_app.py +45 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ 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 |
+
Abra-vs-Pikachu-vs-Zubat-model_transferlearning.keras filter=lfs diff=lfs merge=lfs -text
|
Abra-vs-Pikachu-vs-Zubat-model_transferlearning.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a4190ad1a47e2afd5cebe970c0de99894433f688ff2782dc6358f6338dc248c3
|
3 |
+
size 250560147
|
app.ipynb
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stderr",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"/Users/sonah/Library/CloudStorage/OneDrive-ZHAW/AI/Task2/myvenv/lib/python3.11/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",
|
13 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
14 |
+
]
|
15 |
+
}
|
16 |
+
],
|
17 |
+
"source": [
|
18 |
+
"import gradio as gr\n",
|
19 |
+
"import tensorflow as tf\n",
|
20 |
+
"import numpy as np\n",
|
21 |
+
"from PIL import Image"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "code",
|
26 |
+
"execution_count": 2,
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"model_path = \"Abra-vs-Pikachu-vs-Zubat-model_transferlearning.keras\"\n",
|
31 |
+
"model = tf.keras.models.load_model(model_path)"
|
32 |
+
]
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"cell_type": "code",
|
36 |
+
"execution_count": 3,
|
37 |
+
"metadata": {},
|
38 |
+
"outputs": [],
|
39 |
+
"source": [
|
40 |
+
"# Define the core prediction function\n",
|
41 |
+
"def predict_pokemon(image):\n",
|
42 |
+
" # Preprocess image\n",
|
43 |
+
" print(type(image))\n",
|
44 |
+
" image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image\n",
|
45 |
+
" image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale\n",
|
46 |
+
" image = np.array(image)\n",
|
47 |
+
" image = np.expand_dims(image, axis=0) # same as image[None, ...]\n",
|
48 |
+
" \n",
|
49 |
+
" # Predict\n",
|
50 |
+
" prediction = model.predict(image)\n",
|
51 |
+
" \n",
|
52 |
+
" # Apply sigmoid to get probabilities\n",
|
53 |
+
" prediction_prob = tf.sigmoid(prediction).numpy()\n",
|
54 |
+
"\n",
|
55 |
+
"\n",
|
56 |
+
" # Extract probabilities for Abra and Pikachu and Zubat\n",
|
57 |
+
" p_Abra = round(prediction_prob[0][0], 2)\n",
|
58 |
+
" p_Pikachu = round(prediction_prob[0][1], 2)\n",
|
59 |
+
" p_Zubat = round(prediction_prob[0][2], 2)\n",
|
60 |
+
" \n",
|
61 |
+
" \n",
|
62 |
+
" return {'Abra': p_Abra, 'Pikachu': p_Pikachu, 'Zubat': p_Zubat}"
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"execution_count": 4,
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [
|
70 |
+
{
|
71 |
+
"name": "stdout",
|
72 |
+
"output_type": "stream",
|
73 |
+
"text": [
|
74 |
+
"Running on local URL: http://127.0.0.1:7861\n",
|
75 |
+
"\n",
|
76 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"data": {
|
81 |
+
"text/html": [
|
82 |
+
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
83 |
+
],
|
84 |
+
"text/plain": [
|
85 |
+
"<IPython.core.display.HTML object>"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
"metadata": {},
|
89 |
+
"output_type": "display_data"
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"data": {
|
93 |
+
"text/plain": []
|
94 |
+
},
|
95 |
+
"execution_count": 4,
|
96 |
+
"metadata": {},
|
97 |
+
"output_type": "execute_result"
|
98 |
+
}
|
99 |
+
],
|
100 |
+
"source": [
|
101 |
+
"# Create the Gradio interface\n",
|
102 |
+
"input_image = gr.Image()\n",
|
103 |
+
"iface = gr.Interface(\n",
|
104 |
+
" fn=predict_pokemon,\n",
|
105 |
+
" inputs=input_image, \n",
|
106 |
+
" outputs=gr.Label(),\n",
|
107 |
+
" examples=[\"images/Abra1.png\", \"images/Abra2.jpg\", \"images/Abra3.jpg\", \"images/Pikachu1.jpg\", \"images/Pikachu2.png\", \"images/Pikachu3.jpg\", \"images/Zubat1.jpg\",\"images/Zubat2.jpg\",\"images/Zubat3.jpg\"], \n",
|
108 |
+
" description=\"A simple mlp classification model for image classification using the mnist dataset.\")\n",
|
109 |
+
"iface.launch()"
|
110 |
+
]
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"cell_type": "code",
|
114 |
+
"execution_count": null,
|
115 |
+
"metadata": {},
|
116 |
+
"outputs": [],
|
117 |
+
"source": []
|
118 |
+
}
|
119 |
+
],
|
120 |
+
"metadata": {
|
121 |
+
"kernelspec": {
|
122 |
+
"display_name": "venv_new",
|
123 |
+
"language": "python",
|
124 |
+
"name": "python3"
|
125 |
+
},
|
126 |
+
"language_info": {
|
127 |
+
"codemirror_mode": {
|
128 |
+
"name": "ipython",
|
129 |
+
"version": 3
|
130 |
+
},
|
131 |
+
"file_extension": ".py",
|
132 |
+
"mimetype": "text/x-python",
|
133 |
+
"name": "python",
|
134 |
+
"nbconvert_exporter": "python",
|
135 |
+
"pygments_lexer": "ipython3",
|
136 |
+
"version": "3.11.8"
|
137 |
+
}
|
138 |
+
},
|
139 |
+
"nbformat": 4,
|
140 |
+
"nbformat_minor": 2
|
141 |
+
}
|
my_app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
model_path = "Abra-vs-Pikachu-vs-Zubat-model_transferlearning.keras"
|
7 |
+
model = tf.keras.models.load_model(model_path)
|
8 |
+
|
9 |
+
# Define the core prediction function
|
10 |
+
|
11 |
+
|
12 |
+
def predict_pokemon(image):
|
13 |
+
# Preprocess image
|
14 |
+
print(type(image))
|
15 |
+
# Convert numpy array to PIL image
|
16 |
+
image = Image.fromarray(image.astype('uint8'))
|
17 |
+
# resize the image to 28x28 and converts it to gray scale
|
18 |
+
image = image.resize((150, 150))
|
19 |
+
image = np.array(image)
|
20 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
21 |
+
|
22 |
+
# Predict
|
23 |
+
prediction = model.predict(image)
|
24 |
+
|
25 |
+
# Apply sigmoid to get probabilities
|
26 |
+
prediction_prob = tf.sigmoid(prediction).numpy()
|
27 |
+
|
28 |
+
# Extract probabilities for Abra and Pikachu and Zubat
|
29 |
+
p_Abra = round(prediction_prob[0][0], 2)
|
30 |
+
p_Pikachu = round(prediction_prob[0][1], 2)
|
31 |
+
p_Zubat = round(prediction_prob[0][2], 2)
|
32 |
+
|
33 |
+
return {'Abra': p_Abra, 'Pikachu': p_Pikachu, 'Zubat': p_Zubat}
|
34 |
+
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
input_image = gr.Image()
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=predict_pokemon,
|
40 |
+
inputs=input_image,
|
41 |
+
outputs=gr.Label(),
|
42 |
+
examples=["images/Abra1.png", "images/Abra2.jpg", "images/Abra3.jpg", "images/Pikachu1.jpg",
|
43 |
+
"images/Pikachu2.png", "images/Pikachu3.jpg", "images/Zubat1.jpg", "images/Zubat2.jpg", "images/Zubat3.jpg"],
|
44 |
+
description="A simple mlp classification model for image classification using the mnist dataset.")
|
45 |
+
iface.launch()
|