{ "cells": [ { "cell_type": "markdown", "id": "4b48236b-3e6f-4dec-ac45-5bb8c5f7ff45", "metadata": {}, "source": [ "
Deadpool™️ Detector
\n", "\n" ] }, { "cell_type": "markdown", "id": "731ffedc-3131-4d20-bed6-ecfcf0681d73", "metadata": {}, "source": [ "# Description\n", "First development app using deep learning techniques. Stepping stone in my journey.\n", "\n", "Given an image infer if it contains Deadpool in it." ] }, { "cell_type": "markdown", "id": "1afbc8dd-8b83-4603-8ab8-3724fca03fa3", "metadata": {}, "source": [ "# Train a model" ] }, { "cell_type": "markdown", "id": "3e2cfb0c-6ac4-4cd3-b66e-1eed809bb88d", "metadata": {}, "source": [ "## Training Data\n", "Using [SH2022 Dataset](https://www.kaggle.com/datasets/muhammadhananasghar/sh2022-dataset) there is a labeled set of Deadpool images." ] }, { "cell_type": "markdown", "id": "2549da81-e133-4f5d-93e2-e754885d9d99", "metadata": {}, "source": [ "Add data to a `DataLoaders` object and display a batch" ] }, { "cell_type": "code", "execution_count": null, "id": "d9fa3c5f-c90f-44bb-bf95-cfbc9698c97e", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "\n", "data_path = Path('/home/zeus/data/SH2022')\n", "\n", "dls = ImageDataLoaders.from_folder(\n", " path=data_path,\n", " train='train',\n", " valid='test/Deadpool',\n", " item_tfms=Resize(224),\n", " batch_tfms=aug_transforms()\n", ")\n", "dls.show_batch()" ] }, { "cell_type": "markdown", "id": "f4b6eb81-25b6-449c-8f48-f418c6e1cb7a", "metadata": {}, "source": [ "## Training" ] }, { "cell_type": "code", "execution_count": null, "id": "54353c83-2bf3-419d-a7f3-24eb6feef718", "metadata": {}, "outputs": [], "source": [ "learn = vision_learner(dls, resnet34, metrics=error_rate)\n", "learn.fine_tune(3)" ] }, { "cell_type": "markdown", "id": "1f5afdb5-998b-4b12-b56f-0c89077ae5c2", "metadata": {}, "source": [ "## Export Trained Model" ] }, { "cell_type": "code", "execution_count": null, "id": "22ff7ee5-efc0-4c71-88e6-c1a43b94edba", "metadata": {}, "outputs": [], "source": [ "learn.export('/home/zeus/projects/Deadpool-Detector/deadpool-detection-model.pkl')" ] }, { "cell_type": "markdown", "id": "792d8a70", "metadata": {}, "source": [ "## Share Learner with the world!" ] }, { "cell_type": "code", "execution_count": 4, "id": "27376bb5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "id": "14796a3e", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "491daa3086dd4a109f036d62bdecf5fa", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Upload 1 LFS files: 0%| | 0/1 [00:00 2\u001b[0m labels \u001b[38;5;241m=\u001b[39m \u001b[43mlearn\u001b[49m\u001b[38;5;241m.\u001b[39mdls\u001b[38;5;241m.\u001b[39mvocab\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpredict\u001b[39m(img):\n\u001b[1;32m 4\u001b[0m yes_reaction \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreactions/yes.jfif\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mNameError\u001b[0m: name 'learn' is not defined" ] } ], "source": [ "#|export\n", "labels = learn.dls.vocab\n", "def predict(img):\n", " yes_reaction = \"reactions/yes.jfif\"\n", " no_reaction = \"reactions/no.jfif\"\n", " what_reaction = \"reactions/what.jpg\"\n", " \n", " img = PILImage.create(img)\n", " pred,pred_idx,probs = learn.predict(img)\n", " index = pred_idx.item()\n", " probability = probs[index].item()\n", " isDeadpool = pred == \"Deadpool\"\n", " \n", " if isDeadpool:\n", " x = \"is\"\n", " else:\n", " x = \"is not\"\n", " \n", " #print(\"The probability is: [\" + str(probability) + \"] that this \" + x + \" Deadpool\")\n", " \n", " if probability < .75:\n", " return PILImage.create(what_reaction)\n", " elif isDeadpool:\n", " return PILImage.create(yes_reaction)\n", " else:\n", " return PILImage.create(no_reaction)" ] }, { "cell_type": "code", "execution_count": 9, "id": "3cec4455-8159-457b-9563-7e41aa202dc1", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://ed025bd9d3a5e5798f.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "The probability is: [0.9999980926513672] that this is Deadpool\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "The probability is: [0.9999998807907104] that this is not Deadpool\n" ] } ], "source": [ "#|export\n", "import gradio as gr\n", "\n", "demo = gr.Interface(\n", " title=\"Deadpool™️ Detector\",\n", " fn=predict,\n", " inputs=gr.Image(shape=(200,200)),\n", " outputs=gr.Image(shape=(200,200)), \n", " examples=[\"examples/deadpool_example1.jpg\", \"examples/deadpool_example2.jpg\", \"examples/deadpool_example3.jpg\", \"examples/spiderman.jpg\",]\n", ").launch(share=true)" ] }, { "cell_type": "code", "execution_count": null, "id": "0c703c3c-f9a9-4c44-a1d5-44dd1d6189ae", "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.16" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "471e25d4adc54a7b971d0064b5c31d4e": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_62adce359c2841ed809d1671b8f7aa78", "style": "IPY_MODEL_f3db0255d2734feb8dcaf68ca698f6d8", "value": " 4/4 [00:00<00:00, 251.70it/s]" } }, "4eee542749dd4a14bc00539ed22854aa": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "503423ce35254544bc26df135dc3b2b7": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "560af530483a40f5b6965fd40a735554": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "5cde88a9faea419bb1148823382105cc": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "ProgressStyleModel", "state": { "description_width": "" } }, "62adce359c2841ed809d1671b8f7aa78": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "70dfbcb89d1142b5bf5113ac1ed7adae": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_503423ce35254544bc26df135dc3b2b7", "style": "IPY_MODEL_560af530483a40f5b6965fd40a735554", "value": "Fetching 4 files: 100%" } }, "7d156bf36fe9401e94bdc607db87cbee": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HBoxModel", "state": { "children": [ "IPY_MODEL_70dfbcb89d1142b5bf5113ac1ed7adae", "IPY_MODEL_9bc68c569c764547a4f80a8fa97c1f17", "IPY_MODEL_471e25d4adc54a7b971d0064b5c31d4e" ], "layout": "IPY_MODEL_4eee542749dd4a14bc00539ed22854aa" } }, "95f5191753fc43eaba2883aafcb9df5d": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "9bc68c569c764547a4f80a8fa97c1f17": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "FloatProgressModel", "state": { "bar_style": "success", "layout": "IPY_MODEL_95f5191753fc43eaba2883aafcb9df5d", "max": 4, "style": "IPY_MODEL_5cde88a9faea419bb1148823382105cc", "value": 4 } }, "f3db0255d2734feb8dcaf68ca698f6d8": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }