Rice Mapping using machine learning in Code Editor

Building a Random Forest model

Step 1: open the code editor by going to https://code.earthengine.google.com/

Step 2: Import Data from Google Earth Engine Asset with the code below

// import the composite
var composites = ee.ImageCollection("projects/servir-mekong/yearlyComposites");
// import the training data
var data = ee.FeatureCollection("projects/servir-mekong/referenceData/riceTraining");
// import the administrative boundaries
var cambodia = ee.FeatureCollection("projects/servir-mekong/referenceData/cam_adm");

Step 3: Filter rice and non_rice data from training dataset for visualization and add the layers to the Map

// Select rice and no rice
var rice = data.filter(ee.Filter.eq('land_class',1))
var non_rice = data.filter(ee.Filter.eq('land_class',0))

//Show Rice and Non_Rice Data Layer 
Map.addLayer(rice,{},"rice");
Map.addLayer(non_rice.draw("red"),{},"no rice");

Step 4:  Sample the training data (only one year) with regional composite to train in the machine learning algorithm.

var y = 2018;
var start = ee.Date.fromYMD(y,1,1);
var end = ee.Date.fromYMD(y,12,31);
 
var image = ee.Image(composites.filterDate(start,end).first()).divide(10000);

var trainingData  = data.filter(ee.Filter.eq("year",y));
print("Training Sample",trainingData.size())
var trainingSample = image.sampleRegions({collection:trainingData,properties:["land_class"],scale:30,geometries:true});

Step 5:  Train the training sample in random forest for primitive and Show the classification results in Map

var bandNames = image.bandNames();
print(bandNames)
var classifier = ee.Classifier.randomForest(25,0).setOutputMode('PROBABILITY').train(trainingSample,"land_class",bandNames);
 
var classification = image.classify(classifier).multiply(100)
 
Map.addLayer(classification.clip(cambodia),{min:20,max:90,palette:"white,gray,black"},"Rice primitive")

find the full code here

2 comments

  1. hello, you did a good job in Random Forest. I want to training datasets more than 1,000,000,0, how can I achieve it? The gee can not run it online. Thank you! If you have any ideas, please share with me.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s