0

I am reading some EVI values from 46 raster layers (250 m resolution) 231 rows, 481 columns each and applying Support Vector MEachine (SVM) to classify using the training data stored in excel file and finally writing back the predicted file into one raster.

The code is okay, but it's taking me around 100 hours to run in my Intel Core i7 (7500 U) 2.9 Ghz, 8 GB RAM machine

Can someone help me rewrite this code to optimize the performance? The inner loop (j loop) in the following code is taking more than 80% of the total time.

library(xlsx)
library(e1071)
library(caTools)
library(gdata)
library(rgdal)
library(raster)
Sys.setenv(JAVA_HOME='C:/Program Files/Java/jre1.8.0_131')
library(rJava)
setwd("D:/PhD Dissertation/R/SVM/June15")

Read the tiff file with 46 bands

phenology<-stack("EVI_2005_All.tif")
names(phenology)
dimensions<-dim(phenology)
rowNumber<-as.integer(dimensions[1])
colNumber<- as.integer(dimensions[2])

Define new raster and its projection, extent and resolution

predict_raster<- raster(nrow=rowNumber, ncol=colNumber)
res(predict_raster)=250
predict_raster<- raster(nrow=231, ncol=481, xmn=163244, xmx=283494, ymn =3028497  , ymx = 3086247)
projection(predict_raster) <- "+proj=utm +zone=45 +datum=WGS84 +units=m"
print(predict_raster)
all <- read.xlsx("Training_data_2005.xlsx", sheetName = "EVI_2005_All")

Create a sample index of the total data set and divide it to train and test set

sample_index <- sample(466,350)
svm_train <- all[sample_index,]

When the class column is numeric in such cases:

Convert the class column to factor so that the SVM performs classification rather than regression

Then convert the training set into data frame

svm_train[,"Species"] <- factor(svm_train[,ncol(svm_train)])
svm_train <- as.data.frame(svm_train)
svm_test <- all[-sample_index,]

---------- Now Create an SVM Model with the training dataset--------------------

svm_model <- svm(Species ~ ., data = svm_train)
summary (svm_model)

p=0


for(i in 1:rowNumber)
{
  for(j in 1:colNumber)
  {
    #read the value for each 46 bands for particular coordinage i and j
    v <- (phenology[i,j])*0.0001

    #Now the data is ready to go for the model
    p <- as.integer(predict(svm_model,v))
    predict_raster[i,j]<-p

  }
  Sys.time()
  print("Working in Row #:")
  print(i+1)
}

print(predict_raster) 
print(phenology)
writeRaster(predict_raster,"Predict_SVM_EVI_All_2005.tif","GTiff", overwrite=TRUE)
print("End time of  writing the output image:")
Sys.time()
1

100 hours is a lot to deal with but some headway could be made making use of parallel processing in your loops.

Check out the foreach library. It's pretty quick to set up.

Beyone that it's hard to help without being familiar with svmand its associated predict method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.