Debian Codegraph @

notebooks/manual-labeling-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "76a9a18b",
6 "metadata": {},
7 "source": [
8 "# Manual labeling for supervised classification"
9 ]
10 },
11 {
12 "cell_type": "code",
13 "execution_count": null,
14 "id": "f75d09d7",
15 "metadata": {},
16 "outputs": [],
17 "source": [
18 "%load_ext autoreload\n",
19 "%autoreload 2\n",
20 "\n",
21 "import os\n",
22 "\n",
23 "import numpy as np\n",
24 "import xarray as xr\n",
25 "import rioxarray as riox\n",
26 "\n",
27 "from pathlib import Path\n",
28 "from polsarpro.io import open_netcdf_beam\n",
29 "from polsarpro.util import pauli_rgb\n",
30 "\n",
31 "# optional import for progress bar\n",
32 "from dask.diagnostics import ProgressBar\n",
33 "\n",
34 "\n",
35 "# change to your data paths\n",
36 "# original dataset\n",
37 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
38 "\n",
39 "# output files \n",
40 "output_test_dir = Path(\"/data/psp/\")"
41 ]
42 },
43 {
44 "cell_type": "markdown",
45 "id": "8f82ace0",
46 "metadata": {},
47 "source": [
48 "## Open the image and make a Pauli RGB representation"
49 ]
50 },
51 {
52 "cell_type": "code",
53 "execution_count": null,
54 "id": "e7c0adce",
55 "metadata": {},
56 "outputs": [],
57 "source": [
58 "S = open_netcdf_beam(input_alos_data)\n",
59 "rgb = pauli_rgb(S)"
60 ]
61 },
62 {
63 "cell_type": "markdown",
64 "id": "f8a93514",
65 "metadata": {},
66 "source": [
67 "## Convert to UINT8 and save as a Tiff file"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": null,
73 "id": "d01b44de",
74 "metadata": {},
75 "outputs": [],
76 "source": [
77 "(255*rgb).astype(\"uint8\").rio.to_raster(output_test_dir / \"SF_Pauli.tif\")"
78 ]
79 },
80 {
81 "cell_type": "markdown",
82 "id": "747ed2ff",
83 "metadata": {},
84 "source": [
85 "## Manual labeling\n",
86 "Recommended approach:\n",
87 "- Open the tif image in GIMP\n",
88 "- Add one layer per label, use transparent background\n",
89 "- Draw labels (any non-zero value) on each layer, avoid overlap bewteen different classes\n",
90 "- Export each layer separately as a png file"
91 ]
92 },
93 {
94 "cell_type": "markdown",
95 "id": "aefec81e",
96 "metadata": {},
97 "source": [
98 "## Import hand-drawn labels\n",
99 "\n",
100 "- In the following we assume that labels are png files\n",
101 "- We map the name of each layer to a class number\n",
102 "- We produce an `xarray.DataArray` that may be read by the classifier"
103 ]
104 },
105 {
106 "cell_type": "code",
107 "execution_count": null,
108 "id": "1bea3a64",
109 "metadata": {},
110 "outputs": [],
111 "source": [
112 "from matplotlib.image import imread\n",
113 "labels = {\"sea\": 1, \"vegetation\":2, \"urban\":3}\n",
114 "\n",
115 "# create a class map with integer type from the input data size\n",
116 "class_map = np.zeros_like(S.hh.data.real, dtype=int)\n",
117 "for name in labels:\n",
118 "\n",
119 " # load class image\n",
120 " im_pth = Path(\"/data/psp/labels\") / f\"{name}.png\"\n",
121 " # if color image we need only one channel\n",
122 " im = imread(im_pth)[..., 0]\n",
123 " # add with corresponding label\n",
124 " class_map += labels[name] * (im > 0)\n",
125 "\n",
126 "# make a data array that can be read by the classifier\n",
127 "da_class_map = xr.DataArray(class_map, dims=S.dims, coords=S.coords) \n",
128 "\n",
129 "# Optional: save array to a file using `to_netcdf`"
130 ]
131 },
132 {
133 "cell_type": "markdown",
134 "id": "277f82fd",
135 "metadata": {},
136 "source": [
137 "## Apply Wishart supervised classifier"
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": null,
143 "id": "2580db8a",
144 "metadata": {},
145 "outputs": [],
146 "source": [
147 "from dask.diagnostics import ProgressBar\n",
148 "from polsarpro.classification import wishart_supervised\n",
149 "with ProgressBar():\n",
150 " cm = wishart_supervised(S, da_class_map).compute()"
151 ]
152 },
153 {
154 "cell_type": "markdown",
155 "id": "d7cf03b3",
156 "metadata": {},
157 "source": [
158 "## Visualize the results"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": null,
164 "id": "8d2527f0",
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "import matplotlib.pyplot as plt\n",
169 "from matplotlib.colors import ListedColormap\n",
170 "\n",
171 "# Blue: sea, green: vegetation, orange: built-up\n",
172 "colors = [\"#387EB9\", \"#4EAF49\", \"#FF7F00\"]\n",
173 "colors2 = [\"#000000\", \"#387EB9\", \"#4EAF49\", \"#FF7F00\"]\n",
174 "# Create the discrete colormap\n",
175 "my_cmap = ListedColormap(colors)\n",
176 "my_cmap2 = ListedColormap(colors2)\n",
177 "\n",
178 "plt.figure(figsize=(10, 10))\n",
179 "plt.subplot(1,3,1)\n",
180 "plt.imshow(cm.wishart_supervised_class[::8], interpolation=\"none\", cmap=my_cmap)\n",
181 "plt.axis(\"off\")\n",
182 "plt.title(\"Output labels\")\n",
183 "plt.subplot(1,3,2)\n",
184 "plt.imshow(class_map[::8], interpolation=\"none\", cmap=my_cmap2)\n",
185 "plt.axis(\"off\")\n",
186 "plt.title(\"Manual inputs\")\n",
187 "plt.subplot(1,3,3)\n",
188 "plt.imshow(rgb.transpose(\"y\",\"x\",\"band\")[::8], interpolation=\"bilinear\")\n",
189 "plt.axis(\"off\")\n",
190 "plt.title(\"Pauli RGB\")\n",
191 "plt.tight_layout()"
192 ]
193 }
194 ],
195 "metadata": {
196 "kernelspec": {
197 "display_name": "psp",
198 "language": "python",
199 "name": "python3"
200 },
201 "language_info": {
202 "codemirror_mode": {
203 "name": "ipython",
204 "version": 3
205 },
206 "file_extension": ".py",
207 "mimetype": "text/x-python",
208 "name": "python",
209 "nbconvert_exporter": "python",
210 "pygments_lexer": "ipython3",
211 "version": "3.12.13"
212 }
213 },
214 "nbformat": 4,
215 "nbformat_minor": 5
216 }