eolearn.ml_tools.classifier

Module for classification helper classes and classification task.

class eolearn.ml_tools.classifier.ImageBaseClassifier(classifier, receptive_field)[source]

Bases: abc.ABC

Abstract class for image classifiers

Image Classifier extends the receptive field of trained classifier with smaller receptive field over entire image. The classifier’s receptive field is usually small, i.e.:

  • pixel based classifier has receptive field (1,1)

  • patch based classifier has receptive field (num_pixels_y, num_pixels_x)

Image Classifier divides the image into non-overlapping pieces of same size as trained classifier’s receptive field and runs classifier over them thus producing a classification mask of the same size as image.

The classifier can be of any type as long as it has the following two methods implemented:

  • predict(X)

  • predict_proba(X)

This is true for all classifiers that follow scikit-learn’s API. The APIs of scikit-learn’s objects is described at: http://scikit-learn.org/stable/developers/contributing.html#apis-of-scikit-learn-objects.

Parameters
  • classifier (object) – The actual trained classifier that will be executed over entire image

  • receptive_field (tuple, (n_rows, n_columns)) – Sensitive area of the classifier ((1,1) for pixel based or (n,m) for patch base)

abstract image_predict(X)[source]

Predicts class label for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted labels of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x)

Return type

np.ndarray

abstract image_predict_proba(X)[source]

Predicts class probabilities for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted probabilities of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x, n_classes)

Return type

np.ndarray

class eolearn.ml_tools.classifier.ImagePixelClassifier(classifier)[source]

Bases: eolearn.ml_tools.classifier.ImageBaseClassifier

Performs a per-pixel classification

It divides the image into individual pixels, runs classifier and collects the result in the shape of the input image.

Parameters

classifier (object) – The actual trained classifier that will be executed over entire image

image_predict(X)[source]

Predicts class label for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted labels of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x)

Return type

np.ndarray

image_predict_proba(X)[source]

Predicts class probabilities for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted probabilities of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x, n_classes)

Return type

np.ndarray

class eolearn.ml_tools.classifier.ImagePatchClassifier(classifier, receptive_field)[source]

Bases: eolearn.ml_tools.classifier.ImageBaseClassifier

Performs a per-patch classification

It divides the image into non-overlapping patches of same size as trained classifier’s receptieve field and runs classifier over them thus producing a classification mask of the same size as image.

Parameters
  • classifier (object) – The actual trained classifier that will be executed over entire image

  • receptive_field (tuple, (n_rows, n_columns)) – Sensitive area of the classifier ((1,1) for pixel based or (n,m) for patch base)

image_predict(X)[source]

Predicts class label for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted labels of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x)

Return type

np.ndarray

image_predict_proba(X)[source]

Predicts class probabilities for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted probabilities of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x, n_classes)

Return type

np.ndarray

class eolearn.ml_tools.classifier.ImagePixel2PatchClassifier(classifier, patch_size, mode='mean_prob', target=None, target_threshold=None)[source]

Bases: eolearn.ml_tools.classifier.ImageBaseClassifier

Pixel to patch classifier first performs classification on pixel level and then combines the results in user defined patches. In case of combining probabilities the weighted sum is taken over all pixels in a patch. In case of predictions the user defines what fraction of pixels within the patch has to belong to signal class ot be considered as signal.

Parameters
  • classifier (object) – The actual trained classifier that will be executed over entire image

  • patch_size ((int, int)) – A tuple defining (n_rows, n_columns)

  • mode (str) – The way predictions are obtained from prediction probabilities

  • target – Target class value. Set the patch class to this target class if its fractional representation within this patch is above the target_threshols

  • target – int or None

  • target_threshold (float or None) – A target prediction threshold

image_predict(X)[source]

Predicts class label for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted labels of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x)

Return type

np.ndarray

image_predict_proba(X)[source]

Predicts class probabilities for the entire image.

Parameters

X (np.ndarray) – Data for prediction of shape (n_samples, n_pixels_y, n_pixels_x, n_bands)

Returns

Predicted probabilities of shape (n_samples,) or (n_samples, n_pixels_y, n_pixels_x, n_classes)

Return type

np.ndarray

class eolearn.ml_tools.classifier.ImageClassificationMaskTask(*args, **kwargs)[source]

Bases: eolearn.core.eotask.EOTask

This task applies pixel-based uni-temporal classifier to each image in the patch and appends to each image the classification mask.

Run a classification task on a EOPatch feature

Classifier is an instance of the ImageBaseClassifier that maps [w, h, d] numpy arrays (d-channel images) into [w, h, 1] numpy arrays (classification masks).

Parameters
  • input_feature ((FeatureType, str)) – Feature which will be classified

  • output_feature ((FeatureType, str)) – Feature where classification results will be saved

  • classifier (ImageBaseClassifier) – A classifier that works over [n, w, h, d]-dimensional numpy arrays.

execute(eopatch)[source]

Transforms [n, w, h, d] eopatch into a [n, w, h, 1] eopatch, adding it the classification mask.

Parameters

eopatch (EOPatch) – An input EOPatch

Returns

Outputs EOPatch with n classification masks appended to out_feature_type with out_feature_name key

Return type

EOPatch