Skip to content

2a) Random Forest Classification

Scrommelinck edited this page Jul 16, 2019 · 45 revisions

Description:

A set of features is calculated per line capturing its geometry and its spatial context. Table 1 shows all features being calculated, of which the first two are not used for the classification. Together with a boundary label, the features are used to train a Random Forest (RF) classifier. The boundary label refers to a line being a boundary (1) or not boundary (0). This label is assigned manually or derived from reference data. The trained classifier predicts boundary likelihoods for unseen testing data without a boundary label (Figure 1). The classification is based on the open-source Scikit-learn RF implementation .

Table 1: Features calculated per line used by the Random Forest (RF) classifier for boundary classification.

Figure 1: Boundary line classification based on Random Forest (RF) to derive boundary likelihoods for MCG lines.

Input:

  • image segmentation lines without attributes (.shp)
  • cadastral reference lines (.shp) (optional)
  • RGB orthoimage raster (.tif)
  • DSM orthoimage raster (.tif) (optional)

Output:

  • image segmentation lines with boundary likelihood as attribute (.shp)

Steps:

  1. Label lines
  2. Calculate attributes
  3. Train classifier
  4. Predict boundary likelihoods


1. Label lines

Goal:

To assign a value of 0 or 1 for each line as boundary attribute in the training data.

  • boundary = 1 : line is useful for boundary delineation
  • boundary = 0 : lines is not useful for boundary delineation

The boundary value can be assigned either

  • manually by selecting lines and setting their value accordingly or
  • semi-automatically by comparing the overlap of the image segmentation lines with cadastral reference lines

Input:

  • image segmentation lines without boundary label (.shp)
  • cadastral reference lines (.shp) (optional)

Output:

  • image segmentation lines with boundary label (.shp)

Steps:

Manual line labelling

  • Create the boundary attribute for your input lines. Open Attribute Table | New Field

  • Select lines to be labelled with boundary= 1 by clicking on features while pressing Ctrl

  • Open Attribute Table | Toggle editing mode | set boundary = 1 for all selected features with Update Selected

  • Save edits | Toggle editing mode


Semi-automatic line labelling (part 1/2)
Note: This requires cadastral reference data. An additional attribute (bound_ref) is calculated that captures the overlap between each line and the buffered cadastral reference.

  • Buffer the cadastral reference by a radius distance of e.g. 0.4m

  • Convert the cadastral reference data to a raster format. The raster should have the value 1 at locations of the cadastral reference buffer. Make sure that the resolution (pixel size) of your output raster is smaller than the buffer size you have chosen.

  • Save the raster to \share\input

  • Set "Ref_RasterFile" in config\config.json to the name of your raster file

  • Continue with 2. Calculate attributes and label lines thereafter



2. Calculate attributes

Goal:

To calculate features required to train the classifier and to save them as attributes for the image segmentation lines.

Input:

  • image segmentation lines without attributes (.shp)
  • RGB orthoimage raster (.tif)
  • DSM orthoimage raster (.tif) (optional)

Output:

  • image segmentation lines with attributes (.shp)

Steps:

  • Download all folders from GitHub

  • Save your input data to \share\input

  • Set the following parameters in config\config.json according to your input data file names :

    • "SegmentShapeFile": name of your image segmentation lines without attributes (.shp)
    • "RGB_RasterFile": name of your RGB orthoimage raster (.tif)
    • "DSM_RasterFile": name of your DSM orthoimage raster (.tif) (optional)

  • Open Command Prompt

    • Select the Windows start button > type cmd

  • Change the command promt's working directory to \scripts

    • copy the path to the \scripts folder
    • cd path_to_folder\scripts

  • Start main.py with Python

    • python main.py

  • Start test modus

    • --test

  • Calculate attributes

    • -a

If you now open the input shapefile (.shp) you saved to \share\input in a GIS and check out the attribute table. Each line should have new attributes.

Attribute description:

  • ID - dsm_grad: see Table 1
  • cad_sum: sum of cadastral reference pixels being within buffer around line
  • cad_count: sum of pixels being within buffer around line
  • bound_ref: value between 0 and 1 showing overlap between line's buffer and cadastral reference buffer (cad_sum divided by bound_ref):
    • bound_ref = 0 : no overlap between line and cadastral reference buffer
    • bound_ref = 1 : 100% overlap between line and cadastral reference buffer

Semi-automatic line labelling (part 2/2):

  • Set the boundary value for all lines having bound_ref > 0.5 to 1
    Open Attribute Table | Toggle editing mode | Select features using an expression| "bound_ref">0.5 | Select Features

  • Set boundary = 1 for all selected features with Update Selected

  • Invert selection and Set boundary = 0 for all selected features with Update Selected

  • Save edits | Toggle editing mode



3. Train classifier

Goal:

To create a classifier model based on training data to be applied later on testing data.

Input:

  • image segmentation lines with attributes and boundary label (.shp)

Output:

  • trained classifier model (.pkl)

Steps:

  • Set "TrainingLayer" in config\config.json to the name of your training lines

  • Create classifier

    • -c

The classifier model classifierModel.pkl is now saved in \share\output



4. Predict boundary likelihoods

Goal:

To apply trained classifier on testing data in order to predict boundary likelihoods.

Input:

  • image segmentation lines with attributes and without boundary label (.shp)

Output:

  • image segmentation lines with attributes and with predicted boundary label (.shp)

Steps:

  • Copy classifierModel.pkl from \share\output to \share\input

  • Set "ValidationLayer" in config\config.json to the name of your testing lines

  • Predict boundary likelihoods

    • -p

The boundary attribute in your training data is now updated with a boundary value between 0 and 1.



General notes:

  • A log file containing information about the data processing is saved as processing.txt in \share\temp
  • If not further specified, the term lines refers to the input segmentation lines
  • The created classifier can be reused and applied to more testing data. In that case, steps 1 and 3 are not needed.


References:

  • The GIS used for this wiki is the open source QGIS.