Lab 12 - Unsupervised Classification

Fall 2024 Lab Lecture - 

Fall 2023 Lab Lecture - 

Spring 2023 Lab Lecture - 

In this lab, we will build off work from Lab 11 - Supervised classification.   You can use your Lab 11, or you are welcome to use this Code to start off with: https://code.earthengine.google.com/e5b89ac826e8e2ef8c08111e929b07e1. In Lab 11, we trained a supervised classification with a training sample and used it to create a landuse/landcover map in Northern Italy.  We will build off that lab to classify the same image with an unsupervised classification. At the end of this assignment, we can compare supervised versus unsupervised classification results.

Before we start, we should add a comment in the code to know where we started working on Unsupervised Classification. Write a comment code like this in the last line of your Lab 11 code.

//////////////// Unsupervised classification ////////////////

Then, save the code in a new file using the Save As menu.

adding in the comment for where unsupervised classification starts

A reminder of the differences between supervised and unsupervised classification. In supervised classification, a training data set is used to teach the classifier how to classify new data. The training set contains already classified data, and the classifier learns to associate certain features with certain classes. Once the classifier is trained, it can classify new data. In unsupervised classification, the classifier is not given any labeled data. Instead, it is given a set of unlabeled data and tries to find patterns. The classifier then groups the data points based on these patterns.

The unsupervised classification in Earth Engine has this workflow:

  1. Assemble features with numeric properties to find clusters (training data).
  2. Select and instantiate a clusterer.
  3. Train the clusterer with the training data.
  4. Apply the clusterer to the scene (classification).
  5. Label the clusters.

To generate training data, we will use the sample method, which randomly takes samples from a region (unlike sampleRegions, which takes samples from predefined locations). We will use the image’s footprint as the region by calling the geometry method. Additionally, we will define the number of pixels (numPixels) to sample—in this case, 1000 pixels—and define a tileScale of 8 to avoid computation errors due to the region's size.

Copy and paste the code below to sample 1000 pixels from the Landsat image. 

// Make the training dataset.
var training = landsat.sample({
    region: landsat.geometry(),
    scale: 30,
    numPixels: 1000,
    tileScale: 8
});

Now we can instantiate a clusterer and train it. As with the supervised algorithms, there are many unsupervised algorithms to choose from. We will use the k-means clustering algorithm, a commonly used approach in remote sensing. This algorithm uses an iterative regrouping strategy to identify groups of pixels near each other in the spectral space (image x bands). We define a number of clusters, k, and then the method randomly distributes that number of seed points into the spectral space. A large sample of pixels is then grouped into its closest seed, and the mean spectral value of this group is calculated. That mean value is akin to a center of mass of the points, known as the centroid. Each iteration recalculates the class means and reclassifies pixels with respect to the new means. This process is repeated until the centroids remain relatively stable, and only a few pixels change from class to class on subsequent iterations.

 K-means visual concept

Figure -  K-means visual concept 

Copy and paste the code below to request four clusters, the same number as for the supervised classification, in order to directly compare them.

// Instantiate the clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(4).train(training);

Now copy and paste the code below to apply the clusterer to the image and add the resulting classification to the Map. Note that we use a method called randomVisualizer to assign colors for the visualization. We are not associating the unsupervised classes with the color palette we defined earlier in the supervised classification. Instead, we assign random colors to the classes since we do not yet know which unsupervised classes best correspond to each named class (e.g., forest, herbaceous). Note that the colors in your map might not be the same as you see on the screenshots Map, since they are assigned randomly.

// Cluster the input using the trained clusterer.
var Kclassified = landsat.cluster(clusterer);

// Display the clusters with random colors.
Map.addLayer(Kclassified.randomVisualizer(), {},
    'K-means classified - random colors');

SCREENSHOT OF THE RESULT

Challenge 1: Determine which pixel values are which land covers.  Your image has four classes on the clustering band. Using the inspector tool, click the classes and document what cluster pixel value is which of the four landcover classes you have. Herbaceous, water, forest, and developed. Add a comment in your code documenting the pixel values and landcover classes.

Challenge 2: Using the Map.addLayer method, Display the clusters with the same palette as supervised classification. Hint: Map.addLayer(Kclassified, {HERE GOES YOUR Visualization Parameters}, 'K-means classified').  You want to map the palette color to pixel values for the Visualization Parameters.  Verify your pixel values start with 0 and end with 3, and then order them based on the answer from challenge 1 with the palette values from Lab 11 {min: 0, max: 3, palette: ['d0741e','1a11ff','589400', 'ff0000']}

Challenge 3: Inspect the results. How does this classification compare to the previous ones? Use the Inspector to check which classes were assigned to each pixel value (“cluster” band).  Add a short paragraph comment in your code explaining the results' differences. 

TURN IN A SNAPSHOT URL OF YOUR CODE

Lab Submission

Submit lab via email.

Subject: Lab 12 - Unsupervised Classification - [Your Name]