|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Visualizing Samples\n", |
| 8 | + "\n", |
| 9 | + "This tutorial shows how to visualize and save the extent of your samples before and during training. In this particular example, we compare a vanilla RandomGeoSampler with one bounded by multiple ROI's and show how easy it is to gain insight on the distribution of your samples." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import os\n", |
| 19 | + "import tempfile\n", |
| 20 | + "\n", |
| 21 | + "import matplotlib.pyplot as plt\n", |
| 22 | + "from torch.utils.data import DataLoader\n", |
| 23 | + "\n", |
| 24 | + "from torchgeo.datasets import NAIP, stack_samples\n", |
| 25 | + "from torchgeo.datasets.utils import download_url\n", |
| 26 | + "from torchgeo.samplers import RandomGeoSampler\n", |
| 27 | + "\n", |
| 28 | + "\n", |
| 29 | + "def run_epochs(dataset, sampler):\n", |
| 30 | + " dataloader = DataLoader(\n", |
| 31 | + " dataset, sampler=sampler, batch_size=1, collate_fn=stack_samples, num_workers=0\n", |
| 32 | + " )\n", |
| 33 | + " fig, ax = plt.subplots()\n", |
| 34 | + " num_epochs = 5\n", |
| 35 | + " for epoch in range(num_epochs):\n", |
| 36 | + " color = plt.cm.viridis(epoch / num_epochs)\n", |
| 37 | + " # sampler.chips.to_file(f'naip_chips_epoch_{epoch}') # Optional: save chips to file for display in GIS software\n", |
| 38 | + " ax = sampler.chips.plot(ax=ax, color=color)\n", |
| 39 | + " for sample in dataloader:\n", |
| 40 | + " pass\n", |
| 41 | + " plt.show()" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "Generate dataset" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": null, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "naip_root = os.path.join(tempfile.gettempdir(), 'naip')\n", |
| 58 | + "naip_url = (\n", |
| 59 | + " 'https://naipeuwest.blob.core.windows.net/naip/v002/de/2018/de_060cm_2018/38075/'\n", |
| 60 | + ")\n", |
| 61 | + "tiles = ['m_3807511_ne_18_060_20181104.tif', 'm_3807512_sw_18_060_20180815.tif']\n", |
| 62 | + "for tile in tiles:\n", |
| 63 | + " download_url(naip_url + tile, naip_root)\n", |
| 64 | + "\n", |
| 65 | + "naip = NAIP(naip_root)" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "First we create the default sampler for our dataset (3 samples) and run it for 5 epochs and plot its results. Each color displays a different epoch, so we can see how the RandomGeoSampler has distributed it's samples for every epoch." |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "sampler = RandomGeoSampler(naip, size=1000, length=3)" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "run_epochs(naip, sampler)" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "Now we split our dataset by two bounding boxes and re-inspect the samples." |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": null, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "import numpy as np\n", |
| 107 | + "\n", |
| 108 | + "from torchgeo.datasets import roi_split\n", |
| 109 | + "from torchgeo.datasets.utils import BoundingBox\n", |
| 110 | + "\n", |
| 111 | + "rois = [\n", |
| 112 | + " BoundingBox(440854, 442938, 4299766, 4301731, 0, np.inf),\n", |
| 113 | + " BoundingBox(449070, 451194, 4289463, 4291746, 0, np.inf),\n", |
| 114 | + "]\n", |
| 115 | + "datasets = roi_split(naip, rois)" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "combined = datasets[0] | datasets[1]" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "sampler = RandomGeoSampler(combined, size=1000, length=3)\n", |
| 134 | + "run_epochs(combined, sampler)" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "metadata": { |
| 139 | + "kernelspec": { |
| 140 | + "display_name": "cca", |
| 141 | + "language": "python", |
| 142 | + "name": "python3" |
| 143 | + }, |
| 144 | + "language_info": { |
| 145 | + "codemirror_mode": { |
| 146 | + "name": "ipython", |
| 147 | + "version": 3 |
| 148 | + }, |
| 149 | + "file_extension": ".py", |
| 150 | + "mimetype": "text/x-python", |
| 151 | + "name": "python", |
| 152 | + "nbconvert_exporter": "python", |
| 153 | + "pygments_lexer": "ipython3", |
| 154 | + "version": "3.10.14" |
| 155 | + } |
| 156 | + }, |
| 157 | + "nbformat": 4, |
| 158 | + "nbformat_minor": 2 |
| 159 | +} |
0 commit comments