Commit ce09c4d8 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

RSImage_Predictor.predict now applies predition in tiles to save memory.

parent 99b26062
......@@ -659,8 +659,8 @@ class TrainingData(object):
def im2spectra(geoArr):
# type: (Union[GeoArray, np.ndarray]) -> np.ndarray
"""Convert images to array of spectra samples (rows: samples; cols: spectral information)."""
return geoArr.reshape((geoArr.rows * geoArr.cols, geoArr.bands))
"""Convert 3D images to array of spectra samples (rows: samples; cols: spectral information)."""
return geoArr.reshape((geoArr.shape[0] * geoArr.shape[1], geoArr.shape[2]))
def spectra2im(spectra, tgt_rows, tgt_cols):
......@@ -1506,19 +1506,26 @@ class RSImage_Predictor(object):
image = image if isinstance(image, GeoArray) else GeoArray(image, nodata=nodataVal)
image.nodata = image.nodata if image.nodata is not None else nodataVal
#
spectra = im2spectra(image)
# adjust classifier
if CPUs is None or CPUs > 1:
# FIXME does not work -> parallelize with https://github.com/ajtulloch/sklearn-compiledtrees?
classifier.n_jobs = cpu_count() if CPUs is None else CPUs
# predict!
spectra_predicted = classifier.predict(spectra).astype(image.dtype)
# apply prediction
# NOTE: prediction is applied in 1000 x 1000 tiles to save memory (because classifier.predict returns float32)
image_predicted = GeoArray(np.empty((image.rows, image.cols, classifier.tgt_n_bands), dtype=image.dtype))
for ((rS, rE), (cS, cE)), im_tile in image.tiles(tilesize=(1000, 1000)):
# 3D -> 2D
spectra = im2spectra(im_tile)
# predict!
spectra_pred = classifier.predict(spectra).astype(image.dtype)
# 2D -> 3D
tiledata_pred = spectra2im(spectra_pred, tgt_rows=im_tile.shape[0], tgt_cols=im_tile.shape[1])
# 2D -> 3D
image_predicted = GeoArray(spectra2im(spectra_predicted, tgt_rows=image.shape[0], tgt_cols=image.shape[1]))
image_predicted[rS:rE + 1, cS:cE + 1] = tiledata_pred
# re-apply nodata values to predicted result
if image.nodata is not None:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment