सुपरवाइज़्ड क्लासिफ़िकेशन

Classifier पैकेज, Earth Engine में चलने वाले पारंपरिक एमएल एल्गोरिदम की मदद से, सुपरवाइज़्ड क्लासिफ़िकेशन को हैंडल करता है. इन क्लासिफ़ायर में CART, RandomForest, NaiveBayes, और SVM शामिल हैं. कैटगरी तय करने का सामान्य वर्कफ़्लो इस तरह है:

  1. ट्रेनिंग डेटा इकट्ठा करना. ऐसी सुविधाओं को इकट्ठा करें जिनमें ऐसी प्रॉपर्टी हो जो जाने-पहचाने क्लास लेबल को सेव करती हो. साथ ही, ऐसी प्रॉपर्टी भी हों जो अनुमान लगाने वाले टूल के लिए न्यूमेरिक वैल्यू सेव करती हों.
  2. क्लासिफ़ायर को इंस्टैंशिएट करें. अगर ज़रूरी हो, तो इसके पैरामीटर सेट करें.
  3. ट्रेनिंग डेटा का इस्तेमाल करके, क्लासिफ़ायर को ट्रेन करें.
  4. किसी इमेज या सुविधा के कलेक्शन को अलग-अलग कैटगरी में बांटना.
  5. पुष्टि करने के लिए इकट्ठा किए गए डेटा की मदद से, कैटगरी तय करने में हुई गड़बड़ी का अनुमान लगाना.

ट्रेनिंग डेटा एक FeatureCollection है, जिसमें क्लास लेबल को स्टोर करने वाली प्रॉपर्टी और अनुमान लगाने वाले वैरिएबल को स्टोर करने वाली प्रॉपर्टी होती हैं. क्लास लेबल, 0 से शुरू होने वाले पूर्णांक होने चाहिए. अगर ज़रूरी हो, तो क्लास वैल्यू को लगातार पूर्णांकों में बदलने के लिए, remap() का इस्तेमाल करें. अनुमान लगाने वाले वैरिएबल, अंकों में होने चाहिए.

ट्रेनिंग और/या पुष्टि करने वाला डेटा, अलग-अलग सोर्स से मिल सकता है. Earth Engine में इंटरैक्टिव तरीके से ट्रेनिंग डेटा इकट्ठा करने के लिए, ज्यामिति ड्रॉइंग टूल का इस्तेमाल किया जा सकता है. इसके बारे में ज़्यादा जानने के लिए, कोड एडिटर पेज का ज्यामिति टूल सेक्शन देखें. इसके अलावा, Earth Engine टेबल ऐसेट से पहले से तय ट्रेनिंग डेटा इंपोर्ट किया जा सकता है. ज़्यादा जानकारी के लिए, टेबल डेटा इंपोर्ट करना पेज देखें. ee.Classifier में मौजूद किसी कंस्ट्रक्टर से क्लासिफ़ायर पाएं. classifier.train() का इस्तेमाल करके, क्लासिफ़ायर को ट्रेनिंग दें. classify() का इस्तेमाल करके, Image या FeatureCollection को कैटगरी में बांटें. यहां दिए गए उदाहरण में, तीन सामान्य क्लास का अनुमान लगाने के लिए, क्लासिफ़िकेशन ऐंड रिग्रेशन ट्री (सीएआरटी) क्लासिफ़ायर (Breiman et al. 1984) का इस्तेमाल किया गया है:

कोड एडिटर (JavaScript)

// Define a function that scales and masks Landsat 8 surface reflectance images.
function prepSrL8(image) {
  // Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var getFactorImg = function(factorNames) {
    var factorList = image.toDictionary().select(factorNames).values();
    return ee.Image.constant(factorList);
  };
  var scaleImg = getFactorImg([
    'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10']);
  var offsetImg = getFactorImg([
    'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10']);
  var scaled = image.select('SR_B.|ST_B10').multiply(scaleImg).add(offsetImg);

  // Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, null, true)
    .updateMask(qaMask).updateMask(saturationMask);
}

// Make a cloud-free Landsat 8 surface reflectance composite.
var image = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  .filterDate('2021-03-01', '2021-07-01')
  .map(prepSrL8)
  .median();

// Use these bands for prediction.
var bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5',
             'SR_B6', 'SR_B7', 'ST_B10'];

