来自 Sentinel-2 的 GEE 监督分类和 shapefile

问题描述

我是 GEE 的初学者,我在根据 Sentinel-2 数据进行监督分类时遇到了问题。我使用了五个类别的预制 shapefile 作为训练数据。我尝试了更多代码,但分类不起作用。不知道是数据有问题还是我用错了代码。但是出现这个问题 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','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','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','swir2','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,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,});

var validated = validation.classify(classifier);
var testAccuracy = validated.errorMatrix('class','classification');
print ('Validation accuracy exported to "Tasks"');

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...