0

I am trying to classify crops using Sentinel-2 image including 10 classes, using randomForest classification in R. For that, I extracted 2000 random samples points from the training areas for 11 bands in sentinel-2 images and stored them in a dataframe to train and validate the model. It consists of 11 predictors (bands) and one column containing response variable (Class). The structure of the dataframe looks like this:

Classes ‘data.table’ and 'data.frame':  2000 obs. of  12 variables:
$ Clss: Factor w/ 10 levels "Alfalfa","Barren",..: 4 4 2 5 2 4 5 3 4 1 ...
$ B2  : num  0.122 0.1621 0.2427 0.0267 0.3304 ...
$ B3  : num  0.1812 0.233 0.3134 0.0548 0.4002 ...
$ B4  : num  0.2907 0.356 0.3995 0.0203 0.4682 .
$ B5  : num  0.3319 0.3861 0.4245 0.0672 0.4894 ...
$ B6  : num  0.35 0.4 0.429 0.379 0.48 ...
$ B7  : num  0.382 0.429 0.441 0.554 0.491 ...`
$ B8  : num  0.424 0.495 0.462 0.579 0.507 ...
$ B8A : num  0.435 0.489 0.444 0.577 0.485 ..
$ B9  : num  0.447 0.492 0.452 0.573 0.473 ...
$ B11 : num  0.482 0.516 0.544 0.19 0.574 ...
$ B12 : num  0.3169 0.361 0.5553 0.0844 0.5559 ...
- attr(*, ".internal.selfref")=<externalptr> 

Then, I performed 10 fold cross validation and grid search to optimize the model using 'trainControl' function. Following is the code to generate final fitted model.

rfmodel<- train(factor(Clss) ~ . , method ="rf", type="Classification",data=B.train, importance=TRUE,
allowParallel=TRUE,tune.grid=data.frame(mtry= c(2:11)) , ntree=800,
trControl=ctrl,metric="AUC")
           

After fitting the model, I predicted the whole raster image using the 'predict' function in raster package using the code below:

img_predict <- predict(img,rfmodel,type='raw',progress='window')

Here, img is my raster image, that I am trying to predict and rfmodel is my fitted model.

The output of the predicted raster 'img_predict' looks like this:

class      : RasterLayer 
dimensions : 8359, 6583, 55027297  (nrow, ncol, ncell)
resolution : 10, 10  (x, y)
extent     : 602990, 668820, 3608270, 3691860  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=11 +datum=WGS84 +units=m +no_defs 
names      : layer 
values     : 2, 9  (min, max)
attributes :
   ID   value
from:  1 Alfalfa
to : 10   Wheat

Now the problem is that I trained the classifier using 10 different classes, however the classified raster has only two classes. Though the attributes in the predicted raster shows 10 classes, the plot of the raster showed only two classes. This seems quite strange to me. Am I missing something while using 'predict' function?

Some of the classes in my training sample contains relatively larger sample points than other. Although, I am getting quite good accuracy for the classifier results, could that be the problem? What should I add or check to get proper classified image with all 10 class?

I've tried this multiple times but getting the similar results and could not identify the reason. I am quite new to R. Any help would be appreciated. This is the first time I am posting something here, so I apologize for the formatting issues. RGB composite of the image is shown on left and the predicted raster on the right. As seen below, my input raster is not exactly rectangular. However, R is predicting that outside portion as one class and whole study area as one. enter image description here

8
  • I don't see evidence that there's only two classes in your output - it says the min and max are 2 and 9 and the table goes from 1 to 10. What does it plot like? What is table(img_predict[]) ? – Spacedman Jul 27 '20 at 8:56
  • Have you visually inspected the results to see what the two predicted classes are showing? – Aaron Jul 27 '20 at 15:17
  • @Aaron Yes, I have. The plot shows only two classes, minimum and maximum one. – UshaP Jul 27 '20 at 16:38
  • 1
    @Spacedman The table(img_predict[]) shows two minimum and maximum classes (2&9) with the no. of cells associated with it. I forgot to mention before that my raster image is not exactly rectangular. The classified raster however is predicting the portion of image outside the study area as one class and whole study area as one class. I have edited my question and added the ss of RGB composite on left and the plot from 'img_predict' on the right. – UshaP Jul 27 '20 at 17:16
  • Certainly something odd going on here. I do notice that if your factors are names that caret::predict.train returns the names and not the factor levels. Perhaps this is being parsed incorrectly in raster::predict. Have you tried doing this in randomForest? You should know that caret uses the Sobel method for node splitting, which I find somewhat suspect in general application. It is a bad idea to use a grid search to optimize parameters (ie., mtry), as it is very biased. You should also specify what packages you are using as to not leave us guessing. – Jeffrey Evans Jul 27 '20 at 19:19

Your Answer

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

Browse other questions tagged or ask your own question.