// Load training points. The numeric property 'class' stores known labels.
var points = ee.FeatureCollection('GOOGLE/EE/DEMOS/demo_landcover_labels');

// This property stores the land cover labels as consecutive
// integers starting from zero.
var label = 'landcover';

// Overlay the points on the imagery to get training.
var training = image.select(bands).sampleRegions({
  collection: points,
  properties: [label],
  scale: 30
});

// Train a CART classifier with default parameters.
var trained = ee.Classifier.smileCart().train(training, label, bands);

// Classify the image with the same bands used for training.
var classified = image.select(bands).classify(trained);

// Display the inputs and the results.
Map.setCenter(-122.0877, 37.7880, 11);
Map.addLayer(image,
             {bands: ['SR_B4', 'SR_B3', 'SR_B2'], min: 0, max: 0.25},
             'image');
Map.addLayer(classified,
             {min: 0, max: 2, palette: ['orange', 'green', 'blue']},
             'classification');

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Define a function that scales and masks Landsat 8 surface reflectance images.
def prep_sr_l8(image):
  """Scales and masks Landsat 8 surface reflectance images."""
  # Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  qa_mask = image.select('QA_PIXEL').bitwiseAnd(0b11111).eq(0)
  saturation_mask = image.select('QA_RADSAT').eq(0)

  # Apply the scaling factors to the appropriate bands.
  def _get_factor_img(factor_names):
    factor_list = image.toDictionary().select(factor_names).values()
    return ee.Image.constant(factor_list)

  scale_img = _get_factor_img([
      'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10'])
  offset_img = _get_factor_img([
      'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10'])
  scaled = image.select('SR_B.|ST_B10').multiply(scale_img).add(offset_img)

  # Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, None, True).updateMask(
      qa_mask).updateMask(saturation_mask)


# Make a cloud-free Landsat 8 surface reflectance composite.
l8_image = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterDate('2021-03-01', '2021-07-01')
    .map(prep_sr_l8)
    .median())

# Use these bands for prediction.
bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'ST_B10']

# Load training points. The numeric property 'class' stores known labels.
points = ee.FeatureCollection('GOOGLE/EE/DEMOS/demo_landcover_labels')

# This property stores the land cover labels as consecutive
# integers starting from zero.
label = 'landcover'

# Overlay the points on the imagery to get training.
training = l8_image.select(bands).sampleRegions(
    collection=points, properties=[label], scale=30
)

# Train a CART classifier with default parameters.
trained = ee.Classifier.smileCart().train(training, label, bands)

# Classify the image with the same bands used for training.
classified = l8_image.select(bands).classify(trained)

# Display the inputs and the results.
m = geemap.Map()
m.set_center(-122.0877, 37.7880, 11)
m.add_layer(
    l8_image,
    {'bands': ['SR_B4', 'SR_B3', 'SR_B2'], 'min': 0, 'max': 0.25},
    'image',
)
m.add_layer(
    classified,
    {'min': 0, 'max': 2, 'palette': ['orange', 'green', 'blue']},
    'classification',
)
m

इस उदाहरण में, टेबल में मौजूद ट्रेनिंग पॉइंट सिर्फ़ क्लास लेबल को सेव करते हैं. ध्यान दें कि ट्रेनिंग प्रॉपर्टी ('landcover') में, 0 से शुरू होने वाले लगातार पूर्णांक सेव होते हैं. अगर ज़रूरी हो, तो अपनी टेबल पर remap() का इस्तेमाल करके, क्लास लेबल को शून्य से शुरू होने वाले लगातार पूर्णांकों में बदलें. यह भी ध्यान दें कि टेबल में अनुमान लगाने वाले वैरिएबल पाने और ट्रेनिंग डेटासेट बनाने के लिए, image.sampleRegions() का इस्तेमाल किया गया है. क्लासिफ़ायर को ट्रेनिंग देने के लिए, क्लास लेबल प्रॉपर्टी का नाम और ट्रेनिंग टेबल में मौजूद प्रॉपर्टी की सूची बताएं. क्लासिफ़ायर को इस सूची का इस्तेमाल, अनुमान लगाने के लिए करना चाहिए. जिस इमेज को अलग-अलग कैटगरी में बांटना है उसमें मौजूद बैंड की संख्या और क्रम, classifier.train() को दी गई प्रॉपर्टी की सूची के क्रम से पूरी तरह मेल खाना चाहिए. image.select() का इस्तेमाल करके, यह पक्का करें कि क्लासिफ़ायर स्कीमा, इमेज से मैच करता हो.

