Spaces:
Sleeping
Sleeping
File size: 10,150 Bytes
38783f2 a1fb560 38783f2 a1fb560 38783f2 a1fb560 38783f2 a1fb560 38783f2 a1fb560 38783f2 a1fb560 38783f2 a1fb560 38783f2 2b7215d 38783f2 2b7215d 38783f2 2b7215d 38783f2 |
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4b40cb3a-544a-4b23-8c00-431cb7133130",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.11.5\n"
]
}
],
"source": [
"%%bash\n",
"python --version"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "93e1afb8-f78c-4862-9d56-a06a3559b4d1",
"metadata": {},
"outputs": [],
"source": [
"#|default_exp app"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2adf2fa8-199b-48e4-a91c-9a093032480c",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"from fastai.vision.all import *\n",
"import gradio as gr\n",
"import timm"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f0218bf1-1836-4d7a-8d47-33584471f28b",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"learn = load_learner('model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "168ac2e4-f83b-4ce0-8f23-00999eb5d556",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"categories = learn.dls.vocab\n",
"\n",
"def classify_image(img):\n",
" pred,idx,probs = learn.predict(img)\n",
" return dict(zip(categories, map(float,probs)))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d343a0d3-40fd-4502-a86b-cb3bac9fdf7f",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"examples = ['images/unicycle.jpeg', 'images/bicycle.jpeg', 'images/tricycle.png']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "645eb0ee-b7e5-4ec4-a42e-9f43a163a3a5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"images/unicycle.jpeg is a tricycle\n"
]
},
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"images/bicycle.jpeg is a bicycle\n"
]
},
{
"ename": "UnidentifiedImageError",
"evalue": "cannot identify image file 'images/tricycle.png'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUnidentifiedImageError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m example \u001b[38;5;129;01min\u001b[39;00m examples:\n\u001b[0;32m----> 2\u001b[0m image \u001b[38;5;241m=\u001b[39m PILImage\u001b[38;5;241m.\u001b[39mcreate(example)\n\u001b[1;32m 3\u001b[0m res_dict \u001b[38;5;241m=\u001b[39m classify_image(image)\n\u001b[1;32m 4\u001b[0m top_prob_key \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mmax\u001b[39m(res_dict, key\u001b[38;5;241m=\u001b[39mres_dict\u001b[38;5;241m.\u001b[39mget)\n",
"File \u001b[0;32m~/miniconda3/lib/python3.11/site-packages/fastai/vision/core.py:125\u001b[0m, in \u001b[0;36mPILBase.create\u001b[0;34m(cls, fn, **kwargs)\u001b[0m\n\u001b[1;32m 123\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,\u001b[38;5;28mbytes\u001b[39m): fn \u001b[38;5;241m=\u001b[39m io\u001b[38;5;241m.\u001b[39mBytesIO(fn)\n\u001b[1;32m 124\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,Image\u001b[38;5;241m.\u001b[39mImage): \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(fn)\n\u001b[0;32m--> 125\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(load_image(fn, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mmerge(\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_open_args, kwargs)))\n",
"File \u001b[0;32m~/miniconda3/lib/python3.11/site-packages/fastai/vision/core.py:98\u001b[0m, in \u001b[0;36mload_image\u001b[0;34m(fn, mode)\u001b[0m\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload_image\u001b[39m(fn, mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 97\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOpen and load a `PIL.Image` and convert to `mode`\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 98\u001b[0m im \u001b[38;5;241m=\u001b[39m Image\u001b[38;5;241m.\u001b[39mopen(fn)\n\u001b[1;32m 99\u001b[0m im\u001b[38;5;241m.\u001b[39mload()\n\u001b[1;32m 100\u001b[0m im \u001b[38;5;241m=\u001b[39m im\u001b[38;5;241m.\u001b[39m_new(im\u001b[38;5;241m.\u001b[39mim)\n",
"File \u001b[0;32m~/miniconda3/lib/python3.11/site-packages/PIL/Image.py:3280\u001b[0m, in \u001b[0;36mopen\u001b[0;34m(fp, mode, formats)\u001b[0m\n\u001b[1;32m 3278\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message)\n\u001b[1;32m 3279\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot identify image file \u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (filename \u001b[38;5;28;01mif\u001b[39;00m filename \u001b[38;5;28;01melse\u001b[39;00m fp)\n\u001b[0;32m-> 3280\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m UnidentifiedImageError(msg)\n",
"\u001b[0;31mUnidentifiedImageError\u001b[0m: cannot identify image file 'images/tricycle.png'"
]
}
],
"source": [
"for example in examples:\n",
" image = PILImage.create(example)\n",
" res_dict = classify_image(image)\n",
" top_prob_key = max(res_dict, key=res_dict.get)\n",
" print(example + ' is a '+ top_prob_key)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "156a1fa0-e124-4a18-b411-367e7926afa4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7860\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" 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": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#|export\n",
"\n",
"image = gr.Image()\n",
"label = gr.Label()\n",
"\n",
"intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
"intf.launch()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2894f2be-e453-4795-8a16-2aa4770aa16d",
"metadata": {},
"outputs": [],
"source": [
"import nbdev"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "10568397-2167-4c39-8120-436e577b452d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Export successful\n"
]
}
],
"source": [
"nbdev.export.nb_export('notebook.ipynb', '')\n",
"print('Export successful')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|