Neural Network MLPClassifier API

Source code: https://bitbucket.org/kul-reseco/lnns/src.

For issues, bugs, proposals or remarks, visit the issue tracker.

Algorithms

class lnns.core.network.Network(number_of_hidden, activation)[source]

Bases: object

The Network class create a neural network using the sklearn.neural_network.MLPClassifier. The network can be used to predict classified images using supervised classification.

predict_image(band_data, probability_of_class=None)[source]

Use the trained network, it is possible to classify the complete image using the different bands of the image. If probability_of_class is given, than the image will show the probability that this class will occur.

Parameters:
  • band_data (np.array) – array with all bands
  • probability_of_class (int) – class for which you would like the probability image
Returns:

np.array classified_image: the classified image

train_image(band_data, classes_data, max_iter, no_data_value=0, test_size=0.33, log_function=<built-in function print>)[source]

The given network can be trained given an image of different band waves (band_data) and a respectively data set indicating a subset of different classes of the image (class_data).

Parameters:
  • band_data (np.array) – array with all bands
  • classes_data (np.array) – an overlap image indicating different classes
  • max_iter (int) – the number of iterations when training the neural network
  • no_data_value (int) – value that describes pixels with no data in the classes_data file
  • test_size (float) – the proportion of the test_size that will be used to evaluate the trained network
  • log_function – function to log
validate(x_test, y_test, log_function=<built-in function print>)[source]

The trained neural network can be evaluated using a test set of the data. The validation results will be saved in the validation_results dictionary with the keys: Average accuracy, a Kappa for each predicted class, an average kappa and a rapport including precision, recall, f1-score and support.

Example output:

Average accuracy: 0.9740708729472775
Kappa class 1: 0.9996048676750681
Kappa class 2: 0.9969686283161031
Kappa class 3: 0.9269671354418852
Kappa class 4: 0.9384942730689072
Kappa class 5: 0.9854574554108237
Kappa class 6: 0.8991142021469177
Average Kappa: 0.9577677603432843
. precision recall f1-score support
0 1.00 1.00 1.00 1664
1 1.00 1.00 1.00 956
2 0.91 0.96 0.93 597
3 0.96 0.94 0.95 1149
4 1.00 0.98 0.99 2345
5 0.88 0.92 0.90 231
avg / total 0.98 0.98 0.98 6942
Parameters:
  • x_test (np.array[float]) – test input variables
  • y_test (np.array[float]) – test output values
  • log_function – function to log

Interfaces

———————————————————————————————————————-
Date : August 2018
Copyright : © 2018 - 2020 by Tinne Cahy (Geo Solutions) and Ann Crabbé (KU Leuven)
Acknowledgements : Translated from LNNS 1.0 A neural network simulator [C++ software]
Ghent University, Laboratory of Forest Management and Spatial Information Techniques
Lieven P.C. Verbeke

This file is part of the QGIS Neural Network MLP Classifier plugin and mlp-image-classifier python package.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License (COPYING.txt). If not see www.gnu.org/licenses.
———————————————————————————————————————-
lnns.interfaces.plot(x, y, size=None, title='Neural Network MLPClassifier: Network error decline', testing=False)[source]
lnns.interfaces.plot_to_file(x, y, path, title='Neural Network MLPClassifier: Network error decline')[source]
lnns.interfaces.run_algorithm_images(image_paths, classified_path, no_data_value, hidden_layer_size, activation, iterations, test_size, probability_of_class, update_process_bar=<built-in function print>, log_function=<built-in function print>, output_path=None, plot_path=None, feedback=None, context=None)[source]

Process all input, in order to run the nn script.

Parameters:
  • image_paths – the absolute path to the input raster files
  • classified_path – absolute path to the classified raster file
  • no_data_value – value that describes pixels with no data in the classes_data file
  • hidden_layer_size – Hidden layer size with length = n_layers - 2, comma separated values.
  • activation – Activation function for the MLPClassifier, choices=[‘identity’, ‘logistic’, ‘tanh’, ‘relu’]
  • iterations – Maximum number of iterations
  • test_size – the proportion of the test_size that will be used to evaluate the trained network
  • probability_of_class – class for which you would like the probability image
  • update_process_bar – function to update the progress bar
  • log_function – function to log
  • output_path – path for output files (optional)
  • plot_path – necessary for the processing tool: path for the plot
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
Returns:

output path

lnns.interfaces.run_algorithm_pattern(pattern_train, pattern_predict, no_data_value, hidden_layer_size, activation, iterations, test_size, probability_of_class, log_function=<built-in function print>, output_path=None, plot_path=None, feedback=None, context=None)[source]

Process all input, in order to run the nn script.

