Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
538 changes: 503 additions & 35 deletions lib/inat_inferrer.py

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions lib/inat_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,44 +155,56 @@ def index_route(self):

def score_image(self, form, file_path, lat, lng, filter_taxon_id, geomodel,
common_ancestor_rank_type=None):
if self.debug:
start_time = time.time()
filter_taxon = None
if filter_taxon_id is not None:
if self.inferrer.taxon_exists(filter_taxon_id):
filter_taxon = self.inferrer.lookup_taxon(filter_taxon_id)
else:
filter_taxon = ModelTaxonomyDataframe.undefined_filter_taxon()

predictions_for_image = self.inferrer.predictions_for_image(
file_path, lat, lng, filter_taxon, debug=self.debug
)
leaf_scores = predictions_for_image["combined_scores"]

if self.debug:
print("score_image Time: %0.2fms" % ((time.time() - start_time) * 1000.))

if form.aggregated.data == "true":
aggregated_scores = self.inferrer.aggregate_results(leaf_scores, debug=self.debug)
if form.format.data == "tree":
return InatVisionAPIResponses.aggregated_tree_response(
aggregated_scores, self.inferrer
)
embedding = predictions_for_image["features"]
return InatVisionAPIResponses.aggregated_object_response(
response = InatVisionAPIResponses.aggregated_object_response(
leaf_scores, aggregated_scores, self.inferrer,
embedding=embedding,
human_exclusion_strategy=form.human_exclusion.data
)
if self.debug:
print("total Time: %0.2fms" % ((time.time() - start_time) * 1000.))
return response

# legacy dict response
if geomodel != "true":
return InatVisionAPIResponses.legacy_dictionary_response(leaf_scores, self.inferrer)

if form.format.data == "object":
embedding = predictions_for_image["features"]
return InatVisionAPIResponses.object_response(
response = InatVisionAPIResponses.object_response(
leaf_scores,
self.inferrer,
common_ancestor_rank_type=common_ancestor_rank_type,
embedding=embedding,
human_exclusion_strategy=form.human_exclusion.data,
debug=self.debug
)
if self.debug:
print("total Time: %0.2fms" % ((time.time() - start_time) * 1000.))
return response

return InatVisionAPIResponses.array_response(leaf_scores, self.inferrer)

Expand Down Expand Up @@ -223,8 +235,12 @@ def download_observation(self, observation_id, image_uuid):
latlng = [None, None]
else:
latlng = data["results"][0]["location"].split(",")
if data["results"][0]["taxon"] is None:
iconic_taxon_id = None
else:
iconic_taxon_id = data["results"][0]["taxon"]["iconic_taxon_id"]
# return the path to the cached image, coordinates, and iconic taxon
return cache_path, latlng[0], latlng[1], data["results"][0]["taxon"]["iconic_taxon_id"]
return cache_path, latlng[0], latlng[1], iconic_taxon_id

def valid_leaf_taxon_id_for_request(self, request):
if "taxon_id" in request.args:
Expand Down
36 changes: 24 additions & 12 deletions lib/inat_vision_api_responses.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import numpy as np
import pandas as pd
from lib.model_taxonomy_dataframe import ModelTaxonomyDataframe
Expand All @@ -20,10 +21,17 @@ def array_response(leaf_scores, inferrer):
return InatVisionAPIResponses.array_response_columns(leaf_scores).to_dict(orient="records")

@staticmethod
def object_response(leaf_scores, inferrer, common_ancestor_rank_type=None,
def object_response(all_scores, inferrer, common_ancestor_rank_type=None,
embedding=None, debug=False, human_exclusion_strategy=None):

if debug:
start_time = time.time()
leaf_scores = all_scores.query("leaf_class_id.notnull() and vision_score > 0")
leaf_scores = InatVisionAPIResponses.limit_leaf_scores_for_response(leaf_scores)
leaf_scores = InatVisionAPIResponses.update_leaf_scores_scaling(leaf_scores)
if debug:
print("object_response update_scaling: %0.2fms" % ((time.time() - start_time) * 1000.))
start_time = time.time()

post_human_exclusion_scores = inferrer.limit_leaf_scores_that_include_humans(
leaf_scores, strategy=human_exclusion_strategy
Expand All @@ -32,15 +40,21 @@ def object_response(leaf_scores, inferrer, common_ancestor_rank_type=None,
if not leaf_scores.empty and post_human_exclusion_scores.empty:
human_exclusion_cleared_results = True
leaf_scores = post_human_exclusion_scores

results = InatVisionAPIResponses.array_response_columns(
leaf_scores.sort_values(
"combined_score",
ascending=False
).head(10)
).to_dict(orient="records")
if inferrer.new_scoring:
all_scores = InatVisionAPIResponses.update_leaf_scores_scaling(all_scores)
common_ancestor = inferrer.common_ancestor_from_leaf_scores(
leaf_scores, common_ancestor_rank_type=common_ancestor_rank_type, debug=debug
all_scores, common_ancestor_rank_type=common_ancestor_rank_type, debug=debug
)
if debug:
print("object_response common_ancestor: %0.2fms" % ((time.time() - start_time) * 1000.))
start_time = time.time()
if common_ancestor is not None:
common_ancestor_frame = pd.DataFrame([common_ancestor])
common_ancestor_frame = InatVisionAPIResponses.update_common_ancestor_scores_scaling(
Expand All @@ -57,6 +71,9 @@ def object_response(leaf_scores, inferrer, common_ancestor_rank_type=None,
}
if embedding is not None:
response["embedding"] = embedding.numpy().tolist()
if debug:
print("object_response remainder: %0.2fms" % ((time.time() - start_time) * 1000.))
start_time = time.time()
return response

@staticmethod
Expand Down Expand Up @@ -127,6 +144,7 @@ def aggregated_object_response(

common_ancestor = inferrer.common_ancestor_from_leaf_scores(
leaf_scores, debug=True, disallow_humans=True)

if common_ancestor is not None:
common_ancestor_frame = pd.DataFrame([common_ancestor])
common_ancestor_frame = InatVisionAPIResponses.update_aggregated_scores_scaling(
Expand All @@ -148,10 +166,7 @@ def aggregated_object_response(
def limit_leaf_scores_for_response(leaf_scores):
if leaf_scores.empty:
return leaf_scores
top_combined_score = leaf_scores.sort_values(
"combined_score",
ascending=False
).head(1)["combined_score"].values[0]
top_combined_score = leaf_scores["combined_score"].max()
# set a cutoff so results whose combined scores are
# much lower than the best score are not returned
leaf_scores = leaf_scores.query(f"combined_score > {top_combined_score * 0.001}")
Expand All @@ -162,7 +177,6 @@ def update_leaf_scores_scaling(leaf_scores):
score_columns = [
"normalized_combined_score",
"geo_score",
"raster_geo_score",
"normalized_vision_score",
"geo_threshold"
]
Expand All @@ -187,9 +201,9 @@ def update_aggregated_scores_scaling(aggregated_scores):
"aggregated_combined_score",
"normalized_aggregated_combined_score",
"aggregated_geo_score",
"aggregated_raster_geo_score",
"aggregated_vision_score",
"aggregated_geo_threshold"
"aggregated_geo_threshold",
"normalized_geo_score"
]
aggregated_scores[score_columns] = aggregated_scores[
score_columns
Expand All @@ -201,7 +215,6 @@ def array_response_columns(leaf_scores):
columns_to_return = [
"normalized_combined_score",
"geo_score",
"raster_geo_score",
"taxon_id",
"name",
"normalized_vision_score",
Expand Down Expand Up @@ -239,7 +252,6 @@ def aggregated_scores_response_columns(aggregated_scores):
"aggregated_combined_score",
"normalized_aggregated_combined_score",
"aggregated_geo_score",
"aggregated_raster_geo_score",
"taxon_id",
"parent_taxon_id",
"name",
Expand All @@ -249,14 +261,14 @@ def aggregated_scores_response_columns(aggregated_scores):
"depth",
"aggregated_vision_score",
"aggregated_geo_threshold",
"normalized_geo_score"
]
column_mapping = {
"taxon_id": "id",
"parent_taxon_id": "parent_id",
"aggregated_combined_score": "combined_score",
"normalized_aggregated_combined_score": "normalized_combined_score",
"aggregated_geo_score": "geo_score",
"aggregated_raster_geo_score": "raster_geo_score",
"aggregated_vision_score": "vision_score",
"aggregated_geo_threshold": "geo_threshold"
}
Expand Down
Loading
Loading