-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
97 lines (73 loc) · 2.74 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rasterclassifier
<!-- badges: start -->
<!-- badges: end -->
Simplify machine learning workflows for raster imagery.
## Installation
You can install the development version of rasterclassifier from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("tomwilsonsco/rasterclassifier")
```
## Example
```{r example, message=FALSE, warning=FALSE, results='hide'}
library(rasterclassifier)
# read the example tif included with package as terra spatRaster
example_img <- terra::rast(system.file("test_raster.tif",
package = "rasterclassifier"
))
# read the example training polygon as sf features
example_polys <- sf::st_read(system.file("test_polys.gpkg",
package = "rasterclassifier"
))
# read the example mask layer as sf feature
mask_poly <- sf::st_read(system.file("test_mask.gpkg",
package = "rasterclassifier"
))
# extract pixel values from raster using sf feature collection
pixel_df <- extract_pixels(example_img, example_polys)
# This will run 5 different train test splits,
# then train `randomForest` (default) on training set
# and report different accuracy metrics against the testing set
classif_train_test(pixel_df, classifier = "random_forest")
# A custom maximum likelihood classifier was created in the package
# This is generally less accurate than random forest, but can easily
# compare with the same training data
classif_train_test(pixel_df, classifier = "maximum_likelihood")
# If happy with RF metrics then can proceed to train a randomForest
rf <- pixel_df |>
train_test_split() |>
features_labels_select() |>
fit_random_forest()
# Mask the prediction image to area of interest
example_img <- terra::mask(example_img, mask_poly)
# Use the trained model to predict a whole image
pred_img <- terra::predict(example_img, rf, na.rm = TRUE)
# Convert the classified image to polygons
pred_features <- sf::st_as_sf(terra::as.polygons(pred_img, dissolve = TRUE)) |>
subset(class == 1) |>
sf::st_cast("POLYGON")
# Plot rgb version of raster and prediction polys with red outline
terra::plotRGB(example_img, stretch = "hist", r = 3, g = 2, b = 1)
terra::plot(pred_features, color = "black", add = TRUE)
# if wanted to do the same with maximum likelihood classifier:
# Train the mlc
# (this is just calculating per class band means and covariance matrices)
mlc <- pixel_df |>
train_test_split() |>
features_labels_select() |>
mlc()
# Use the trained mlc model to predict a whole image
pred_img <- terra::predict(example_img, model=mlc, na.rm = TRUE)
```