Hi and welcome back! By now we have seen how to apply a classification to an image, and what your options are when performing a supervised classification. The one thing we haven’t discussed yet is how to do unsupervised classification.
There can be cases where you do not have a large list of training data and we want the algorithm to figure out different land cover classes on its own. The way unsupervised classification works, is by looking at a lot of pixels, and determining which pixels are most similar, and which are most different. By doing this, pixels can be clustered in similarly looking groups of pixels and be classified in that way.
Also here, for performing this process, many algorithms exist that approach the problem in different ways. This unsupervised classification is also often called clustering instead of unsupervised classification I will refer to it as clustering from now on since that is the name the Google Earth Engine gives it as well. In the Google Earth Engine, a supervised classification is stored in a Classifier object.
This is similar for a clustering algorithm, which is stored in a Clusterer object. so the way they are trained and implemented is very similar. For supervised classification it was necessary to specify training data of the different classes you wanted to detect.
And when performing the clustering algorithm, all steps are the same and you also need to create training data, but this data does not have to contain an attribute about which class it is I will start from the same setup I also used when introducing supervised classification. And I will do the same steps. So first I select a region of interest and fetch an image to classify.
This clustering, just as a classification, could also be done on an entire image collection, but for simplicity and speed I will just use one image. When we did the supervised classification, at this point, we started to collect data about the different classes. Here we won’t do it manually, but we could use a sample function to give us 100 random points in the area.
This sample function returns a FeatureCollection with 100 points containing the spectral values of the image they are located on. Ideally you should have a lot more training points, but I will take 100 to speed things up. Then the next step is to create a clusterer object from one of the different algorithms available.
I will just pick one and go over the different algorithms later in the video. This clusterer object we have now still needs to be trained. This is done by applying the train() function on the clusterer and passing in the training data This cluster will now grouped training points that are most similar, while these groups are as distinct as possible from other groups of points.
When we apply this clusterer to our image, it will take all pixels of the image and determine to which of these groups the pixel is most similar. And when we do this, we get a result like so: Based on those 100 random samples, the clustering algorithm was able to identify 5 clusters on its own. We can then interpret these and determine what the clusters represent.
If however, you want finer control about how many clusters there are and how it is processed we have to take a look at the different clustering algorithms and their attributes. When looking at the docs for clustering, you can see 5 different algorithms. The Earth Engine uses WEKA to perform its clustering and that’s why you see it in every algorithm name.
WEKA is a free collection of tools, developed in New Zealand, specifically used for data analysis and data mining. The first algorithm, and in my opinion the easiest to understand, is the K-means algorithm. I will show this with an example.
Let’s say we have an image with two bands, and the pixels of this image are distributed like this. What K-means will do is randomly put k points on this distribution and calls them your cluster centers. It then looks at all pixels to see which pixels would classify as each class.
These are by far the correct values, but then it recalculates the cluster center and do the whole process again. After a certain amount of iterations, the clusters will no longer change and the K-means algorithm reaches convergence. The class values it obtained then are a best estimation of the different clusters in the image.
An important aspect about this algorithm is that you have to know how many clusters you want to identify. And while it is easy to understand, it becomes very computationally expensive when you have a large dataset, which is kind of the thing with the Google Earth Engine. Luckily the people at google thought about this, and there is a technique to improve the performance of this algorithm.
This technique is called canopy clustering. This technique creates canopies based on distance metrics. A canopy is basically a group, and when calculating distances, it will only take into account other points in the same canopy.
There is a value that determines how far canopy centers must be from each other and there is a distance metric that determines which points are included in that canopy. These are called the tight distance and the loose distance respectively. Since the loose distance is by definition larger than the tight distance, a point can be included in multiple canopies.
When the K-means algorithm is running, it will only compare the distances of points within the same canopy and ignore everything else. When there are a lot of points, this significantly improves performance of the algorithm. When you look at these canopies in the graph, it indeed looks like the canopy of a forest.
I don’t know the exact math behind this technique, but I will put a link in the description to the video that helped me understand the concept. Now that we understand the workings of this algorithm, we can look at the attributes we can add to the function in the Google Earth Engine. The fist attribute is the number of clusters that K-means should create.
Next we have an init value that determines how these clusters are initialized. By default the initial locations of the clusters are chosen randomly. The k-means++ option chooses the location based on the distribution of the points and often reduces the amount of iterations needed to reach convergence.
You can also choose to start with the canopy centers as cluster centers or choose the points that are furthest from each other to use as initial cluster centers. The next two attributes are whether canopies should be used and how many there can maximally be retained in memory. Periodic Pruning will determine how often small canopies should be removed and the Min Density determines what is considered a small canopy.
Next, we have the values t1 and t2 which are the tight and loose distances mentioned earlier. All of these attributes might look intimidating but are only important when you choose to enable canopies. The distance function will determine which distance metric is used when Kmeans calculates distances between points.
You can choose between Euclidean and Manhattan distances. The maxIterations attribute determines an upper limit of how many iterations the algorithm should run. If it did not yet find convergence, it will stop after this number of iterations.
I have no idea what preserveOrder does, so let me know if you have any idea. Then the fast option also improves performance by reducing the accuracy of the numbers it works with, but this disables the option to get error metrices as described in the docs. And finally we have a seed attribute, and this attribute is used to reproduce the algorithm even though it relies on random values.
When using the same seed, the random values will stay the same when you run the algorithm again. Okey, this was a long explanation, but two of the other algorithms have the same foundation as the K-means. The Cascade K-means algorithm for example is the same as the K-means algorithm, but it iterates over different amounts of clusters and determines which cluster count has the least dispersion within a cluster, while having most dispersion between clusters.
So it automatically finds the best number of clusters. This is calculated using a special criterion that you can see in the docs, which is also called the Variance Ratio Criterion. The arguments are therefor quite self explanatory, you can choose the minimum and maximum number of clusters it should condsider, how often the algorithm should restart, what the starting position of the cluster centers should be and so on.
Then the X-means algorithm is again the same as the K-means algorithm and also implements a technique to automatically find a more ideal number of clusters. It does this by splitting the most promising clusters and see if clustering with these splitted clusters is better compared to clustering when the clusters were not split. Therefor in de docs, we can again choose the minimum number of clusters, this will be the initial number of clusters it will start with, and the maximum number of clusters, it will no longer split clusters after this amount is reached.
Next you can choose the maximum amount of iterations for the entire algorithm, both for when it compares the K-means clustering without splits and for the K-means clustering performed on the split clusters. A K-D Tree is not a very complicated concept, but I cannot find the effect of this attribute on the X-means algorithm. I assume it improves performance at the cost of precision.
But I am not sure so I will put a link to a good resource in the description and leave it like that. Also the cutoff Factor, I played a lot with it, but I could not make it have any effect on the clustering output. And at last we have a seed to reproduce the result.
Next we have two more algorithms that are not directly related to K-means. Let’s start with the Cobweb algorithm. The cobweb algorithm organizes the different training points into a classification tree where each leaf of the tree, or node, is a class.
When this tree is constructed, it can be used to classify all other pixels in the image to determine to which node, or class, it corresponds the most. There are two properties you can enter that alter the way this classification tree is created. When you increase acuity, nodes are less likely to split, and the result is a tree with less nodes and thus less classes.
The cutoff value determines the minimal category utility, or category goodness. Increasing this value will require that the split of a node contributes more improvement before it accepts the split. Increasing this value also results in fewer split branches and thus less classes.
The final algorithm that the Google Earth Engine offers is the Learning Vector Quantization algorithm. This is a neural network based approach, but it does have a similar strategy as the K-means algorithm. Each iteration it loops over all training points and it pulls the closest class center to its location.
The amount by which it is pulled depends on the learning rate. And when looking at the possible arguments we again see the number of clusters we want and the learning rate that I just mentioned. It is possible to determine the number of training epochs; this is simply the name for iterations in a machine learning context and determines how many times it should perform this loop of pulling class centers.
And then you can choose to normalize your input. If you have a band that ranges from 0 to 1, and another band that ranges from 0 to 100 for example, you should normalize these values so that the algorithm can compare them. I assume that since bands from satellite images mostly have the same range, this is disabled by default.
So that was quickly how all clustering algorithms work in the Google Earth Engine. I hope it is now a bit more clear why there are these different algorithms. Have fun and try out the different options to find out which one suits best for your application.