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='/'))
# \donttest{
# 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='/'))
# }
# \donttest{
# 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='/'))
# }
Run the code above in your browser using DataLab