<jupyter_start><jupyter_text>KNN Depth Estimation DemoThis notebook will illustrate how to use the [OpticalRS Python library](http://jkibele.github.io/OpticalRS/) to estimate depths as described in Kibele and Shears (not published yet).<jupyter_code>%pylab inline
style.use('ggplot')
from OpticalRS import *
import warnings
warnings.filterwarnings('ignore')<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>Change to the working directory to the location where the test data are stored.<jupyter_code>cd ~/Documents/code_projects/OpticalRS/tests/data/<jupyter_output>/home/jkibele/Documents/code_projects/OpticalRS/tests/data<jupyter_text>Create a depth estimator object with the denoised WorldView-2 imagery sample and a raster of known depths.<jupyter_code>de = DepthEstimator('eReefWV2_denoised.tif', 'BPS_adj_depth_raster_masked.tif')<jupyter_output><empty_output><jupyter_text>Use the `DepthEstimator` object to estimate the depths:<jupyter_code>depthest = de.knn_depth_estimation(k=5)<jupyter_output><empty_output><jupyter_text>Display an RGB version of the multispectral imagery with raster of known depths overlayed. Then display the estimated depths. This step is just for display. It is not necessary for depth estimation.<jupyter_code>fig, (ax1, ax2) = subplots(1, 2, figsize=(12,8))
rgbarr = MSExposure.equalize_adapthist(de.imarr[...,[3,2,1]], clip_limit=0.02)
ax1.imshow(rgbarr)
mapable = ax1.imshow(de.known_depth_arr)
ax1.set_axis_off()
ax1.set_title("RGB Image and Known Depths")
blah = colorbar(mapable, ax=ax1, label='Depth (m)', cmap=mpl.cm.Spectral)

m2 = ax2.imshow(depthest, cmap=mpl.cm.jet)
blah = colorbar(m2, ax=ax2, label='Depth (m)')
ax2.set_title("KNN Depth Estimate")
ax2.set_axis_off()<jupyter_output><empty_output><jupyter_text>Calculate root mean square error (RMSE) and plot estimated depths against measured depths. Because the same measured depths are being used for both training and evaluation, the RMSE will be better (lower) than it should be. In practice, the training and evaluation (test) sets should be different.<jupyter_code>err = de.known_depth_arr - depthest
rmse = np.sqrt((err**2).mean())

scatter(de.known_depth_arr, depthest, edgecolor='none', alpha=0.3)
ax = gca()
titletxt = "Estimate Self RMSE: {:.2}m".format(rmse)
ax.set_title(titletxt)
ax.set_xlabel("Measured Depths (m)")
ax.set_ylabel("KNN Estimated Depths (m)")
ax.set_aspect('equal')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11030008110300081, 'fraction_numerical': 0.012570965125709651, 'mean_word_length': 7.278523489932886, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '553578', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 894, 'n_tokens_neox': 855, 'n_words': 217, 'path': 'docs/notebooks/demos/KNNDepthDemo.ipynb', 'repo_name': 'melkimble/OpticalRS', 'size': '2466'}