अगर ट्रेनिंग डेटा, एक जैसे इलाकों को दिखाने वाले पॉलीगॉन हैं, तो हर पॉलीगॉन में मौजूद हर पिक्सल एक ट्रेनिंग पॉइंट होता है. मॉडल को ट्रेनिंग देने के लिए, पॉलीगॉन का इस्तेमाल किया जा सकता है. इसके बारे में यहां दिए गए उदाहरण में बताया गया है:

कोड एडिटर (JavaScript)

// Define a function that scales and masks Landsat 8 surface reflectance images.
function prepSrL8(image) {
  // Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var getFactorImg = function(factorNames) {
    var factorList = image.toDictionary().select(factorNames).values();
    return ee.Image.constant(factorList);
  };
  var scaleImg = getFactorImg([
    'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10']);
  var offsetImg = getFactorImg([
    'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10']);
  var scaled = image.select('SR_B.|ST_B10').multiply(scaleImg).add(offsetImg);

  // Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, null, true)
    .updateMask(qaMask).updateMask(saturationMask);
}

// Make a cloud-free Landsat 8 surface reflectance composite.
var image = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  .filterDate('2018-01-01', '2019-01-01')
  .map(prepSrL8)
  .median();

// Use these bands for prediction.
var bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5',
             'SR_B6', 'SR_B7'];

// Manually created polygons.
var forest1 = ee.Geometry.Rectangle(-63.0187, -9.3958, -62.9793, -9.3443);
var forest2 = ee.Geometry.Rectangle(-62.8145, -9.206, -62.7688, -9.1735);
var nonForest1 = ee.Geometry.Rectangle(-62.8161, -9.5001, -62.7921, -9.4486);
var nonForest2 = ee.Geometry.Rectangle(-62.6788, -9.044, -62.6459, -8.9986);

// Make a FeatureCollection from the hand-made geometries.
var polygons = ee.FeatureCollection([
  ee.Feature(nonForest1, {'class': 0}),
  ee.Feature(nonForest2, {'class': 0}),
  ee.Feature(forest1, {'class': 1}),
  ee.Feature(forest2, {'class': 1}),
]);

// Get the values for all pixels in each polygon in the training.
var training = image.sampleRegions({
  // Get the sample from the polygons FeatureCollection.
  collection: polygons,
  // Keep this list of properties from the polygons.
  properties: ['class'],
  // Set the scale to get Landsat pixels in the polygons.
  scale: 30
});

// Create an SVM classifier with custom parameters.
var classifier = ee.Classifier.libsvm({
  kernelType: 'RBF',
  gamma: 0.5,
  cost: 10
});

// Train the classifier.
var trained = classifier.train(training, 'class', bands);

// Classify the image.
var classified = image.classify(trained);

// Display the classification result and the input image.
Map.setCenter(-62.836, -9.2399, 9);
Map.addLayer(image,
             {bands: ['SR_B4', 'SR_B3', 'SR_B2'], min: 0, max: 0.25},
             'image');
Map.addLayer(polygons, {color: 'yellow'}, 'training polygons');
Map.addLayer(classified,
             {min: 0, max: 1, palette: ['orange', 'green']},
             'deforestation');

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Define a function that scales and masks Landsat 8 surface reflectance images.
def prep_sr_l8(image):
  # Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  qa_mask = image.select('QA_PIXEL').bitwiseAnd(0b11111).eq(0)
  saturation_mask = image.select('QA_RADSAT').eq(0)

  # Apply the scaling factors to the appropriate bands.
  def _get_factor_img(factor_names):
    factor_list = image.toDictionary().select(factor_names).values()
    return ee.Image.constant(factor_list)
  scale_img = _get_factor_img([
      'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10'])
  offset_img = _get_factor_img([
      'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10'])
  scaled = image.select('SR_B.|ST_B10').multiply(scale_img).add(offset_img)

  # Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, None, True).updateMask(
      qa_mask).updateMask(saturation_mask)


# Make a cloud-free Landsat 8 surface reflectance composite.
l8_image = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterDate('2018-01-01', '2019-01-01')
    .map(prep_sr_l8)
    .median())

# Use these bands for prediction.
bands = ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7']

# Manually created polygons.
forest1 = ee.Geometry.Rectangle(-63.0187, -9.3958, -62.9793, -9.3443)
forest2 = ee.Geometry.Rectangle(-62.8145, -9.206, -62.7688, -9.1735)
non_forest1 = ee.Geometry.Rectangle(-62.8161, -9.5001, -62.7921, -9.4486)
non_forest2 = ee.Geometry.Rectangle(-62.6788, -9.044, -62.6459, -8.9986)

# Make a FeatureCollection from the hand-made geometries.
polygons = ee.FeatureCollection([
    ee.Feature(non_forest1, {'class': 0}),
    ee.Feature(non_forest1, {'class': 0}),
    ee.Feature(forest1, {'class': 1}),
    ee.Feature(forest2, {'class': 1}),
])

# Get the values for all pixels in each polygon in the training.
training = l8_image.sampleRegions(
    # Get the sample from the polygons FeatureCollection.
    collection=polygons,
    # Keep this list of properties from the polygons.
    properties=['class'],
    # Set the scale to get Landsat pixels in the polygons.
    scale=30,
)

# Create an SVM classifier with custom parameters.
classifier = ee.Classifier.libsvm(kernelType='RBF', gamma=0.5, cost=10)

# Train the classifier.
trained = classifier.train(training, 'class', bands)

# Classify the image.
classified = l8_image.classify(trained)

# Display the classification result and the input image.
m = geemap.Map()
m.set_center(-62.836, -9.2399, 9)
m.add_layer(
    l8_image,
    {'bands': ['SR_B4', 'SR_B3', 'SR_B2'], 'min': 0, 'max': 0.25},
    'image',
)
m.add_layer(polygons, {'color': 'yellow'}, 'training polygons')
m.add_layer(
    classified,
    {'min': 0, 'max': 1, 'palette': ['orange', 'green']},
    'deforestation',
)
m

इस उदाहरण में, सपोर्ट वेक्टर मशीन (एसवीएम) क्लासिफ़ायर (Burges 1998) का इस्तेमाल किया गया है. ध्यान दें कि एसवीएम को कस्टम पैरामीटर के सेट के साथ तय किया जाता है. अनुमान लगाने से जुड़ी समस्या की भौतिक प्रकृति के बारे में पहले से जानकारी न होने पर, ऑप्टिमल पैरामीटर के बारे में पता नहीं चलता. एसवीएम के लिए पैरामीटर चुनने के बारे में सामान्य जानकारी पाने के लिए, Hsu et al. (2003) देखें.

क्लासिफ़ायर के आउटपुट मोड

ee.Classifier.setOutputMode() मेथड, निगरानी में किए गए क्लासिफ़िकेशन के नतीजों के फ़ॉर्मैट को कंट्रोल करता है. इससे आउटपुट को कई अलग-अलग तरीकों से स्ट्रक्चर किया जा सकता है:

  • CLASSIFICATION (डिफ़ॉल्ट): आउटपुट, क्लास नंबर होता है.
  • REGRESSION: आउटपुट, स्टैंडर्ड रिग्रेशन का नतीजा होता है.
  • PROBABILITY: आउटपुट, इस बात की संभावना है कि क्लासिफ़िकेशन सही है.
  • MULTIPROBABILITY: आउटपुट, संभावनाओं का एक कलेक्शन होता है. इसमें हर क्लास को, देखी गई क्लास के हिसाब से सही क्रम में लगाया जाता है.
  • RAW: आउटपुट, क्लासिफ़िकेशन की प्रोसेस के इंटरनल रिप्रज़ेंटेशन का एक कलेक्शन होता है. उदाहरण के लिए, मल्टी-डिसीज़न ट्री मॉडल में रॉ वोट.
  • RAW_REGRESSION: आउटपुट, रिग्रेशन प्रोसेस के इंटरनल रिप्रज़ेंटेशन का एक कलेक्शन होता है. उदाहरण के लिए, कई रिग्रेशन ट्री के रॉ अनुमान.

इन आउटपुट मोड की उपलब्धता अलग-अलग हो सकती है. नीचे दी गई टेबल में, हर क्लासिफ़ायर के लिए काम करने वाले मोड की खास जानकारी दी गई है.

