Advanced image classification using ILWISPy in conjuntion with SciKit

Notebook prepared by Ben Maathuis and Bas Retsios. ITC-University of Twente, Enschede. The Netherlands

This notebook is a continuation of the initial land cover classification of Sentinel-2 MSI satellite image as conducted in the notebook 'Intro_RS_ILWISPy.ipynb'. Ensure that the Sample data is locally available. It can be retrieved from: https://filetransfer.itc.nl/pub/52n/ilwis_py/sample_data/ILWISPy_ImageClassification.zip. Unzip the file.

Here it is assumed that the data folder '/ILWISPy_ImageClassification' is situated within the notebook folder! It is furthermore assumed that you have locally installed ILWIS386 for data visualization when data is written to disk and have gone throught the notebook 'Intro_RS_ILWISPy.ipynb'.

Additional image classifiers used within this notebook are:

Prerequisite: You have reviewed the notebook 'Intro_ILWISPy.ipynb' and 'Intro_RS_ILWISPy.ipynb', available at: https://filetransfer.itc.nl/pub/52n/ilwis_py/notebooks/. Note that the files with postfix _result.html files show the processed results if the notebook would be executed.

Import Modules

Read Data and get meta data information

Two datasets are at your disposal, an ilwis map list containing a sub set of Sentinel 2, as created in the notebook 'Intro_RS_ILWISPy.ipynb' and a map showing the samples which are used for the supervised classification routines used below.

The data sets have been visualized in the notebook 'Intro_RS_ILWISPy.ipynb' already, if you want to display the image and the sample map, use ILWIS 386. This step is omitted here and the data is directly loaded.

Convert the map list into Numpy array

Ground truth

For the area of interest a field survey was conducted at approximately the same time as the image was acquired. The following classes are used:

The field survey was used to prepare a map providing the training data required for supervised classification. This map is using the land cover labels as indicated above, note that the background area has been assigned to '0'

Also convert the sample data into Numpy array

Pairing Y with X

Now that we have the image we want to classify (our X feature inputs), and the Training Data with the land cover labels (our Y labeled data), we need to pair them in NumPy arrays so we may feed them to our classifier.

Reshape our training input data

Train Test Split

Now we split the sample data into a training and testing data set, 70 % is used for training and the remaining data for testing the classifier.

The Random Forest classifier

Training the Random Forest

Now that we have our X matrix of feature inputs (the spectral bands) and our y array (the labels), we can train our model.

Random Forest diagnostics: With our Random Forest model fit, we can check out the "Out-of-Bag" (OOB) prediction score:

To help us get an idea of which spectral bands were important, we can look at the feature importance scores:

Review the weights assigned for each of the spectral channels used, those with the largest weights look the most useful to use.

Let's look at a cross-tabulation to see the class confusion. To do so, we will use the Pandas library

It will be very doubtfull if the real confusion matrix will be having a 100% accuracy. What is likely going on is that we used a large number of trees within a machine learning algorithm to best figure out the pattern in our training data. Given enough information and effort, this algorithm precisely learned what we gave it. Asking to validate a machine learning algorithm on the training data is a useless exercise that will overinflate the accuracy.

Instead, we use the test data to get a better indication of the accuracy.

The meaning of precision, recall, F-measure and support for each class:

Classifying the image stack using the RF classifier

Predicting the rest of the image. Continue with the Random Forest classifier as trained above and proceed to classify the entire image . To reshape the image take the full image and reshape into long 2d array (nrow * ncol, nband) for classification

Scaling

Standardization is another scaling technique where the values are centered around the mean with a unit standard deviation. This means that the mean of the attribute becomes zero and the resultant distribution has a unit standard deviation. Scaling is done to Normalize data so that priority is not given to a particular feature. Role of Scaling is mostly important in algorithms that are distance based and require Euclidean Distance. Random Forest, as applied above, is a tree-based model and hence does not require feature scaling.

Reshape input satellite spectral channels

Scale the input satellite spectral channels

K-Nearest Neighbor Classifier (K-NNC)

k-Nearest Neighbor Classifier is one of the widely used classifiers in machine learning. The main objective of this method is that the data instances of the same class should be closer in the feature space. K used in KNN classifier represents the number of nearest neighbors to examine. It is supposed to be specified by the User. So, how can we choose right value for K?

In the cell below we calculate the accuracy of KNN for different Ks.

Plot model accuracy for different number of Neighbors

The cells below K-NNC instance with n_neighbors has to be specified (note k = 3 was providing the highest accuracy) and fits the train data, predicts the labels of the test data, shows the accuracy, and prints the classification report which includes precision, recall and F1-score of each class.

Accuracy classification score.

In multilabel classification, this function computes subset accuracy: the set of labels predicted for a sample must exactly match the corresponding set of labels in y_true.

Support Vector Machine (SVM)

The support vector machine (SVM) is a supervised learning method that generates input-output mapping functions from a set of labeled training data. The mapping function can be either a classification function, i.e., the category of the input data, or a regression function.

For classification, nonlinear kernel functions such as Radial Basis Function (RBF), Polynomial, Sigmoid, etc. are often used to transform input data to a high-dimensional feature space in which the input data become more separable compared to the original input space. Maximum-margin hyperplanes are then created. The model thus produced depends on only a subset of the training data near the class boundaries.

The below code is used to create an instance of SVM with the regularization parameter C as 3 and linear kernel. Fits the data, predict the labels for test data, and prints the accuracy and classification report.

Light Gradient Boosting Classifier (GBM)

Gradient boosting is a technique attracting attention for its prediction speed and accuracy, especially with large and complex data. Gradient boosting is a type of machine learning boosting. It relies on the intuition that the best possible next model, when combined with previous models, minimizes the overall prediction error. The key idea is to set the target outcomes for this next model to minimize the error.

Today we are going to use lightGBM which is a gradient boosting framework that makes use of tree-based learning algorithms. LightGBM is called “Light” because of its computation power and giving results faster. It takes less memory to run and can deal with large amounts of data. It is one of the most widely used algorithms in competition because the motive of the algorithm is to get good accuracy of results.

The below code is used to create an instance of the lightgbm with parameters such as learning rate, maximum depth, number of classes, etc.

Save the classfied images with into ILWIS format, examples below for the fr, knn, svm and gmb accordingly. First a new -empty Raster Coverage is created which is filled with the classified data arrays using the function 'array2raster()'

Write the individual classified results into output maps, check the initial data type and transform to same output format data type

Note that for the 'gbm_class' some further data type manipulation is required as it is in a dtype 'int64' and the other 3 layers are in dtype uint8

Evaluation of your results

Have the different classifiers applied using the same input multi spectral image as well as the same sample data set performed in a similar manner, or are the results obtained different? From the images displayed above it is hard to draw conclusions. So therefor some simple additional analysis is conducted.

Transform the stack of classified images into ILWIS

Eventually store the output map stack and display it using ILWIS 386, uncomment the line in the cell below

Conduct some further statistical analysis

If you want to store your results uncomment the lines in the cell below and open the maps in ILWIS 386

Final comment:

This notebook shows you how to interchange between various sitepackages and make use of the various processing routines offered. You have used four classification routines and lateron selected the median of the classification results obtained as well as standard deviation, showing the uncertainty / differences between the individual classified maps.