-
Notifications
You must be signed in to change notification settings - Fork 11
use centroids for geo prior inference #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3021b63
do inference at points
alexshepard d3ddfd3
update sample config.yml
alexshepard 312c1a7
utility to format elevation feats
alexshepard 971c922
add required change to config file for ci test
alexshepard 46a787f
return traditional h3 geo scores for marking results nearby
pleary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import math | ||
|
|
||
| import numpy as np | ||
| import tensorflow as tf | ||
|
|
||
|
|
||
| class CoordEncoder: | ||
| def __init__(self, raster): | ||
| assert raster is not None | ||
| self.raster = np.nan_to_num(raster, nan=0.0) | ||
|
|
||
| def encode(self, locs): | ||
| locs = CoordEncoder.normalize_coords(locs) | ||
|
|
||
| loc_feats = CoordEncoder.encode_loc_sinusoidal(locs) | ||
|
|
||
| context_feats = self.bilinear_interpolate(locs) | ||
| loc_feats = np.concatenate((loc_feats, context_feats), 1) | ||
|
|
||
| return loc_feats | ||
|
|
||
| def bilinear_interpolate(self, loc_ip): | ||
| """ | ||
| Perform bilinear interpolation on a raster using normalized | ||
| [-1, 1] lng, lat input. | ||
|
|
||
| Args: | ||
| loc_ip: [N x 2] tensor/array of [lng, lat] in [-1, 1] space | ||
| data: [H x W x C] raster data | ||
| remove_nans_raster: whether to replace NaNs in `data` with 0.0 | ||
|
|
||
| Returns: | ||
| np.ndarray: [N x C] interpolated feats for each location | ||
| """ | ||
| assert loc_ip.shape[1] == 2 | ||
|
|
||
| # normalize from [-1, 1] to [0, 1] | ||
| loc = (loc_ip + 1.0) / 2.0 | ||
|
|
||
| # flip y-axis for raster top down layout | ||
| x = loc[:, 0] | ||
| y = 1.0 - loc[:, 1] | ||
|
|
||
| # convert to pixel indices | ||
| px = x * (self.raster.shape[1] - 1) | ||
| py = y * (self.raster.shape[0] - 1) | ||
|
|
||
| # corner integer indices | ||
| x0 = tf.floor(px).numpy().astype(int) | ||
| y0 = tf.floor(py).numpy().astype(int) | ||
| x1 = np.clip(x0 + 1, 0, self.raster.shape[1] - 1) | ||
| y1 = np.clip(y0 + 1, 0, self.raster.shape[0] - 1) | ||
|
|
||
| # deltas for interpolation | ||
| dx = np.expand_dims(px - x0, axis=1) | ||
| dy = np.expand_dims(py - y0, axis=1) | ||
|
|
||
| # fetch corner values | ||
| top_left = self.raster[y0, x0, :] | ||
| top_right = self.raster[y0, x1, :] | ||
| bottom_left = self.raster[y1, x0, :] | ||
| bottom_right = self.raster[y1, x1, :] | ||
|
|
||
| # bilinear interpolation | ||
| interp_value = ( | ||
| (top_left * (1 - dx) * (1 - dy)) + # noqa: W504 | ||
| (top_right * dx * (1 - dy)) + # noqa: W504 | ||
| (bottom_left * (1 - dx) * dy) + # noqa: W504 | ||
| (bottom_right * dx * dy) | ||
| ) | ||
|
|
||
| return interp_value | ||
|
|
||
| @staticmethod | ||
| def normalize_coords(locs): | ||
| return tf.stack([ | ||
| locs[:, 0] / 180.0, | ||
| locs[:, 1] / 90.0, | ||
| ], axis=1) | ||
|
|
||
| @staticmethod | ||
| def encode_loc_sinusoidal(loc_ip): | ||
| return tf.concat([ | ||
| tf.sin(loc_ip * math.pi), | ||
| tf.cos(loc_ip * math.pi), | ||
| ], axis=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point I think I'd like to refactor things so the TFGeoPriorModelElev class accepts a lat/lng and elevation encoding type and does the elevation calculation and scoring itself. That way TFGeoPriorModelElev can call CoordEncoder if it needs to, and this InatInferrer class doesn't need to think about those things. InatInferrer will just ask for the geo score for a lat lng using the traditional H3 encoder, or using the new CoordEncoder encoder and TFGeoPriorModelElev will handle the rest