क्लासिफ़ायर कैटगरी रिग्रेशन प्रॉबेबिलिटी MULTIPROBABILITY RAW RAW_REGRESSION
ee.Classifier.amnhMaxent
ee.Classifier.minimumDistance
ee.Classifier.smileCart
ee.Classifier.smileGradientTreeBoost
ee.Classifier.smileKNN
ee.Classifier.smileNaiveBayes
ee.Classifier.smileRandomForest
ee.Classifier.libsvm C_SVC
ee.Classifier.libsvm NU_SVC
ee.Classifier.libsvm ONE_CLASS
ee.Classifier.libsvm EPSILON_SVR
ee.Classifier.libsvm NU_SVR

आउटपुट फ़ॉर्मैट तय करने के लिए, क्लासिफ़ायर को ट्रेनिंग देने से पहले setOutputMode() का इस्तेमाल करें. उदाहरण के लिए, पिछले कोड ब्लॉक में एसवीएम क्लासिफ़ायर को कॉन्फ़िगर करके, डिफ़ॉल्ट क्लासिफ़िकेशन लेबल के बजाय आउटपुट की संभावना को दिखाया जा सकता है:

कोड एडिटर (JavaScript)

var classifier = ee.Classifier.libsvm({
  kernelType: 'RBF',
  gamma: 0.5,
  cost: 10
}).setOutputMode('PROBABILITY');

var trained = classifier.train(training, 'class', bands);

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

classifier = ee.Classifier.libsvm(
    kernelType='RBF', gamma=0.5, cost=10
).setOutputMode('PROBABILITY')

trained = classifier.train(training, 'class', bands)

सटीकता का आकलन

क्लासिफ़ायर की सटीक जानकारी का आकलन करने के लिए, ConfusionMatrix का इस्तेमाल करें (Stehman 1997). sample() का इस्तेमाल करके, MODIS की रेफ़रंस इमेज से ट्रेनिंग और पुष्टि करने वाला डेटा जनरेट करने का उदाहरण यहां दिया गया है. साथ ही, ट्रेनिंग और पुष्टि करने की प्रोसेस की सटीक जानकारी देने वाले कन्फ़्यूज़न मैट्रिक्स की तुलना की गई है:

कोड एडिटर (JavaScript)

// Define a region of interest.
var roi = ee.Geometry.BBox(-122.93, 36.99, -121.20, 38.16);

// Define a function that scales and masks Landsat 8 surface reflectance images.
function prepSrL8(image) {
  // Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var getFactorImg = function(factorNames) {
    var factorList = image.toDictionary().select(factorNames).values();
    return ee.Image.constant(factorList);
  };
  var scaleImg = getFactorImg([
    'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10']);
  var offsetImg = getFactorImg([
    'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10']);
  var scaled = image.select('SR_B.|ST_B10').multiply(scaleImg).add(offsetImg);

  // Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, null, true)
    .updateMask(qaMask).updateMask(saturationMask);
}

// Make a cloud-free Landsat 8 surface reflectance composite.
var input = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterBounds(roi)
    .filterDate('2020-03-01', '2020-07-01')
    .map(prepSrL8)
    .median()
    .setDefaultProjection('EPSG:4326', null, 30)
    .select(['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7']);

// Use MODIS land cover, IGBP classification, for training.
var modis = ee.Image('MODIS/006/MCD12Q1/2020_01_01')
    .select('LC_Type1');

// Sample the input imagery to get a FeatureCollection of training data.
var training = input.addBands(modis).sample({
  region: roi,
  numPixels: 5000,
  seed: 0
});

// Make a Random Forest classifier and train it.
var classifier = ee.Classifier.smileRandomForest(10)
    .train({
      features: training,
      classProperty: 'LC_Type1',
      inputProperties: ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7']
    });

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

// Get a confusion matrix representing resubstitution accuracy.
var trainAccuracy = classifier.confusionMatrix();
print('Resubstitution error matrix: ', trainAccuracy);
print('Training overall accuracy: ', trainAccuracy.accuracy());

// Sample the input with a different random seed to get validation data.
var validation = input.addBands(modis).sample({
  region: roi,
  numPixels: 5000,
  seed: 1
  // Filter the result to get rid of any null pixels.
}).filter(ee.Filter.notNull(input.bandNames()));

// Classify the validation data.
var validated = validation.classify(classifier);

// Get a confusion matrix representing expected accuracy.
var testAccuracy = validated.errorMatrix('LC_Type1', 'classification');
print('Validation error matrix: ', testAccuracy);
print('Validation overall accuracy: ', testAccuracy.accuracy());

// Define a palette for the IGBP classification.
var igbpPalette = [
  'aec3d4', // water
  '152106', '225129', '369b47', '30eb5b', '387242', // forest
  '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40',  // shrub, grass
  '111149', // wetlands
  'cdb33b', // croplands
  'cc0013', // urban
  '33280d', // crop mosaic
  'd7cdcc', // snow and ice
  'f7e084', // barren
  '6f6f6f'  // tundra
];

// Display the input and the classification.
Map.centerObject(roi, 10);
Map.addLayer(input.clip(roi),
             {bands: ['SR_B4', 'SR_B3', 'SR_B2'], min: 0, max: 0.25},
             'landsat');
Map.addLayer(classified.clip(roi),
             {palette: igbpPalette, min: 0, max: 17},
             'classification');

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Define a region of interest.
roi = ee.Geometry.BBox(-122.93, 36.99, -121.20, 38.16)

# Define a function that scales and masks Landsat 8 surface reflectance images.
def prep_sr_l8(image):
  """Scales and masks Landsat 8 surface reflectance images."""
  # Develop masks for unwanted pixels (fill, cloud, cloud shadow).
  qa_mask = image.select('QA_PIXEL').bitwiseAnd(0b1111).eq(0)
  saturation_mask = image.select('QA_RADSAT').eq(0)

  # Apply the scaling factors to the appropriate bands.
  def _get_factor_img(factor_names):
    factor_list = image.toDictionary().select(factor_names).values()
    return ee.Image.constant(factor_list)

  scale_img = _get_factor_img([
      'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10'])
  offset_img = _get_factor_img([
      'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10'])
  scaled = image.select('SR_B.|ST_B10').multiply(scale_img).add(offset_img)

  # Replace original bands with scaled bands and apply masks.
  return image.addBands(scaled, None, True).updateMask(
      qa_mask).updateMask(saturation_mask)


# Make a cloud-free Landsat 8 surface reflectance composite.
input_image = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterBounds(roi)
    .filterDate('2020-03-01', '2020-07-01')
    .map(prep_sr_l8)
    .median()
    .setDefaultProjection('EPSG:4326', None, 30)
    .select(['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7'])
)

# Use MODIS land cover, IGBP classification, for training.
modis = ee.Image('MODIS/006/MCD12Q1/2020_01_01').select('LC_Type1')

# Sample the input imagery to get a FeatureCollection of training data.
training = input_image.addBands(modis).sample(
    region=roi, numPixels=5000, seed=0
)

# Make a Random Forest classifier and train it.
classifier = ee.Classifier.smileRandomForest(10).train(
    features=training,
    classProperty='LC_Type1',
    inputProperties=['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7'],
)

# Classify the input imagery.
classified = input_image.classify(classifier)

# Get a confusion matrix representing resubstitution accuracy.
train_accuracy = classifier.confusionMatrix()
display('Resubstitution error matrix:', train_accuracy)
display('Training overall accuracy:', train_accuracy.accuracy())

# Sample the input with a different random seed to get validation data.
validation = (
    input_image.addBands(modis)
    .sample(
        region=roi,
        numPixels=5000,
        seed=1,
        # Filter the result to get rid of any null pixels.
    )
    .filter(ee.Filter.notNull(input_image.bandNames()))
)

# Classify the validation data.
validated = validation.classify(classifier)

# Get a confusion matrix representing expected accuracy.
test_accuracy = validated.errorMatrix('LC_Type1', 'classification')
display('Validation error matrix:', test_accuracy)
display('Validation overall accuracy:', test_accuracy.accuracy())

# Define a palette for the IGBP classification.
igbp_palette = [
    'aec3d4',  # water
    '152106', '225129', '369b47', '30eb5b', '387242',  # forest
    '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40',  # shrub, grass
    '111149',  # wetlands
    'cdb33b',  # croplands
    'cc0013',  # urban
    '33280d',  # crop mosaic
    'd7cdcc',  # snow and ice
    'f7e084',  # barren
    '6f6f6f'   # tundra
]

# Display the input and the classification with geemap in a notebook.
m = geemap.Map()
m.center_object(roi, 10)
m.add_layer(
    input_image.clip(roi),
    {'bands': ['SR_B4', 'SR_B3', 'SR_B2'], 'min': 0, 'max': 0.25},
    'landsat',
)
m.add_layer(
    classified.clip(roi),
    {'palette': igbp_palette, 'min': 0, 'max': 17},
    'classification',
)
m

