{ "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", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "images/unicycle.jpeg is a tricycle\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "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": [ "
" ], "text/plain": [ "" ] }, "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 }