{ "cells": [ { "cell_type": "code", "execution_count": 4, "id": "2ccea5e4", "metadata": {}, "outputs": [], "source": [ "#|default_exp app\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1d47b7c1", "metadata": {}, "outputs": [], "source": [ "# Make sure we've got the latest version of fastai:\n", "!pip install -Uqq fastai gradio\n", "!pip install -Uqq fastbook" ] }, { "cell_type": "code", "execution_count": 12, "id": "553b0061", "metadata": {}, "outputs": [], "source": [ "#|export\n", "import fastbook\n", "fastbook.setup_book()" ] }, { "cell_type": "code", "execution_count": 32, "id": "efc0850a", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastbook import *\n", "from fastai.vision.widgets import *\n", "from fastai.vision.all import *\n", "from urllib.request import urlopen" ] }, { "cell_type": "markdown", "id": "c73e94bb", "metadata": {}, "source": [ "# Dogs vs Cats" ] }, { "cell_type": "code", "execution_count": 5, "id": "15421cd2", "metadata": { "scrolled": true }, "outputs": [], "source": [ "#|export\n", "import gradio as gr\n", "\n", "def is_cat(x): return x[0].issuper()" ] }, { "cell_type": "code", "execution_count": 64, "id": "f8743ab2", "metadata": {}, "outputs": [], "source": [ "ims = search_images_ddg('dog', max_images=3)\n", "dest = 'images/'\n", "download_images(dest, urls=ims)" ] }, { "cell_type": "code", "execution_count": 38, "id": "91067f42", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(#9) [Path('images/3cf5e7be-7aa0-4f7d-b585-ab7bd97d6d95.jpg'),Path('images/4d3b88ed-d0dc-43ec-bbaf-6babf6f310a7.jpg'),Path('images/5c283719-addd-4dbf-9847-eda74d0af8e7.jpg'),Path('images/6c9ec27e-0dbf-4e92-8059-12ec91bfa37b.jpg'),Path('images/a8ceb639-1fbf-447f-856e-7cf9bc796475.jpg'),Path('images/cc15e76a-5cd9-471e-9620-ae608e1d6be6.jpg'),Path('images/d53d92d8-7958-4fb6-ba9d-5f1e27fe9ceb.jpg'),Path('images/dog.jpg'),Path('images/edca2fd3-9ef6-40a5-a091-446b1abdfb55.jpg')]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fns = get_image_files(dest)\n", "fns" ] }, { "cell_type": "code", "execution_count": 39, "id": "ba9916e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(#0) []" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "failed = verify_images(fns)\n", "failed" ] }, { "cell_type": "code", "execution_count": 40, "id": "cf2964df", "metadata": {}, "outputs": [], "source": [ "failed.map(Path.unlink);" ] }, { "cell_type": "markdown", "id": "a5deae21", "metadata": {}, "source": [ "# Load Learner" ] }, { "cell_type": "code", "execution_count": 44, "id": "13b84738", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('model.pkl')" ] }, { "cell_type": "code", "execution_count": 48, "id": "ad82eadc", "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": [ "('False', tensor(0), tensor([9.9998e-01, 1.5045e-05]))\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": [ "('False', tensor(0), tensor([1.0000e+00, 2.4750e-06]))\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": [ "('False', tensor(0), tensor([1.0000e+00, 2.2275e-06]))\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": [ "('False', tensor(0), tensor([1.0000e+00, 1.0369e-07]))\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": [ "('False', tensor(0), tensor([9.9997e-01, 2.7372e-05]))\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": [ "('False', tensor(0), tensor([0.9984, 0.0016]))\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": [ "('False', tensor(0), tensor([9.9941e-01, 5.8600e-04]))\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": [ "('False', tensor(0), tensor([9.9920e-01, 7.9651e-04]))\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": [ "('False', tensor(0), tensor([9.9999e-01, 9.5192e-06]))\n" ] } ], "source": [ "for path in fns:\n", " im = PILImage.create(path)\n", " im.thumbnail((192,192))\n", " print(learn.predict(im)) " ] }, { "cell_type": "code", "execution_count": 49, "id": "011cccba", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('Dog', 'Cat')\n", "def classify_image(img):\n", " pref,idx,probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": 50, "id": "16881a1b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{'Dog': 0.999957799911499, 'Cat': 4.219008405925706e-05}" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base_image = PILImage.create(fns[0])\n", "classify_image(base_image)" ] }, { "cell_type": "markdown", "id": "4c11585c", "metadata": {}, "source": [ "# Gradio" ] }, { "cell_type": "code", "execution_count": 74, "id": "69406102", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/inputs.py:257: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " warnings.warn(\n", "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/outputs.py:197: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " warnings.warn(\n", "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n", " warnings.warn(value)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192,192))\n", "label = gr.outputs.Label()\n", "examples = ['images/4d3b88ed-d0dc-43ec-bbaf-6babf6f310a7.jpg',\n", " 'images/5c283719-addd-4dbf-9847-eda74d0af8e7.jpg']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "markdown", "id": "38ae565f", "metadata": {}, "source": [ "# Exporting" ] }, { "cell_type": "code", "execution_count": 75, "id": "ef61159a", "metadata": {}, "outputs": [], "source": [ "!pip install -Uqq nbdev\n", "import nbdev" ] }, { "cell_type": "code", "execution_count": 77, "id": "5e35eaab", "metadata": {}, "outputs": [], "source": [ "nbdev.export.nb_export('app.ipynb', '.')" ] }, { "cell_type": "code", "execution_count": null, "id": "7f0bd0f2", "metadata": {}, "outputs": [], "source": [] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }