{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2023-06-06T16:12:56.482001Z", "iopub.status.busy": "2023-06-06T16:12:56.481629Z", "iopub.status.idle": "2023-06-06T16:12:56.511919Z", "shell.execute_reply": "2023-06-06T16:12:56.510864Z", "shell.execute_reply.started": "2023-06-06T16:12:56.481922Z" }, "scrolled": true }, "outputs": [], "source": [ "# This Python 3 environment comes with many helpful analytics libraries installed\n", "# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n", "# For example, here's several helpful packages to load\n", "\n", "#import numpy as np # linear algebra\n", "#import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "\n", "# Input data files are available in the read-only \"../input/\" directory\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n", "\n", "#import os\n", "#for dirname, _, filenames in os.walk('/kaggle/input'):\n", "# for filename in filenames:\n", "# print(os.path.join(dirname, filename))\n", "\n", "# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n", "# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "import pathlib\n", "temp = pathlib.PosixPath\n", "pathlib.PosixPath = pathlib.WindowsPath" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:12:56.515330Z", "iopub.status.busy": "2023-06-06T16:12:56.514653Z", "iopub.status.idle": "2023-06-06T16:13:17.032978Z", "shell.execute_reply": "2023-06-06T16:13:17.031623Z", "shell.execute_reply.started": "2023-06-06T16:12:56.515291Z" }, "scrolled": true }, "outputs": [], "source": [ "#|export\n", "\n", "import fastbook\n", "import pandas as pd\n", "import numpy as np\n", "import os \n", "\n", "from fastbook import *\n", "from fastai.vision.widgets import *\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im = PILImage.create('plume1.png')\n", "im.thumbnail((182,192))\n", "im" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:17.047225Z", "iopub.status.busy": "2023-06-06T16:13:17.046261Z", "iopub.status.idle": "2023-06-06T16:13:17.053274Z", "shell.execute_reply": "2023-06-06T16:13:17.052116Z", "shell.execute_reply.started": "2023-06-06T16:13:17.047171Z" } }, "outputs": [], "source": [ "path = Path('/kaggle/input/speuntapped/train')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:17.058708Z", "iopub.status.busy": "2023-06-06T16:13:17.057847Z", "iopub.status.idle": "2023-06-06T16:13:20.265898Z", "shell.execute_reply": "2023-06-06T16:13:20.264895Z", "shell.execute_reply.started": "2023-06-06T16:13:17.058670Z" } }, "outputs": [], "source": [ "fns = get_image_files(path)\n", "fns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:20.269200Z", "iopub.status.busy": "2023-06-06T16:13:20.267211Z", "iopub.status.idle": "2023-06-06T16:13:20.276861Z", "shell.execute_reply": "2023-06-06T16:13:20.275708Z", "shell.execute_reply.started": "2023-06-06T16:13:20.269168Z" } }, "outputs": [], "source": [ "plume = DataBlock(\n", " blocks=(ImageBlock, CategoryBlock),\n", " get_items=get_image_files,\n", " splitter=RandomSplitter(valid_pct=0.2,seed=42),\n", " get_y=parent_label\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:20.279400Z", "iopub.status.busy": "2023-06-06T16:13:20.279013Z", "iopub.status.idle": "2023-06-06T16:13:20.294720Z", "shell.execute_reply": "2023-06-06T16:13:20.293498Z", "shell.execute_reply.started": "2023-06-06T16:13:20.279361Z" } }, "outputs": [], "source": [ "plume = plume.new(\n", " item_tfms=RandomResizedCrop(152, min_scale=0.5),\n", " batch_tfms=aug_transforms()\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:20.296793Z", "iopub.status.busy": "2023-06-06T16:13:20.296142Z", "iopub.status.idle": "2023-06-06T16:13:29.647446Z", "shell.execute_reply": "2023-06-06T16:13:29.646201Z", "shell.execute_reply.started": "2023-06-06T16:13:20.296747Z" } }, "outputs": [], "source": [ "dls = plume.dataloaders(path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:29.649289Z", "iopub.status.busy": "2023-06-06T16:13:29.648894Z", "iopub.status.idle": "2023-06-06T16:13:30.941482Z", "shell.execute_reply": "2023-06-06T16:13:30.940546Z", "shell.execute_reply.started": "2023-06-06T16:13:29.649249Z" } }, "outputs": [], "source": [ "dls.valid.show_batch(max_n=12, nrows=4) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:13:30.942995Z", "iopub.status.busy": "2023-06-06T16:13:30.942633Z", "iopub.status.idle": "2023-06-06T16:16:55.896473Z", "shell.execute_reply": "2023-06-06T16:16:55.895239Z", "shell.execute_reply.started": "2023-06-06T16:13:30.942955Z" } }, "outputs": [], "source": [ "learn = vision_learner(dls, resnet50, metrics=error_rate)\n", "learn.fine_tune(epochs=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:16:55.899023Z", "iopub.status.busy": "2023-06-06T16:16:55.898611Z", "iopub.status.idle": "2023-06-06T16:17:12.173908Z", "shell.execute_reply": "2023-06-06T16:17:12.172400Z", "shell.execute_reply.started": "2023-06-06T16:16:55.898975Z" } }, "outputs": [], "source": [ "from IPython.core.pylabtools import figsize \n", "interp = ClassificationInterpretation.from_learner(learn)\n", "interp.plot_confusion_matrix(figsize=(10,10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:17:12.177922Z", "iopub.status.busy": "2023-06-06T16:17:12.177508Z", "iopub.status.idle": "2023-06-06T16:17:13.734058Z", "shell.execute_reply": "2023-06-06T16:17:13.732857Z", "shell.execute_reply.started": "2023-06-06T16:17:12.177864Z" } }, "outputs": [], "source": [ "interp.plot_top_losses(20, nrows=10, figsize=(20,20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:17:13.739200Z", "iopub.status.busy": "2023-06-06T16:17:13.735848Z", "iopub.status.idle": "2023-06-06T16:17:13.744700Z", "shell.execute_reply": "2023-06-06T16:17:13.743507Z", "shell.execute_reply.started": "2023-06-06T16:17:13.739151Z" } }, "outputs": [], "source": [ "test_image = '/kaggle/input/speuntapped/test/test/ang20190922t192642-4_r4928_c373-plume.png'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:17:13.750250Z", "iopub.status.busy": "2023-06-06T16:17:13.749605Z", "iopub.status.idle": "2023-06-06T16:17:13.883090Z", "shell.execute_reply": "2023-06-06T16:17:13.881894Z", "shell.execute_reply.started": "2023-06-06T16:17:13.750206Z" } }, "outputs": [], "source": [ "is_plume,_,probs = learn.predict(PILImage.create(test_image))\n", "print(f\"This is a: {is_plume}.\")\n", "print(f\"Probability it's a plume: {probs[1]:.4f}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-06-06T16:22:39.764768Z", "iopub.status.busy": "2023-06-06T16:22:39.764285Z", "iopub.status.idle": "2023-06-06T16:22:40.137875Z", "shell.execute_reply": "2023-06-06T16:22:40.136697Z", "shell.execute_reply.started": "2023-06-06T16:22:39.764726Z" } }, "outputs": [], "source": [ "learn.export('methane plume detect.pkl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "learn = load_learner('methane plume detect.pkl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.predict(im)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "categories = ('No Plume', 'Plume')\n", "\n", "def classify_image(img):\n", " pred, idx, probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "classify_image(im)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "image = gr.inputs.Image(shape = (152, 152))\n", "label = gr.outputs.Label()\n", "examples = ['plume1.png', 'plume2.png', 'noplume1.png', 'noplume2.png']\n", "\n", "intf = gr.Interface(fn = classify_image, inputs = image, outputs = label, examples = examples)\n", "intf.launch(inline = False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nbdev\n", "nbdev.export.nb_export('methane-plume-classification-yes-or-no.ipynb')\n", "print('Export successful')" ] }, { "cell_type": "code", "execution_count": null, "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.10.9" } }, "nbformat": 4, "nbformat_minor": 4 }