Spaces:
Runtime error
Runtime error
File size: 11,833 Bytes
40849b7 c4368ce 3e1f6f1 c4368ce 40849b7 c4368ce 40849b7 c4368ce 40849b7 c4368ce f4f2030 c4368ce f4f2030 c4368ce f831b11 c4368ce 40849b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
{
"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
}
|