I am a beginner in GEE and I have a problem with making supervised classification from Sentinel-2 data. I used my pre-made shapefiles in five categories as training data. I tried more codes but the classification doesn't work. I don't know if it is problem with the data or I use the code wrong. But this problem appears Classified: Layer error: An internal error has occurred
var startDate = '2017-03-01';
var endDate = '2017-07-30';
var randomSeed = 0;
var checkbox1 = ui.Checkbox('S2 least cloudy', true);
var checkbox2 = ui.Checkbox('NDVI', false);
var checkbox3 = ui.Checkbox('Texture', false);
var checkbox4 = ui.Checkbox('Classified', true);
print(checkbox4);
print(checkbox3);
print(checkbox2);
print(checkbox1);
//////////Sentinel-2 data//////////
var s2 = ee.ImageCollection('COPERNICUS/S2')
.filterDate(startDate, endDate)
.filterBounds(roi);
// Get the dates of available images
var list = s2.aggregate_array('system:time_start').map(function(d) { return ee.Date(d)});
print('S2 images of the area during the study period',list);
// Get the cloud score of the images
var getCloudScores = function(img){
var value = ee.Image(img).get('CLOUDY_PIXEL_PERCENTAGE');
return ee.Feature(null, {'score': value});
};
var s2clouds = s2.map(getCloudScores);
print ('cloud score', ui.Chart.feature.byFeature(s2clouds));
// Sort images by least cloudy pixel %, select and rename the bands
var s2image = ee.ImageCollection('COPERNICUS/S2')
.filterDate(startDate, endDate)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
.sort('CLOUDY_PIXEL_PERCENTAGE')
.filterBounds(roi)
.map(function(img){
var t = img.select([ 'B1','B2','B3','B4','B5','B6','B7','B8','B8A',
'B9','B10', 'B11','B12']).divide(10000);//Rescale to 0-1
var out =
t.copyProperties(img).copyProperties(img,['system:time_start']);
return out;
})
.select(['B1','B2','B3','B4','B5','B6','B7','B8','B8A', 'B9','B10',
'B11','B12'],['aerosol', 'blue', 'green', 'red', 'red1','red2','red3','nir','red4','h2o',
'cirrus','swir1', 'swir2']);
// Obtain the least cloudy image and clip to the ROI
var s2leastCloud = ee.Image(s2image.first());
var s2ROI = s2leastCloud.clip(roi);
var vizParams = {bands: ['red', 'green', 'blue'], min: 0, max: 0.3};
Map.addLayer(s2ROI, vizParams, 'S2 least cloudy');
// Compute the Normalized Difference Vegetation Index (NDVI)
var red = s2ROI.select('red');
var nir = s2ROI.select('nir');
var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi, ndviParams, 'NDVI', false);
// Compute standard deviation of NDVI as texture
var texture = ndvi.reduceNeighborhood({
reducer: ee.Reducer.stdDev(),
kernel: ee.Kernel.square(5),
});
Map.addLayer(texture, {min: 0, max: 0.25},'Texture', false);
var s2final = s2ROI.addBands(ndvi).addBands(texture);
var bands = ['aerosol', 'blue', 'green', 'red', 'red1','red2','red3','nir','red4','h2o',
'cirrus','swir1', 'swir2','NDVI','NDVI_stdDev'];
var s2finalWbands = s2final.select(bands);
var n = randomSeed;
var randomR1_2 = R1_2.randomColumn('random', n);
var randomR1_4 = R1_4.randomColumn('random', n);
var randomR2_2 = R2_2.randomColumn('random', n);
var randomR2_3 = R2_3.randomColumn('random', n);
var randomR3 = R3.randomColumn('random', n);
var split_tr = 0.3;
var trainingSample = randomR1_2.filter(ee.Filter.lt('random', split_tr))
.merge(randomR1_4.filter(ee.Filter.lt('random', split_tr)))
.merge(randomR2_2.filter(ee.Filter.lt('random', split_tr)))
.merge(randomR2_3.filter(ee.Filter.lt('random', split_tr)))
.merge(randomR3.filter(ee.Filter.lt('random', split_tr)));
var split_test=0.7
var testingSample = randomR1_2.filter(ee.Filter.lt('random', split_test))
.merge(randomR1_4.filter(ee.Filter.lt('random', split_test)))
.merge(randomR2_2.filter(ee.Filter.lt('random', split_test)))
.merge(randomR2_3.filter(ee.Filter.lt('random', split_test)))
.merge(randomR3.filter(ee.Filter.lt('random', split_test)));
//classification
var s2classification =s2finalWbands.reproject(ee.Projection('EPSG:32647').atScale(10)).reduceResolution({reducer:
ee.Reducer.mean(),maxPixels: 65535});
var training = s2classification.sampleRegions({
collection: trainingSample,
properties: ['kateg'],
scale: 10,
});
var classifier = ee.Classifier.smileRandomForest({
numberOfTrees: 30,
variablesPerSplit: 4
})
.train(training, 'kateg');
var classified = s2classification.classify(classifier, 'classification');
var palette =['ff0000',// palm 0 (red)
'9933ff',//rubber 1 (purple)
'FF7F00',//betel 2 (orange)
'008000',//forest 3 (green)
'ffff00',//nonforest 4 (yellow)
'ffffff',//bare 5 (white)
];
Map.addLayer(classified, {min: 0, max: 6, palette: palette}, 'Classified');
Map.centerObject(roi, 10);
var trainAccuracy = classifier.confusionMatrix();
print('Resubstitution error matrix: ', trainAccuracy);
print('Training overall accuracy: ', trainAccuracy.accuracy());
var validation = s2classification.sampleRegions({
collection: testingSample,
properties: ['kateg'],
scale: 10,
});
var validated = validation.classify(classifier);
var testAccuracy = validated.errorMatrix('class', 'classification');
print ('Validation accuracy exported to "Tasks"');