{ "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')" ] }, { "attachments": {}, "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" ], "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": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }