train {deforestable} | R Documentation |
Train models for forest detection
Description
As input data, the function needs two folders- Nonforestdir with images of non-forest and Forestdir with ones of forest. train() uses all images in both folders to train a model. Putting an image into an incorrect folder is equivalent to tagging the image incorrectly.
Usage
train(
n_pts,
model = c("fr_Non-Param", "fr_Param"),
Forestdir,
Nonforestdir,
train_method = c("cv", "train"),
k_folds,
parallel = FALSE
)
Arguments
n_pts |
matters only when train_method='cv'. Defines the size of the square sub-frames into which images would be split during cross-validation. |
model |
which model to train |
Forestdir |
path to the directory with (only) forest images |
Nonforestdir |
path to the directory with (only) non-forest images |
train_method |
how to train the model: simple training, cross-validation. |
k_folds |
matters only when train_method='cv'. The number of folds in the k-fold cross-validation setup. |
parallel |
matters only when train_method='cv'. Boolean. whether or not use a parallel setting during cross-validation |
Details
Currently, both fr_Non-Param and fr_Param use parameter n_pts only in the testing part of cross-validation, not during training. Training is always done on whole original images in the training folders.
Value
object of class ForestTrain potentially with a sub-class. See Class_ForestTrain
.
Examples
library(deforestable)
n_pts <- 20
# Choosing folders with training data
Forestdir <- system.file('extdata/Forest/', package = "deforestable")
Nonforestdir <- system.file('extdata/Non-forest/', package = "deforestable")
k_folds=3;
#### Read the target image ####
tg_dir <- system.file('extdata/', package = "deforestable")
test_image <- read_data_raster('smpl_1.jpeg', dir = tg_dir)
#### Models ####
# Simple training of the non-parametric model
Model_nonP_tr <- train(model='fr_Non-Param', Forestdir=Forestdir, Nonforestdir=Nonforestdir,
train_method='train', parallel=FALSE)
res <- classify(data=test_image, Model=Model_nonP_tr,
n_pts=n_pts, parallel=FALSE, progress = 'text')
tmp_d <- tempdir(); tmp_d
jpeg::writeJPEG(image=res, target=paste(tmp_d,'Model_nonP_tr.jpeg', sep='/'))
# Cross-validation of the non-parametric model
Model_nonP_cv <- train(n_pts=n_pts, model='fr_Non-Param', Forestdir=Forestdir,
Nonforestdir=Nonforestdir, train_method='cv',
k_folds=k_folds, parallel=FALSE)
res <- classify(data=test_image, Model=Model_nonP_cv,
n_pts=n_pts, parallel=FALSE, progress = 'text')
tmp_d <- tempdir(); tmp_d
jpeg::writeJPEG(image=res, target=paste(tmp_d,'Model_nonP_cv.jpeg', sep='/'))
# Cross-validation of the parametric model
Model_P_cv <- train(n_pts=n_pts, model='fr_Param', Forestdir=Forestdir,
Nonforestdir=Nonforestdir, train_method='cv',
k_folds=k_folds, parallel=FALSE)
res <- classify(data=test_image, Model=Model_P_cv,
n_pts=n_pts, parallel=FALSE, progress = 'text')
tmp_d <- tempdir(); tmp_d
jpeg::writeJPEG(image=res, target=paste(tmp_d,'Model_P_cv.jpeg', sep='/'))
# Simple training of the parametric model
Model_P_tr <- train(model='fr_Param', Forestdir=Forestdir, Nonforestdir=Nonforestdir,
train_method='train', parallel=FALSE)
res <- classify(data=test_image, Model=Model_P_tr,
n_pts=n_pts, parallel=FALSE, progress = 'text')
tmp_d <- tempdir(); tmp_d
jpeg::writeJPEG(image=res, target=paste(tmp_d,'Model_P_tr.jpeg', sep='/'))