1

I'm doing a supervised classification using training data from a 2023 Image to a 2022 Image. However, when doing the migration I got the error message "No valid training data were found" for the 2022 Image even though the 2023 Image worked as usual. The Image for 2023 has a previous pre processing to add some bands like elevation and index, and the training data was taken using this same 2023 Image.

The code is right here: https://code.earthengine.google.com/7e3ca5825d18782c457c32b38b793007

var distance = Image_2022.spectralDistance(Image_2023);

Map.addLayer(distance,{}, 'Distancia Espectral')
var samples= Forest.merge(Nonforest)

Map.addLayer(samples, {}, 'Puntos 2023')
var samples_distance= distance.sampleRegions({
 collection: samples,
 properties: ['name','id'],
 scale:10
})

Map.addLayer(samples_distance, {}, 'Puntos Migrados')

var threshold = 0.2
var newGcp = samples_distance.filter(ee.Filter.lt('distance', threshold));
print('Total GCPs', samples.size());
print('Migrated GCPs', newGcp.size());
performClassification(Image_2023, samples, 'Classificacion 2023');
performClassification(Image_2022, newGcp, 'Classificacion 2022');

function performClassification(Image_2022, samples, year) {
  var samples = samples.randomColumn();
  var trainingGcp = samples.filter(ee.Filter.lt('random', 0.6));
  var validationGcp = samples.filter(ee.Filter.gte('random', 0.6));
  
  // Overlay the point on the image to get training data.
  var training_2 = Image_2022.sampleRegions({
    collection: trainingGcp,
    properties: ['name','id'],
    scale: 10,
    tileScale: 16
  });
  
  // Train a classifier.
  var classifier_2 = ee.Classifier.smileRandomForest(50)
  .train({
    features: training_2,  
    classProperty: 'id',
    inputProperties: Image_2022.bandNames()
  });
   
  // Classify the image.
  var classified_2022 = Image_2022.classify(classifier_2);
  
  Map.addLayer(classified_2022, classVis, year);
  
  // Use classification map to assess accuracy using the validation fraction
  // of the overall training set created above.
  var test = classified_2022.sampleRegions({
    collection: validationGcp,
    properties: ['id'],
    tileScale: 16,
    scale: 10,
  });
  
  var testConfusionMatrix = test.errorMatrix('id', 'clasificacion')
  // Printing of confusion matrix may time out. Alternatively, you can export it as CSV
  print('Confusion Matrix ' + year, testConfusionMatrix);
  print('Test Accuracy ' + year, testConfusionMatrix.accuracy());

}
var styling = {color: 'black', fillColor: '00000000'};

Map.addLayer(Poligonos_Leaf.style(styling), {}, 'Poligonos Sosty')

I was following this methodology: https://courses.spatialthoughts.com/end-to-end-gee.html#classification-with-migrated-training-samples

1 Answer 1

0

The problem is that samples_distance doesn't contain any geometries. This cause training_2 = Image_2022.sampleRegions() to give you an empty collection, hence the "No training data" error. Luckily, there's a very simple fix - specify that you want to keep the geometries:

var samples_distance = distance.sampleRegions({
  collection: samples,
  properties: ['name', 'id'],
  scale: 10,
  geometries: true
})

https://code.earthengine.google.com/25aeb520d708a24a12333ace1065f247

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.