Parameters:
  • pattern_train – the absolute path to the pattern file with training data
  • pattern_predict – absolute path to the pattern file for predictions
  • no_data_value – value that describes pixels with no data in the classes_data file
  • hidden_layer_size – Hidden layer size with length = n_layers - 2, comma separated values.
  • activation – Activation function for the MLPClassifier, choices=[‘identity’, ‘logistic’, ‘tanh’, ‘relu’]
  • iterations – Maximum number of iterations
  • test_size – the proportion of the test_size that will be used to evaluate the trained network
  • probability_of_class – class for which you would like the probability image
  • log_function – function to log
  • output_path – path for output files (optional)
  • plot_path – necessary for the processing tool: path for the plot
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
Returns:

output path

lnns.interfaces.tuple_int(s)[source]
lnns.interfaces.tuple_int_cli(s)[source]
lnns.interfaces.tuple_string(s)[source]
class lnns.interfaces.imports.PatternFile(number_of_patterns, input_neurons, output_neurons, x, y)[source]

Bases: object

lnns.interfaces.imports.check_path(path)[source]

Check if path exists. Skipp path which are in memory

Parameters:path – the absolute path to the input file
lnns.interfaces.imports.import_image(path, reflectance=False)[source]

Browse for an image.

Parameters:
  • path – the absolute path to the image
  • reflectance – return reflectance values instead of DN between 0 and 254
Returns:

float32 numpy array [#good bands x #rows x #columns]

lnns.interfaces.imports.read_pattern(path_prn)[source]
lnns.interfaces.exports.write_classified_image(file_path, image, geo_transform=None, projection=None, gdal_type=<Mock name='mock.gdal.GDT_Int16' id='140574537438160'>)[source]
lnns.interfaces.exports.write_pattern(output_file, number_of_patterns, input_neurons, output_neurons, array, fmt)[source]

CLI: mlpclassifier-image

The Network class create a neural network using the sklearn.neural_network.MLPClassifier. The network can be used to predict classified images using supervised classification.

usage: mlpclassifier-image [-h] [-n NO_DATA_VALUE] [-l HIDDEN_LAYER_SIZE]
                           [-a {identity,logistic,tanh,relu}] [-i ITERATIONS]
                           [-t TEST_SIZE] [-p PROBABILITY_OF_CLASS]
                           [-o OUTPUT] [-g OUTPUT_GRAPH]
                           images_folder image_names classes_data_name

Positional Arguments

images_folder Path to the input image data and the classes data.
image_names Name(s) of different image bands.
classes_data_name
 Name of an overlap image indicating different classes

Named Arguments

-n, --no_data_value
 

Value that describes pixels with no data in the classes_data file (default: -1).

Default: -1

-l, --hidden_layer_size
 

Hidden layer size with length = n_layers - 2, comma separated values. The ith element represents the number of neurons in the ith hidden layer. (default: 10,)

Default: (10,)

-a, --activation
 

Possible choices: identity, logistic, tanh, relu

Activation function for the MLPClassifier (default: logistic).

Default: “logistic”

-i, --iterations
 

Maximum number of iterations (default: 200).

Default: 200

-t, --test_size
 

Portion of test pixels used to evaluate the trained network (default: 0.33).

Default: 0.33

-p, --probability_of_class
 Class for which you would like the probability image (default: None).
-o, --output Output predicted file (default: in same folder with name ‘output_classified.tif’
-g, --output_graph
 Output error graph (default: in same folder with name ‘output_error.PNG’

CLI: mlpclassifier-pattern

The Network class create a neural network using the sklearn.neural_network.MLPClassifier. The network can be used to predict classified images using supervised classification.

usage: mlpclassifier-pattern [-h] [-n NO_DATA_VALUE] [-l HIDDEN_LAYER_SIZE]
                             [-a {identity,logistic,tanh,relu}]
                             [-i ITERATIONS] [-t TEST_SIZE]
                             [-p PROBABILITY_OF_CLASS] [-o OUTPUT]
                             [-g OUTPUT_GRAPH]
                             pattern_predict_path pattern_train_path

Positional Arguments

pattern_predict_path
 Pattern text file that includes the values to predict,’number_of_patterns’, ‘number_of_inputs’ and ‘number_of_outputs.’
pattern_train_path
 Pattern text file that includes the network training values, ‘number_of_patterns’, ‘number_of_inputs’ and ‘number_of_outputs.’

Named Arguments

-n, --no_data_value
 

Value that describes pixels with no data in the classes_data file (default: -1).

Default: -1

-l, --hidden_layer_size
 

Hidden layer size with length = n_layers - 2, comma separated values. The ith element represents the number of neurons in the ith hidden layer. (default: 10,)

Default: (10,)

-a, --activation
 

Possible choices: identity, logistic, tanh, relu

Activation function for the MLPClassifier (default: logistic).

Default: “logistic”

-i, --iterations
 

Maximum number of iterations (default: 200).

Default: 200

-t, --test_size
 

Portion of test pixels used to evaluate the trained network (default: 0.33).

Default: 0.33

-p, --probability_of_class
 class for which you would like the probability image (default: None).
-o, --output Output predicted file (default: in same folder with extension ‘_predict.prn’
-g, --output_graph
 Output error graph (default: in same folder with extension ‘_error.PNG’