{ "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": "code", "execution_count": null, "id": "61850308-6410-412b-a0a3-2a9c8c5ac982", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": null, "id": "3e665c52-32ba-40a7-b100-480e60d128f6", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": null, "id": "6697efd8-df96-4db1-834a-9136c6d71ebb", "metadata": {}, "outputs": [], "source": [ "# copy test data to project if not there already\n", "data_path = Path(\"/home/zeus/data/SH2022/\")\n", "data_path.ls()" ] }, { "cell_type": "markdown", "id": "04538e6b-bc2e-4ef6-b5b7-3134185ba01c", "metadata": {}, "source": [ "Load the data" ] }, { "cell_type": "code", "execution_count": null, "id": "7e40d0c6-7118-4b31-8304-f0fdb1f3c490", "metadata": {}, "outputs": [], "source": [ "train_files = get_image_files(data_path/\"train\")\n", "len(train_files)" ] }, { "cell_type": "code", "execution_count": null, "id": "f3e72099-446d-4e7b-8a6d-a6111f6c8556", "metadata": {}, "outputs": [], "source": [ "test_files = get_image_files(data_path/\"test/Deadpool\")\n", "len(test_files)" ] }, { "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": [ "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": "61b3019d-cd37-49b4-bced-503499796d6c", "metadata": {}, "source": [ "# Create App" ] }, { "cell_type": "code", "execution_count": 1, "id": "f678e4ce-6b99-45b2-ae12-3e3a6978fe0a", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "cebedb5a-32ed-483d-90ce-9ccf101bda42", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('/home/zeus/projects/Deadpool-Detector/deadpool-detection-model.pkl')" ] }, { "cell_type": "code", "execution_count": 3, "id": "cc0fa8a2-51fe-4591-9056-e84e0fec9af7", "metadata": {}, "outputs": [], "source": [ "#|export\n", "labels = learn.dls.vocab\n", "def predict(img):\n", " img = PILImage.create(img)\n", " pred,pred_idx,probs = learn.predict(img)\n", " return {labels[i]: float(probs[i]) for i in range(len(labels))}" ] }, { "cell_type": "code", "execution_count": 7, "id": "3cec4455-8159-457b-9563-7e41aa202dc1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7863\n", "Running on public URL: https://40a72959264a859fdc.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" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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.Label(num_top_classes=-3),\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": "018d3761-beac-4999-b915-32e61297db7c", "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 }