তত্ত্বাবধানে থাকা ক্লাসিফিকেশন

Classifier প্যাকেজটি Earth Engine-এ চলা প্রথাগত ML অ্যালগরিদমের মাধ্যমে তত্ত্বাবধান করা ক্লাসিফিকেশন ম্যানেজ করে। এইসব ক্লাসিফায়ারের মধ্যে CART, RandomForest, NaiveBayes ও SVM অন্তর্ভুক্ত। শ্রেণীবিভাগের জন্য সাধারণ ওয়ার্কফ্লো হল:

  1. ট্রেনিং ডেটা সংগ্রহ করুন। এমন ফিচার অ্যাসেম্বল করুন যার প্রপার্টিতে পরিচিত ক্লাস লেবেল এবং প্রেডিক্টরের জন্য সংখ্যাসূচক মান স্টোর করা থাকে।
  2. ক্লাসিফায়ার ইনস্ট্যান্টিয়েট করুন। প্রয়োজন হলে এর প্যারামিটার সেট করুন।
  3. ট্রেনিং ডেটা ব্যবহার করে ক্লাসিফায়ারকে ট্রেনিং দিন।
  4. ছবি বা ফিচার সংগ্রহ শ্রেণীবদ্ধ করুন।
  5. স্বাধীন যাচাইকরণ ডেটার সাহায্যে আনুমানিক শ্রেণীকরণ সংক্রান্ত সমস্যা।

ট্রেনিং ডেটা হল একটি FeatureCollection যার একটি প্রপার্টি ক্লাস লেবেল স্টোর করে এবং অন্য প্রপার্টিগুলি প্রেডিক্টর ভেরিয়েবল স্টোর করে। ক্লাস লেবেলগুলি পরপর, 0 থেকে শুরু হওয়া পূর্ণসংখ্যা হতে হবে। প্রয়োজন হলে, ক্লাস ভ্যালু পরপর পূর্ণসংখ্যায় পরিবর্তন করতে remap() ব্যবহার করুন। প্রেডিক্টর সংখ্যাবাচক হতে হবে।

ট্রেনিং এবং/অথবা যাচাইকরণ ডেটা বিভিন্ন সোর্স থেকে আসতে পারে। Earth Engine-এ ইন্টার‍্যাক্টিভভাবে ট্রেনিং ডেটা সংগ্রহ করতে, আপনি জিওমেট্রি ড্রয়িং টুল ব্যবহার করতে পারেন (কোড এডিটর পৃষ্ঠার জিওমেট্রি টুল বিভাগ দেখুন)। বিকল্প হিসেবে, আপনি Earth Engine টেবিল অ্যাসেট থেকে আগে থেকে নির্ধারিত ট্রেনিং ডেটা ইমপোর্ট করতে পারবেন (বিস্তারিত জানতে টেবিল ডেটা ইমপোর্ট করা সংক্রান্ত পৃষ্ঠা দেখুন)। ee.Classifier-এর কোনও একটি কনস্ট্রাক্টর থেকে ক্লাসিফায়ার পান। classifier.train() ব্যবহার করে ক্লাসিফায়ারকে প্রশিক্ষণ দিন। classify() ব্যবহার করে Image বা FeatureCollection-এর শ্রেণীবিভাগ করো। নিম্নলিখিত উদাহরণে তিনটি সাধারণ ক্লাস অনুমান করার জন্য Classification and Regression Trees (CART) ক্লাসিফায়ার (Breiman et al. 1984) ব্যবহার করা হয়:

কোড এডিটর (জাভাস্ক্রিপ্ট)

// 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() ব্যবহার করুন।

ট্রেনিং ডেটা যদি সমজাতীয় অঞ্চলকে উপস্থাপনকারী পলিগন হয়, তাহলে প্রতিটি পলিগনের প্রতিটি পিক্সেল হল একটি ট্রেনিং পয়েন্ট। আপনি নিম্নলিখিত উদাহরণের মতো প্রশিক্ষণ দিতে পলিগন ব্যবহার করতে পারেন:

কোড এডিটর (জাভাস্ক্রিপ্ট)

// 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

এই উদাহরণে সাপোর্ট ভেক্টর মেশিন (SVM) ক্লাসিফায়ার ব্যবহার করা হয়েছে (Burges 1998)। মনে রাখবেন যে SVM কাস্টম প্যারামিটারের একটি সেট দিয়ে নির্দিষ্ট করা হয়। পূর্বাভাস সংক্রান্ত সমস্যার শারীরিক প্রকৃতি সম্পর্কে আগে থেকে তথ্য না থাকলে, অপ্টিমাল প্যারামিটার জানা যায় না। SVM-এর জন্য প্যারামিটার বেছে নেওয়ার ব্যাপারে মোটামুটি গাইড পেতে Hsu et al. (2003) দেখুন।

ক্লাসিফায়ার আউটপুট মোড

ee.Classifier.setOutputMode() পদ্ধতি তত্ত্বাবধান করা শ্রেণীবিন্যাসের ফলাফলের ফর্ম্যাট নিয়ন্ত্রণ করে, যার ফলে আউটপুটগুলি বিভিন্ন উপায়ে গঠন করা যায়:

  • শ্রেণীবিভাগ (ডিফল্ট): আউটপুট হল ক্লাস নম্বর।
  • রিগ্রেশন: আউটপুট হল স্ট্যান্ডার্ড রিগ্রেশনের ফলাফল।
  • সম্ভাবনা: আউটপুট হল শ্রেণীবিন্যাস সঠিক হওয়ার সম্ভাবনা।
  • MULTIPROBABILITY: আউটপুট হল সম্ভাবনার একটি অ্যারে যেখানে প্রতিটি ক্লাস দেখা ক্লাসের ক্রম অনুযায়ী সঠিক।
  • RAW: আউটপুট হল ক্লাসিফিকেশন প্রসেসের ইন্টার্নাল রিপ্রেজেন্টেশনের অ্যারে। যেমন, মাল্টি-ডিসিশন ট্রি মডেলে র-ভোট।
  • RAW_REGRESSION: আউটপুট হল রিগ্রেশন প্রসেসের ইন্টার্নাল রিপ্রেজেন্টেশনের অ্যারে। যেমন, একাধিক রিগ্রেশন ট্রির র-প্রেডিকশন।

এইসব আউটপুট মোডের জন্য সহায়তা আলাদা আলাদা হয়। নিম্নলিখিত সারণীতে প্রতিটি ক্লাসিফায়ারের জন্য কাজ করে এমন মোডের সংক্ষিপ্ত বিবরণ দেওয়া হয়েছে।

ক্লাসিফায়ার শ্রেণীবিভাগ রিগ্রেশন সম্ভাব্যতা মাল্টিপ্রোবাবিলিটি 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() ব্যবহার করুন। যেমন, ডিফল্ট ক্লাসিফিকেশন লেবেলের পরিবর্তে আউটপুট সম্ভাবনা পেতে, আপনি আগের কোড ব্লকে SVM ক্লাসিফায়ার কনফিগার করতে পারেন:

কোড এডিটর (জাভাস্ক্রিপ্ট)

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) ব্যবহার করুন। নিম্নলিখিত উদাহরণে MODIS রেফারেন্স ইমেজ থেকে ট্রেনিং ও যাচাইকরণ ডেটা তৈরি করতে sample() ব্যবহার করা হয় এবং ট্রেনিং ও যাচাইকরণের নির্ভুলতা উপস্থাপনকারী কনফিউশন ম্যাট্রিক্সের তুলনা করা হয়:

কোড এডিটর (জাভাস্ক্রিপ্ট)

// 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

এই উদাহরণে, MODIS ডেটাকে Landsat রেজোলিউশনে ডাউনস্কেল করার জন্য ১০টি ট্রি সহ একটি র‍্যান্ডম ফরেস্ট (Breiman 2001) ক্লাসিফায়ার ব্যবহার করা হয়েছে। sample() পদ্ধতি MODIS ডেটা থেকে দুটি র‍্যান্ডম স্যাম্পেল তৈরি করে: একটি ট্রেনিংয়ের জন্য এবং একটি যাচাইকরণের জন্য। ট্রেনিং স্যাম্পেল ব্যবহার করে ক্লাসিফায়ারকে ট্রেনিং দেওয়া হয়। আপনি classifier.confusionMatrix() থেকে ট্রেনিং ডেটার উপর রিসাবস্টিটিউশন অ্যাকুরেসি পেতে পারেন। যাচাইকরণের নির্ভুলতা পেতে, যাচাইকরণের ডেটা শ্রেণীবদ্ধ করুন। এটি যাচাইকরণে classification প্রপার্টি যোগ করে FeatureCollection। শ্রেণীবদ্ধ FeatureCollection-এ errorMatrix() কল করুন যাতে যাচাইকরণ (প্রত্যাশিত) নির্ভুলতা উপস্থাপনকারী একটি বিভ্রান্তি ম্যাট্রিক্স পাওয়া যায়।

আউটপুটটি পরীক্ষা করে দেখুন যে ট্রেনিং ডেটা থেকে অনুমান করা সামগ্রিক নির্ভুলতা যাচাইকরণ ডেটার চেয়ে অনেক বেশি। ট্রেনিং ডেটা থেকে অনুমান করা নির্ভুলতা হল একটি অতিরিক্ত অনুমান কারণ র‍্যান্ডম ফরেস্ট ট্রেনিং ডেটার সাথে "ফিট" করে। অজানা ডেটার ক্ষেত্রে প্রত্যাশিত সঠিকতা কম, যেমনটি যাচাইকরণ ডেটা থেকে অনুমান করা হয়েছে।

এছাড়াও, আপনি একটি স্যাম্পেল নিয়ে ফিচার কালেকশনে randomColumn() পদ্ধতির মাধ্যমে সেটি পার্টিশন করতে পারেন। আগের উদাহরণটি চালিয়ে যাওয়া যাক:

কোড এডিটর (জাভাস্ক্রিপ্ট)

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))

এছাড়াও, আপনি হয়ত নিশ্চিত করতে চাইবেন যে ট্রেনিং স্যাম্পেলগুলি মূল্যায়ন স্যাম্পেলগুলির সাথে সম্পর্কিত নয়। পূর্বাভাস দেওয়া ঘটনার স্পেশিয়াল অটো-কোরিলেশনের কারণে এটি হতে পারে। এইভাবে সম্পর্কিত হতে পারে এমন স্যাম্পেল বাদ দেওয়ার একটি উপায় হল, অন্য কোনও স্যাম্পেল থেকে কিছুটা দূরত্বে থাকা স্যাম্পেল সরিয়ে দেওয়া। এটি একটি স্পেশিয়াল জয়েনের মাধ্যমে করা যেতে পারে:

কোড এডিটর (জাভাস্ক্রিপ্ট)

// 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())

আগের স্নিপেটে, লক্ষ্য করুন যে geometries-কে true-এ সেট করা হয়েছে sample()-এ। স্পেশিয়াল জয়েনের জন্য প্রয়োজনীয় স্যাম্পেল পয়েন্টের স্পেশিয়াল তথ্য ধরে রাখার জন্য এটি করা হয়। এছাড়াও মনে রাখবেন যে tileScale-এর জন্য 16 সেট করা আছে। "ব্যবহারকারীর মেমরির সীমা পেরিয়ে গেছে" সংক্রান্ত সমস্যা এড়াতে এটি করা হয়।

ক্লাসিফায়ার সেভ করা হচ্ছে

ইনপুট ডেটার পরিমাণ অনেক বেশি হলে, ইন্টার‍্যাক্টিভভাবে ক্লাসিফায়ারকে ট্রেনিং দেওয়া সম্ভব নাও হতে পারে কারণ ইনপুট অনেক বড় (>৯৯ এমবি) অথবা ট্রেনিং দিতে অনেক বেশি সময় (৫ মিনিট) লাগে। ব্যাচ জব হিসেবে ক্লাসিফায়ার ট্রেনিং চালাতে Export.classifier.toAsset ব্যবহার করুন, যেখানে এটি আরও বেশি মেমরি সহ দীর্ঘ সময়ের জন্য চলতে পারে। ট্রেন করার জন্য ব্যয়বহুল ক্লাসিফায়ার এক্সপোর্ট ও রিলোড করা যেতে পারে যাতে আবার ট্রেন করার প্রয়োজন না হয়।

কোড এডিটর (জাভাস্ক্রিপ্ট)

// 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() অ্যালগরিদম ব্যবহার করুন, এক্সপোর্ট করা ক্লাসিফায়ার আইডি উল্লেখ করুন এবং অন্য যেকোনও প্রশিক্ষিত ক্লাসিফায়ারের মতো এটি ব্যবহার করুন।

কোড এডিটর (জাভাস্ক্রিপ্ট)

// 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