Valentin Pesendorfer
2016-Mar-14 08:43 UTC
[R] RSNNS neural network for image classification in R
Hello everybody,
I'm trying to harness the power of neural networks for image classification
of big rasters using the `RSNNS` package in `R`.
As for the data preparation and training of the model, everything works
perfectly fine and the accuracies look quite promising.
Subsequently, I'm trying to classify the raster values using the function
`predict` with the trained model. Having a quite big amount of data (rasterstack
with the dimension 10980x10980x16), I'm processing the data block by block.
And here's the problem:
The prediction of the class values is extremely slow. I'm working on a quite
powerful machine (Windows x64, 32GB Ram, i7 3.4GHZ quad-core) but still the
process is almost literally taking ages. I already reduced the size of my
blocks, but still the amount of time needed is unacceptable. Currently I split
the data in blocks of 64 rows per block. That would result in a total of 172
blocks. If I assume a linear processing time for each block (in my case 33
minutes !!!), it would take me almost 95 hours to process the whole image.
Again, that can not be right.
I've tried other neural network packages and for instance `nnet` classifies
bigger blocks like these in under one minute.
So please, if you have any pointers on what I'm doing wrong, I'd greatly
appreciate it.
Best regards,
Valentin
Here's a working example similar to my code:
library(RSNNS)
#example data for training and testing
dat <- matrix(runif(702720),ncol = 16)
#example data to classify
rasval <- matrix(runif(11243520),ncol = 16)
dat <- as.data.frame(dat)
#example class labels from 1 to 11
classes <- matrix(,ncol=1,nrow=nrow(dat))
classes <- apply(classes,1,function(x) floor(runif(1,0,11)))
dat$classes <- classes
#shuffle dataset
dat <- dat[sample(nrow(dat)),]
datValues <- dat[,1:16]
datTargets <- decodeClassLabels(dat[,17])
#split dataset
dat <- splitForTrainingAndTest(datValues, datTargets, ratio=0.15)
#normalize data
dat <- normTrainingAndTestSet(dat)
#extract normalization variables
ncolmeans <- attributes(dat$inputsTrain)$normParams$colMeans
ncolsds <- attributes(dat$inputsTrain)$normParams$colSds
#train model
model <- mlp(dat$inputsTrain, dat$targetsTrain, size=1,
learnFunc="SCG", learnFuncParams=c(0, 0, 0, 0),
maxit=400, inputsTest=dat$inputsTest,
targetsTest=dat$targetsTest)
#normalize raster data
rasval <- sweep(sweep(rasval,2,ncolmeans),2,ncolsds,'/')
#Predict classes ##Problem##
pred <- predict(model,rasval)