Classifier
पैकेज, Earth Engine में चल रहे पारंपरिक
मशीन लर्निंग एल्गोरिदम की मदद से, डेटा को कैटगरी में बांटने की सुविधा देता है. इन क्लासिफ़ायर में CART, RandomForest,
NaiveBayes, और SVM शामिल हैं. कैटगरी तय करने का सामान्य वर्कफ़्लो यह है:
- ट्रेनिंग डेटा इकट्ठा करना. ऐसी सुविधाएं इकट्ठा करें जिनमें ऐसी प्रॉपर्टी हो जो पहले से तय की गई क्लास का लेबल और प्रीडिक्टर के लिए संख्या वाली वैल्यू सेव करती हो.
- क्लासिफ़ायर को इंस्टैंशिएट करें. अगर ज़रूरी हो, तो इसके पैरामीटर सेट करें.
- ट्रेनिंग डेटा का इस्तेमाल करके, क्लासिफ़ायर को ट्रेन करें.
- किसी इमेज या सुविधा के कलेक्शन को अलग-अलग कैटगरी में बांटना.
- पुष्टि करने के लिए स्वतंत्र डेटा का इस्तेमाल करके, कैटगरी तय करने में हुई गड़बड़ी का अनुमान लगाना.
ट्रेनिंग डेटा एक 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');
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()
का इस्तेमाल करके, अपने क्लास लेबल को 0 से शुरू होने वाले लगातार पूर्णांक में बदलें). टेबल में अनुमान लगाने वाले एलिमेंट डालने और ट्रेनिंग डेटासेट बनाने के लिए, 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');
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). ध्यान दें कि एसवीएम को कस्टम पैरामीटर के सेट के साथ तय किया जाता है. अनुमान लगाने से जुड़ी समस्या की प्रकृति के बारे में पहले से जानकारी के बिना, सबसे सही पैरामीटर का पता नहीं चलता. एसवीएम के लिए पैरामीटर चुनने के बारे में जानने के लिए, Hsu et al. (2003) देखें.
डेटा की कैटगरी तय करने वाले मॉडल के आउटपुट मोड
ee.Classifier.setOutputMode()
तरीका, सुपरवाइज़्ड
क्लासिफ़िकेशन के नतीजों के फ़ॉर्मैट को कंट्रोल करता है. इससे आउटपुट को कई अलग-अलग तरीकों से स्ट्रक्चर किया जा सकता है:
- CLASSIFICATION (डिफ़ॉल्ट): आउटपुट, क्लास नंबर होता है.
- REGRESSION: आउटपुट, स्टैंडर्ड रिग्रेशन का नतीजा होता है.
- PROBABILITY: इस आउटपुट से पता चलता है कि कैटगरी सही है या नहीं.
- MULTIPROBABILITY: आउटपुट, संभावनाओं का एक ऐरे होता है. इसमें हर क्लास के सही होने की संभावना को, देखी गई क्लास के हिसाब से क्रम में लगाया जाता है.
- RAW: आउटपुट, कैटगरी तय करने की प्रोसेस के इंटरनल रेप्रज़ेंटेशन का ऐरे होता है. उदाहरण के लिए, एक से ज़्यादा फ़ैसले लेने वाले ट्री मॉडल में रॉ वोट.
- RAW_REGRESSION: आउटपुट, रिग्रेशन प्रोसेस के इंटरनल रेप्रज़ेंटेशन का अरे होता है. उदाहरण के लिए, कई रेग्रेसन ट्री के रॉ अनुमान.
इन आउटपुट मोड के लिए, अलग-अलग डिवाइसों पर अलग-अलग सुविधाएं उपलब्ध होती हैं. इस टेबल में, हर क्लासिफ़ायर के लिए काम करने वाले मोड की खास जानकारी दी गई है.
डेटा की कैटगरी तय करने वाला | कैटगरी | रिग्रेशन | PROBABILITY | 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()
का इस्तेमाल करें.
उदाहरण के लिए, पिछले कोड ब्लॉक में SVM क्लासिफ़ायर को कॉन्फ़िगर करके, डिफ़ॉल्ट क्लासिफ़िकेशन लेबल के बजाय,
संभावना का नतीजा दिखाया जा सकता है:
कोड एडिटर (JavaScript)
var classifier = ee.Classifier.libsvm({ kernelType: 'RBF', gamma: 0.5, cost: 10 }).setOutputMode('PROBABILITY'); var trained = classifier.train(training, 'class', bands);
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()
का इस्तेमाल किया गया है. साथ ही, ट्रेनिंग और पुष्टि की सटीक जानकारी देने वाले कंफ़्यूज़न मैट्रिक की तुलना की गई है:
कोड एडिटर (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');
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
इस उदाहरण में, 10 ट्री वाले रैंडम फ़ॉरेस्ट (Breiman 2001) क्लासिफ़ायर का इस्तेमाल करके, एमओडीआईएस डेटा को लैंडसैट रिज़ॉल्यूशन में डाउनस्केल किया गया है. 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));
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());
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
को sample()
में 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 );
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');
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