{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "6003f943-4355-446f-a48a-2ceb3b194f35", "metadata": {}, "outputs": [], "source": [ "#| default_exp app" ] }, { "cell_type": "markdown", "id": "120a54e9-3144-4bb7-ba4b-949c80951030", "metadata": {}, "source": [ "# Snake Image Classification" ] }, { "cell_type": "code", "execution_count": null, "id": "eda4b34e-199c-4358-baf8-235bc0b995c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.7.11'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import fastai\n", "fastai.__version__" ] }, { "cell_type": "code", "execution_count": null, "id": "dc77cb01-22f1-4d1c-bdd2-ab1caff5123a", "metadata": {}, "outputs": [], "source": [ "#| export\n", "from fastai.vision.all import *\n", "from huggingface_hub import from_pretrained_fastai\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": null, "id": "f8bdf35f-049b-4554-b32c-b07f0095336c", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "dc081e18e6b545c985d52e0d13010ece", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Fetching 4 files: 0%| | 0/4 [00:00\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", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Prediction of the genus: Masticophis\n", "Probability: 88.28%\n" ] } ], "source": [ "pred,idx,probs = learner.predict(im)\n", "print(f\"Prediction of the genus: {pred}\")\n", "print(f\"Probability: {100*probs[idx].item():.2f}%\")" ] }, { "cell_type": "code", "execution_count": null, "id": "1ed7a818-dc3c-4d67-937d-d142caf674f1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Path('demo-images/agkistrodon.png')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "snakes[2]" ] }, { "cell_type": "code", "execution_count": null, "id": "bd71a128-18f4-4bee-a0c1-86e719d941cc", "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": [ "Prediction of the genus: Agkistrodon\n", "Probability: 91.21%\n" ] } ], "source": [ "pred,idx,probs = learner.predict(snakes[2])\n", "print(f\"Prediction of the genus: {pred}\")\n", "print(f\"Probability: {100*probs[idx].item():.2f}%\")" ] }, { "cell_type": "code", "execution_count": null, "id": "9fb6688e-ebc8-4718-8793-5f073c46bd89", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Path('demo-images/nerodia.jpg')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "snakes[7]" ] }, { "cell_type": "code", "execution_count": null, "id": "bae66287-589f-476d-925c-02ce34aeb70b", "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": [ "Prediction of the genus: Nerodia\n", "Probability: 64.35%\n" ] } ], "source": [ "pred,idx,probs = learner.predict(snakes[7])\n", "print(f\"Prediction of the genus: {pred}\")\n", "print(f\"Probability: {100*probs[idx].item():.2f}%\")" ] }, { "cell_type": "code", "execution_count": null, "id": "797ec0c4-db66-4312-9d67-d4e008fbca0c", "metadata": {}, "outputs": [], "source": [ "#| export\n", "categories = tuple(learner.dls.vocab)\n", "\n", "def classify_image(img):\n", " pred,idx,probs = learner.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": null, "id": "0a9cf1d8-b2ac-44a1-88f3-778814fe69c5", "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": [ "{'Agkistrodon': 0.00013586886052507907,\n", " 'Crotalus': 0.010002714581787586,\n", " 'Lampropeltis': 0.00047056630137376487,\n", " 'Masticophis': 0.0002429549494991079,\n", " 'Micrurus': 0.00012576498556882143,\n", " 'Natrix': 0.3323909044265747,\n", " 'Nerodia': 0.6434937715530396,\n", " 'Pantherophis': 0.0035959137603640556,\n", " 'Tantilla': 0.00017943432612810284,\n", " 'Thamnophis': 0.009362072683870792}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classify_image(snakes[7])" ] }, { "cell_type": "code", "execution_count": null, "id": "21ab9dc4-c665-4847-b255-6e1135ebe716", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/eleven/mambaforge/envs/fastai/lib/python3.10/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", "/home/eleven/mambaforge/envs/fastai/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/home/eleven/mambaforge/envs/fastai/lib/python3.10/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", "/home/eleven/mambaforge/envs/fastai/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n", " warnings.warn(value)\n", "/home/eleven/mambaforge/envs/fastai/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `enable_queue` is deprecated in `Interface()`, please use it within `launch()` instead.\n", " warnings.warn(value)\n" ] }, { "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/plain": [] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| export\n", "title = \"Snake Image Classification\"\n", "\n", "description = \"\"\"\n", "This demo is an ongoing iteration of a [bigger project](https://github.com/jimmiemunyi/the-snake-project) meant to classify snakes as venomous or non-venomous.\n", "\n", "Currently, it can classify snakes into 10 genera.\n", "\n", "The model can be found here: https://huggingface.co/Jimmie/snake-image-classification\n", " \n", "\n", "Enjoy!\n", "\"\"\"\n", "\n", "article = \"Blog posts on how the model is being trained: .\"\n", "\n", "\n", "image = gr.inputs.Image(shape=(224, 224))\n", "label = gr.outputs.Label()\n", "examples = list(path.ls())\n", "\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples,\n", " title = title, description = description, article = article,\n", " enable_queue=True, cache_examples=False)\n", "intf.launch(inline=False)" ] }, { "cell_type": "markdown", "id": "4caf8f17-34a7-4c98-ac10-80ac3ff7c08b", "metadata": {}, "source": [ "# export" ] }, { "cell_type": "code", "execution_count": null, "id": "eabe8427-b99a-415b-ba02-43c80822c118", "metadata": {}, "outputs": [], "source": [ "from nbdev.export import nb_export" ] }, { "cell_type": "code", "execution_count": null, "id": "55d4181b-fb8c-425f-be5e-d52c3d423708", "metadata": {}, "outputs": [], "source": [ "nb_export('app.ipynb', lib_path='.')" ] }, { "cell_type": "code", "execution_count": null, "id": "90e76e03-90aa-414a-b203-ee306cc8c59f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }