তত্ত্বাবধানে শ্রেণীবিভাগ

Classifier প্যাকেজ আর্থ ইঞ্জিনে চলমান ঐতিহ্যগত ML অ্যালগরিদম দ্বারা তত্ত্বাবধানে শ্রেণীবিভাগ পরিচালনা করে। এই ক্লাসিফায়ারগুলির মধ্যে রয়েছে CART, RandomForest, NaiveBayes এবং SVM। শ্রেণীবিভাগের জন্য সাধারণ কর্মপ্রবাহ হল:

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

প্রশিক্ষণের ডেটা হল একটি FeatureCollection যা ক্লাস লেবেল সংরক্ষণ করে এবং ভবিষ্যদ্বাণীকারী ভেরিয়েবল সংরক্ষণকারী বৈশিষ্ট্যগুলি। ক্লাস লেবেলগুলো ধারাবাহিক হওয়া উচিত, পূর্ণসংখ্যা 0 থেকে শুরু হচ্ছে। প্রয়োজনে, ক্লাসের মানগুলোকে ধারাবাহিক পূর্ণসংখ্যাতে রূপান্তর করতে remap() ব্যবহার করুন। ভবিষ্যদ্বাণী সংখ্যাসূচক হতে হবে.

প্রশিক্ষণ এবং/অথবা বৈধতা ডেটা বিভিন্ন উত্স থেকে আসতে পারে। আর্থ ইঞ্জিনে ইন্টারেক্টিভভাবে প্রশিক্ষণের ডেটা সংগ্রহ করতে, আপনি জ্যামিতি অঙ্কন সরঞ্জামগুলি ব্যবহার করতে পারেন ( কোড এডিটর পৃষ্ঠার জ্যামিতি সরঞ্জাম বিভাগটি দেখুন)। বিকল্পভাবে, আপনি একটি আর্থ ইঞ্জিন টেবিল সম্পদ থেকে পূর্বনির্ধারিত প্রশিক্ষণ ডেটা আমদানি করতে পারেন (বিশদ বিবরণের জন্য আমদানি সারণী ডেটা পৃষ্ঠা দেখুন)। ee.Classifier এর একজন কনস্ট্রাক্টরের কাছ থেকে একটি ক্লাসিফায়ার পান। classifier.train() ব্যবহার করে ক্লাসিফায়ারকে প্রশিক্ষণ দিন। classify() ব্যবহার করে একটি Image বা FeatureCollection শ্রেণিবদ্ধ করুন। নিম্নলিখিত উদাহরণটি তিনটি সাধারণ শ্রেণির ভবিষ্যদ্বাণী করার জন্য একটি শ্রেণীবিভাগ এবং রিগ্রেশন ট্রিস (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');

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

# 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');

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

# 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 কাস্টম প্যারামিটারের একটি সেট দিয়ে নির্দিষ্ট করা হয়েছে। ভবিষ্যদ্বাণী সমস্যার শারীরিক প্রকৃতি সম্পর্কে একটি অগ্রিম তথ্য ছাড়া, সর্বোত্তম পরামিতিগুলি অজানা। Hsu et al দেখুন। (2003) একটি SVM-এর জন্য পরামিতি নির্বাচন করার জন্য একটি রুক্ষ গাইডের জন্য।

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

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

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

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

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

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

# 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 ডেটা ডাউনস্কেল করতে 10টি গাছ সহ একটি র্যান্ডম ফরেস্ট ( ব্রেইম্যান 2001 ) ক্লাসিফায়ার ব্যবহার করে। sample() পদ্ধতিটি MODIS ডেটা থেকে দুটি এলোমেলো নমুনা তৈরি করে: একটি প্রশিক্ষণের জন্য এবং একটি বৈধতার জন্য। প্রশিক্ষণের নমুনা ক্লাসিফায়ারকে প্রশিক্ষণ দিতে ব্যবহৃত হয়। আপনি classifier.confusionMatrix() থেকে প্রশিক্ষণের ডেটাতে পুনঃপ্রতিস্থাপন নির্ভুলতা পেতে পারেন। বৈধতা নির্ভুলতা পেতে, বৈধতা ডেটা শ্রেণীবদ্ধ করুন। এটি বৈধতা FeatureCollection সংগ্রহে একটি classification বৈশিষ্ট্য যুক্ত করে। বৈধতা (প্রত্যাশিত) নির্ভুলতার প্রতিনিধিত্বকারী একটি বিভ্রান্তি ম্যাট্রিক্স পেতে শ্রেণীবদ্ধ 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));

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

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

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

# 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 MB) হওয়ার কারণে বা প্রশিক্ষণ খুব বেশি সময় নেয় (5 মিনিট) হওয়ার কারণে প্রচুর পরিমাণে ইনপুট ডেটাতে একটি শ্রেণিবদ্ধকারীকে প্রশিক্ষণ দেওয়া ইন্টারেক্টিভভাবে সম্ভব নাও হতে পারে। 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
);

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

# 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');

পাইথন সেটআপ

পাইথন এপিআই এবং ইন্টারেক্টিভ ডেভেলপমেন্টের জন্য geemap ব্যবহার করার জন্য পাইথন এনভায়রনমেন্ট পৃষ্ঠাটি দেখুন।

import ee
import geemap.core as geemap

Colab (পাইথন)

# 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