Google Earth Engine – Troubleshooting LULC-2021 Layer Error: User Memory Limit Exceeded

google-earth-enginegoogle-earth-engine-javascript-apiland-coversentinel-2

I am trying to create a LULC map for Maharashtra State in India. I collected training polygons for 7 classes: Agriculture, Water, Builtup, Fallow Land, Barren Land, Wetlands, Other Vegetation. I am trying to classify them using Random Forest Algorithm.

var gcps = builtup.merge(water).merge(agriculture).merge(fallowland).merge(barrenland).merge(otherVegetation).merge(wetlands)

//The name of the property on the points storing the class lebel
var classProperty = 'landcover';

//Sample the input imagery to get a FeatureCollection of training data
var training = filtered.select(bands).sampleRegions({
  collection: gcps,
  properties: [classProperty],
  scale: 10
});

//Train the classifier
var classifier = ee.Classifier.smileRandomForest(50).train({
  features: training,
  classProperty: classProperty,
});

//Classify the input imagery
var classified = filtered.classify(classifier);

//Define color palette
var palette = [
  '57DA25', // agriculture (0) 
  '25DAD6', //waterbody (1) 
  'DAB625', //builtup (2) 
  '98ff00', // fallow land (3)
  '808b04',// barren land (4)
  'b6fdff', // wetlands (5)
  'ff6218' //other Vegetation (6)
];

Map.addLayer(classified.clip(combined_feature), {min:0, max: 6, palette: palette}, 'LULC-2021');

Here is my complete code

I understand that there are many points that have been collected, due to which the 'User Memory Limit' Error Occurs. So, either I will have to reduce the training points, or reduce the size of my study area.

But, is there any alternate way to execute the classification of a large study area, keeping the training points same? Please suggest how I can improve my code to achieve LULC, using GEE JavaScript Web API.

Best Answer

You run out of memory because your training data set has 2,656,173 features. sampleRegions() where each region is a polygon will sample all points within those polygons, at the scale you specified (10m).

To control the number of pixels sampled, you can use ee.Image.sample() instead. Here below, I've sampled 200 pixels from each of your seven feature collections. I pick up the class value from the first feature in each collection, assuming all features represent within a collection only represents a single class (I didn't bother to validate that assumption).

var classProperty = 'landcover'
var samplesPerClass = 200
var filtered = filtered.median()

var training = ee.FeatureCollection(
  [
    builtup, 
    water, 
    agriculture, 
    fallowland, 
    barrenland, 
    otherVegetation, 
    wetlands
  ].map(function (featureCollection) {
    return filtered.select(bands)
      .sample({
        region: featureCollection.geometry(), 
        scale: 10, 
        numPixels: samplesPerClass
      })
      .map(function (point) {
        return point.copyProperties(featureCollection.first(), [classProperty])
      })
  })
).flatten()

var classifier = ee.Classifier.smileRandomForest(50).train({
  features: training,
  classProperty: classProperty,
  inputProperties: bands
})
var classified = filtered.classify(classifier)

https://code.earthengine.google.com/16614ad867a90219f2633953a0a6faab

Related Question