-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorial.Rmd
248 lines (195 loc) · 9.06 KB
/
Tutorial.Rmd
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
---
title: "Parallel raster processing in *stars*"
author: "Krzysztof Dyba"
output:
html_document:
toc: yes
toc_float: true
css: style.css
---
Prediction on large datasets can be time-consuming, but with enough computing
power, this task can be parallelized easily. Some algorithms provide native
multithreading like `predict()` function in the **ranger** package (but this is
not standard). In this situation, we have to parallelize the calculations
ourselves. There are several approaches to do this, but for this example we will
divide a large out of memory raster into smaller blocks and make predictions
using multiprocessing.
## Data acquisition
We will use the Landsat 8 satellite scene with four spectral bands as an example
for analysis. It covers a range of 37,000 km$^2$ and consists of over 62 million
pixels. The data is available on [Zenodo](https://zenodo.org/record/8260938).
We can use the `download.file()` function to download the data and then unpack the
*.zip* archive using the `unzip()` function.
```{r message=FALSE}
options(timeout = 600) # increase connection timeout
url = "https://zenodo.org/record/8260938/files/Landsat.zip"
download.file(url, "Landsat.zip")
unzip("Landsat.zip")
```
## Data loading
```{r message=FALSE}
library("stars")
set.seed(1) # define a random seed
```
The first step is to list the rasters representing the blue (B2), green (B3),
red (B4), and near infrared (B5) bands to be loaded using the `list.files()`
function. We need to define two additional arguments, i.e. `pattern = "\\.TIF$"`
and `full.names = TRUE`.
```{r}
files = list.files("Landsat", pattern = "\\.TIF$", full.names = TRUE)
files
```
Since this data does not fit in memory, we have to load it as a proxy (actually,
we will only load the metadata describing these rasters). To do this, we use the
`read_stars()` function with the arguments `proxy = TRUE` and `along = 3`. This
second argument will cause the bands (layers) to be combined into a
three-dimensional array: $[longitude, \ latitude, \ spectral \ band]$.
```{r}
rasters = read_stars(files, proxy = TRUE, along = 3)
```
We can replace the default filenames with band names, and rename the third
dimension as follows:
```{r}
bands = c("Blue", "Green", "Red", "NIR")
# rename bands and dimension
rasters = st_set_dimensions(rasters, 3, values = bands, names = "band")
names(rasters) = "Landsat" # rename the object
```
Using the default `plot()` function, we can display our four bands separately.
By specifying the `rgb` argument, we can create a three-band composition,
such as RGB or CIR (including near infrared). It is also useful to use the
`downsample` argument to display the image at a lower resolution.
```{r}
plot(rasters, rgb = c(3, 2, 1), downsample = 10, main = "RGB composition")
```
## Sampling
For modeling, we need to use a smaller sample rather than the entire dataset.
Sampling training points consists of several steps, which are shown below:
```{r}
bbox = st_as_sfc(st_bbox(rasters)) # define a bounding box
smp = st_sample(bbox, size = 20000) # sample points from the extent of the polygon
smp = st_extract(rasters, smp) # extract the pixel values for those points
smp = st_drop_geometry(st_as_sf(smp)) # convert to a data frame (remove geometry)
smp = na.omit(smp) # remove missing values (NA)
head(smp)
```
## Modelling
The aim of our analysis is to perform unsupervised classification (clustering)
of raster pixels based on spectral bands. In other words, we want to group similar
cells together to make homogeneous clusters. A popular method is the Gaussian
mixture models available in the **mclust** package. Clustering can be done using
the `Mclust()` function and requires the target number of clusters to be defined
in advance (e.g. `G = 6`).
```{r message=FALSE}
library("mclust")
mdl = Mclust(smp, G = 6) # train the model
```
## Prediction
As stated earlier, the entire raster is too large to be loaded into memory.
We need to split it into smaller blocks. For this, we can use the `st_tile()`
function, which requires the total number of rows and columns, and the number
of rows and columns of a small block (for example, it could be 2048 x 2048,
but usually we should find the optimal size).
```{r}
tiles = st_tile(nrow(rasters), ncol(rasters), 2048, 2048)
head(tiles)
```
Finally, the input raster will be divided into `r nrow(tiles)` smaller blocks.
In the following sections, we will compare the processing performance of one
versus multiple processes.
Now let's discuss what steps we need to take to make a prediction:
1. We have `r nrow(tiles)` blocks, so we need to make predictions in a loop.
2. We have to load the rasters, but this time into memory (`proxy = FALSE`) and
only the block (`RasterIO = tiles[iterator, ]`).
3. We use the `predict()` function for clustering. Note we need to define the
`drop_dimensions = TRUE` argument to remove the coordinates from the data frame.
4. Finally, we have to save the clustering results to disk (it can be a temporary
directory). Be sure to specify the missing values (`NA_value = 0`) and the block
size as the input (`chunk_size = dim(tile)`).
**Note!** The `read_stars()` function opens the connection to the file and closes
it each time, which causes overhead. With a large number of blocks, this can
significantly affect performance. In this case, it is better to use a low-level
API, e.g. **[gdalraster](https://github.com/USDAForestService/gdalraster)**.
The second difference between these packages is that **gdalraster** allows
loading data stored as integers, while **stars** loads it as real (floating point)
by default, so we can practically save half of the memory.
### Single process
```{r}
start_time = Sys.time()
for (i in seq_len(nrow(tiles))) {
tile = read_stars(files, proxy = FALSE, RasterIO = tiles[i, ])
names(tile) = bands # rename bands
pr = predict(tile, mdl, drop_dimensions = TRUE)
pr = pr[1] # select only clusters from output
save_path = file.path(tempdir(), paste0("tile_", i, ".tif"))
write_stars(pr, save_path, NA_value = 0, options = "COMPRESS=NONE",
type = "Byte", chunk_size = dim(tile))
}
end_time_1 = difftime(Sys.time(), start_time, units = "secs")
end_time_1 = round(end_time_1)
```
The prediction took `r as.integer(end_time_1)` seconds.
### Multiple processes
Multi-process prediction is a bit more complicated. This requires the use of
two additional packages, i.e. **foreach** and **doParallel**. Now we need to
setup a parallel backend by defining the number of available cores (or by
detecting them automatically using `detectCores()`) in `makeCluster()`, and then
registering the cluster using the `registerDoParallel()` function. This can be
accomplished, for instance, with:
```{r message=FALSE}
library("foreach")
library("doParallel")
cores = 3 # specify number of cores
cl = makeCluster(cores)
registerDoParallel(cl)
```
Once we have the computing cluster prepared, we can write a loop that will be
executed in parallel. Instead of the standard `for()` loop, we will use `foreach()`
with the `%dopar%` operator. In `foreach()` we need to define an iterator and
packages that will be exported to each worker. Then we use the `%dopar%` operator
and write the core of the function (is identical to the example using a single
process).
```{r}
packages = c("stars", "mclust")
start_time = Sys.time()
tifs = foreach(i = seq_len(nrow(tiles)), .packages = packages) %dopar% {
tile = read_stars(files, proxy = FALSE, RasterIO = tiles[i, ], NA_value = 0)
names(tile) = bands
pr = predict(tile, mdl, drop_dimensions = TRUE)
pr = pr[1]
save_path = file.path(tempdir(), paste0("tile_", i, ".tif"))
write_stars(pr, save_path, NA_value = 0, options = "COMPRESS=NONE",
type = "Byte", chunk_size = dim(tile))
return(save_path)
}
end_time_2 = difftime(Sys.time(), start_time, units = "secs")
end_time_2 = round(end_time_2)
```
The prediction took `r as.integer(end_time_2)` seconds. By using three processes
instead of one, we have cut the operation time by half!
## Post-processing
We have our raster blocks saved in a temporary directory. The final step is to
make a mosaic (that is, to combine the blocks into a single raster). I recommend
using GDAL tools, as they provide the best performance when there are a large
number of blocks. We can use:
1. `buildvrt` to create a virtual mosaic.
2. `translate` to save as a geotiff.
We can call these tools using the `gdal_utils()` function from the **sf** package.
```{r}
vrt = tempfile(fileext = ".vrt")
gdal_utils(util = "buildvrt", unlist(tifs), destination = vrt)
gdal_utils(util = "translate", vrt, destination = "predict.tif")
```
Once the parallel computation is complete, we can close the computing cluster
using `stopCluster()` function (this will delete the temporary files).
```{r}
stopCluster(cl)
```
So let's see what our final map looks like.
```{r message=FALSE}
clustering = read_stars("predict.tif")
colors = c("#29a329", "#cbcbcb", "#ffffff", "#086209", "#fdd327", "#064d06")
names = c("Low vegetation", "Bare soil", "Cloud", "Forest 1", "Cropland", "Forest 2")
clustering[[1]] = factor(clustering[[1]], labels = names)
plot(clustering, main = NULL, col = colors, key.width = lcm(4))
```