geo-deep-learning
geo-deep-learning copied to clipboard
Clean and faster RadiometricTrim
I write this code to trim the img, because I forgot about the RadiometricTrim
. This method is faster and take less line of code, if you think that can substitute the actual RadiometricTrim
I can create a pull request, otherwise we can delete this issues. However, I don't like the hard coding part of the random.uniform
if we keep the current form I would like to change that line.
#------------------- ajust the image
min_percent = 2.0 # Low percentile
max_percent = 98.0 # High percentile
for band_idx in range(img.shape[0]-1): # -1 for the nir (skip it)
lo, hi = np.percentile(img[band_idx], (min_percent, max_percent))
# Apply linear "stretch" - lo goes to 0, and hi goes to 1
res_img = (img[band_idx].astype(float) - lo) / (hi-lo)
#Multiply by 255, clamp range to [0, 255] and convert to uint8
img[band_idx] = np.maximum(np.minimum(res_img*255, 255), 0).astype(np.uint8)
Cool. It does seem like a much simpler implementation. With the current implementation, we had determined that the trim needed to apply to the histogram of the entire original image (before tiling), not just the tile that is being sent to training by dataloader. This required to
- generate the histogram (e.g. with np.bincount) during tiling
- store the histogram in the tile's metadata (currently in hdf5)
- read this histogram during training to trim according to original histogram.
This was more complex to implement and required passing more information from tiling to training. For the sake of simplicity, I prefer using this augmentation directly on the tiles' histogram. However, the risk is with very dark or bright tiles (i.e. with low contrast). These tiles would always get their histogram chopped more aggressively than tiles with high contrast.
Last tests by @valhassan show that the CLAHE enhancement algorithm may be a good solution compared to using the radiometric trim. The CLAHE contains an "Adaptive" aspect that produces a better result when dealing with a variety of histograms from our input rasters.
Closing this issue as Radiometric Trim will be longer be necessary to our group.
See #359