इस उदाहरण में, रैंडम फ़ॉरेस्ट (Breiman 2001) क्लासिफ़ायर का इस्तेमाल किया गया है. इसमें 10 ट्री हैं, ताकि MODIS डेटा को Landsat रिज़ॉल्यूशन में डाउनस्केल किया जा सके. sample() तरीका, MODIS डेटा से दो रैंडम सैंपल जनरेट करता है: एक ट्रेनिंग के लिए और एक पुष्टि के लिए. ट्रेनिंग सैंपल का इस्तेमाल, क्लासिफ़ायर को ट्रेनिंग देने के लिए किया जाता है. classifier.confusionMatrix() से ट्रेनिंग डेटा पर, रीसबस्टिट्यूशन की सटीक जानकारी मिल सकती है. पुष्टि करने की प्रोसेस को सटीक बनाने के लिए, पुष्टि करने के डेटा को अलग-अलग कैटगरी में बांटें. इससे पुष्टि करने की प्रोसेस में classification प्रॉपर्टी जुड़ जाती है FeatureCollection. वर्गीकृत FeatureCollection पर errorMatrix() को कॉल करें, ताकि पुष्टि (अनुमानित) की सटीक जानकारी दिखाने वाला कन्फ़्यूज़न मैट्रिक्स मिल सके.

आउटपुट की जांच करके देखें कि ट्रेनिंग डेटा से अनुमानित कुल सटीक जानकारी, पुष्टि करने वाले डेटा से ज़्यादा है या नहीं. ट्रेनिंग डेटा से अनुमानित सटीक जानकारी, ज़रूरत से ज़्यादा अनुमानित होती है. ऐसा इसलिए, क्योंकि रैंडम फ़ॉरेस्ट, ट्रेनिंग डेटा के लिए “फ़िट” होता है. अनजान डेटा के लिए, अनुमानित सटीक जानकारी कम होती है. इसकी जानकारी, पुष्टि करने के लिए इकट्ठा किए गए डेटा से मिलती है.

इसके अलावा, एक सैंपल लेकर उसे फ़ीचर कलेक्शन पर randomColumn() तरीके से बांटा जा सकता है. पिछले उदाहरण को जारी रखते हुए:

कोड एडिटर (JavaScript)

var sample = input.addBands(modis).sample({
  region: roi,
  numPixels: 5000,
  seed: 0
});

// The randomColumn() method will add a column of uniform random
// numbers in a column named 'random' by default.
sample = sample.randomColumn();

var split = 0.7;  // Roughly 70% training, 30% testing.
var training = sample.filter(ee.Filter.lt('random', split));
var validation = sample.filter(ee.Filter.gte('random', split));

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

sample = input_image.addBands(modis).sample(region=roi, numPixels=5000, seed=0)

# The randomColumn() method will add a column of uniform random
# numbers in a column named 'random' by default.
sample = sample.randomColumn()

split = 0.7  # Roughly 70% training, 30% testing.
training = sample.filter(ee.Filter.lt('random', split))
validation = sample.filter(ee.Filter.gte('random', split))

आपको यह भी पक्का करना चाहिए कि ट्रेनिंग सैंपल, आकलन के सैंपल से अलग हों. ऐसा, अनुमानित घटना के स्पेशल ऑटोकोरिलेशन की वजह से हो सकता है. इस तरह से कोरिलेट किए जा सकने वाले सैंपल को बाहर रखने का एक तरीका यह है कि उन सैंपल को हटा दिया जाए जो किसी अन्य सैंपल से कुछ दूरी पर हैं. स्पेशल जॉइन की मदद से, ऐसा किया जा सकता है:

कोड एडिटर (JavaScript)

// Sample the input imagery to get a FeatureCollection of training data.
var sample = input.addBands(modis).sample({
  region: roi,
  numPixels: 5000,
  seed: 0,
  geometries: true,
  tileScale: 16
});

// The randomColumn() method will add a column of uniform random
// numbers in a column named 'random' by default.
sample = sample.randomColumn();

var split = 0.7;  // Roughly 70% training, 30% testing.
var training = sample.filter(ee.Filter.lt('random', split));
print('Training size:', training.size());
var validation = sample.filter(ee.Filter.gte('random', split));

// Spatial join.
var distFilter = ee.Filter.withinDistance({
  distance: 1000,
  leftField: '.geo',
  rightField: '.geo',
  maxError: 10
});

var join = ee.Join.inverted();

// Apply the join.
training = join.apply(training, validation, distFilter);
print('Training size after spatial filtering:', training.size());

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Sample the input imagery to get a FeatureCollection of training data.
sample = input_image.addBands(modis).sample(
    region=roi, numPixels=5000, seed=0, geometries=True, tileScale=16
)

# The randomColumn() method will add a column of uniform random
# numbers in a column named 'random' by default.
sample = sample.randomColumn()

split = 0.7  # Roughly 70% training, 30% testing.
training = sample.filter(ee.Filter.lt('random', split))
display('Training size:', training.size())
validation = sample.filter(ee.Filter.gte('random', split))

# Spatial join.
dist_filter = ee.Filter.withinDistance(
    distance=1000, leftField='.geo', rightField='.geo', maxError=10
)

join = ee.Join.inverted()

# Apply the join.
training = join.apply(training, validation, dist_filter)
display('Training size after spatial filtering:', training.size())

पिछले स्निपेट में, ध्यान दें कि sample() में geometries को true पर सेट किया गया है. ऐसा इसलिए किया जाता है, ताकि स्पेशल जॉइन के लिए ज़रूरी सैंपल पॉइंट की स्पेशल जानकारी को सेव रखा जा सके. यह भी ध्यान रखें कि tileScale को 16 पर सेट किया गया हो. ऐसा इसलिए किया जाता है, ताकि "उपयोगकर्ता की मेमोरी की सीमा पार हो गई है" गड़बड़ी से बचा जा सके.

क्लासिफ़ायर सेव किए जा रहे हैं

इनपुट डेटा की बड़ी मात्रा पर क्लासिफ़ायर को इंटरैक्टिव तरीके से ट्रेनिंग देना शायद संभव न हो. ऐसा इसलिए, क्योंकि इनपुट बहुत बड़ा (>99 एमबी) है या ट्रेनिंग में बहुत ज़्यादा समय (5 मिनट) लगता है. क्लासिफ़ायर ट्रेनिंग को बैच जॉब के तौर पर चलाने के लिए, Export.classifier.toAsset का इस्तेमाल करें. इससे, ज़्यादा मेमोरी के साथ इसे ज़्यादा समय तक चलाया जा सकता है. ट्रेन करने में ज़्यादा समय लेने वाले क्लासिफ़ायर को एक्सपोर्ट और फिर से लोड किया जा सकता है, ताकि उन्हें फिर से ट्रेन न करना पड़े.

कोड एडिटर (JavaScript)

// Using the random forest classifier defined earlier, export the random
// forest classifier as an Earth Engine asset.
var classifierAssetId = 'projects/<PROJECT-ID>/assets/upscaled_MCD12Q1_random_forest';
Export.classifier.toAsset(
  classifier,
  'Saved-random-forest-IGBP-classification',
  classifierAssetId
);

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Using the random forest classifier defined earlier, export the random
# forest classifier as an Earth Engine asset.
classifier_asset_id = (
    'projects/<PROJECT-ID>/assets/upscaled_MCD12Q1_random_forest'
)
task = ee.batch.Export.classifier.toAsset(
    classifier, 'Saved-random-forest-IGBP-classification', classifier_asset_id
)
task.start()

सेव किए गए क्लासिफ़ायर को लोड करने के लिए, एल्गोरिदम ee.Classifier.load() का इस्तेमाल करें. साथ ही, एक्सपोर्ट किए गए क्लासिफ़ायर का आईडी डालें और उसे किसी अन्य ट्रेन किए गए क्लासिफ़ायर की तरह इस्तेमाल करें.

कोड एडिटर (JavaScript)

// Once the classifier export finishes, we can load our saved classifier.
var savedClassifier = ee.Classifier.load(classifierAssetId);
// We can perform classification just as before with the saved classifier now.
var classified = input.classify(savedClassifier);
Map.addLayer(classified.clip(roi),
             {palette: igbpPalette, min: 0, max: 17},
             'classification');

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Once the classifier export finishes, we can load our saved classifier.
saved_classifier = ee.Classifier.load(classifier_asset_id)
# We can perform classification just as before with the saved classifier now.
classified = input_image.classify(saved_classifier)

m = geemap.Map()
m.center_object(roi, 10)
m.add_layer(
    classified.clip(roi),
    {'palette': igbp_palette, 'min': 0, 'max': 17},
    'classification',
)
m