Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/CICD-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
refresh-dev-staging-deployment:
name: Refresh Dev Staging Deployment
needs: build-and-push-dev-docker-image
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- name: Refresh Dev inatvisionapi
uses: actions/github-script@v6
Expand All @@ -89,7 +89,7 @@ jobs:
name: Notify Slack
needs: build-and-push-dev-docker-image
if: ${{ success() || failure() }}
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: iRoachie/slack-github-actions@v2.3.2
if: env.SLACK_WEBHOOK_URL != null
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/CICD-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
refresh-main-staging-deployment:
name: Refresh Main Staging Deployment
needs: build-and-push-main-docker-image
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- name: Refresh Main inatvisionapi
uses: actions/github-script@v6
Expand All @@ -92,7 +92,7 @@ jobs:
name: Notify Slack
needs: build-and-push-main-docker-image
if: ${{ success() || failure() }}
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: iRoachie/slack-github-actions@v2.3.2
if: env.SLACK_WEBHOOK_URL != null
Expand Down
18 changes: 17 additions & 1 deletion lib/inat_inferrer.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,10 @@ def common_ancestor_from_aggregated_scores(

return common_ancestor

def limit_leaf_scores_that_include_humans(self, leaf_scores):
def limit_leaf_scores_that_include_humans(self, leaf_scores, strategy=None):
if strategy == "never_exclude":
return leaf_scores

if self.taxonomy.human_taxon is None:
return leaf_scores

Expand All @@ -636,6 +639,19 @@ def limit_leaf_scores_that_include_humans(self, leaf_scores):
if human_score_margin > 1.5:
return top_results.head(1)

# if requesting a more limited approach to human exclusion
if strategy == "limited" and self.taxonomy.mammals_taxon is not None:
mammals_results = top_results.query(
f"left > {self.taxonomy.mammals_taxon['left']} and "
f"right < {self.taxonomy.mammals_taxon['right']}"
)
# if there is only 1 Mammals taxon (human), return all non-human results
if mammals_results.index.size == 1:
non_human_results = top_results.query(
f"taxon_id != {self.taxonomy.human_taxon['taxon_id']}"
)
return non_human_results

# otherwise return no results
return leaf_scores.head(0)

Expand Down
4 changes: 3 additions & 1 deletion lib/inat_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def score_image(self, form, file_path, lat, lng, filter_taxon_id, geomodel,
embedding = predictions_for_image["features"]
return InatVisionAPIResponses.aggregated_object_response(
leaf_scores, aggregated_scores, self.inferrer,
embedding=embedding
embedding=embedding,
human_exclusion_strategy=form.human_exclusion.data
)

# legacy dict response
Expand All @@ -192,6 +193,7 @@ def score_image(self, form, file_path, lat, lng, filter_taxon_id, geomodel,
self.inferrer,
common_ancestor_rank_type=common_ancestor_rank_type,
embedding=embedding,
human_exclusion_strategy=form.human_exclusion.data,
debug=self.debug
)

Expand Down
16 changes: 12 additions & 4 deletions lib/inat_vision_api_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ def array_response(leaf_scores, inferrer):

@staticmethod
def object_response(leaf_scores, inferrer, common_ancestor_rank_type=None,
embedding=None, debug=False):
embedding=None, debug=False, human_exclusion_strategy=None):
leaf_scores = InatVisionAPIResponses.limit_leaf_scores_for_response(leaf_scores)
leaf_scores = InatVisionAPIResponses.update_leaf_scores_scaling(leaf_scores)
post_human_exclusion_scores = inferrer.limit_leaf_scores_that_include_humans(leaf_scores)

post_human_exclusion_scores = inferrer.limit_leaf_scores_that_include_humans(
leaf_scores, strategy=human_exclusion_strategy
)
human_exclusion_cleared_results = False
if not leaf_scores.empty and post_human_exclusion_scores.empty:
human_exclusion_cleared_results = True
Expand Down Expand Up @@ -85,7 +88,9 @@ def aggregated_tree_response(aggregated_scores, inferrer):
return "<pre>" + "<br/>".join(printable_tree) + "</pre>"

@staticmethod
def aggregated_object_response(leaf_scores, aggregated_scores, inferrer, embedding=None):
def aggregated_object_response(
leaf_scores, aggregated_scores, inferrer, embedding=None, human_exclusion_strategy=None
):
top_leaf_combined_score = aggregated_scores.query(
"leaf_class_id.notnull()"
).sort_values(
Expand All @@ -100,7 +105,10 @@ def aggregated_object_response(leaf_scores, aggregated_scores, inferrer, embeddi
"normalized_aggregated_combined_score",
ascending=False
).head(100)
top_100_leaves = inferrer.limit_leaf_scores_that_include_humans(top_100_leaves)

top_100_leaves = inferrer.limit_leaf_scores_that_include_humans(
top_100_leaves, strategy=human_exclusion_strategy
)

aggregated_scores = InatVisionAPIResponses.update_aggregated_scores_scaling(
aggregated_scores
Expand Down
15 changes: 15 additions & 0 deletions lib/model_taxonomy_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ModelTaxonomyDataframe:
def __init__(self, path, thresholds_path):
self.load_mapping(path, thresholds_path)
self.set_human_taxon()
self.set_mammals_taxon()

def load_mapping(self, path, thresholds_path):
self.df = pd.read_csv(
Expand Down Expand Up @@ -78,6 +79,20 @@ def set_human_taxon(self):

self.human_taxon = human_rows.iloc[0]

def set_mammals_taxon(self):
self.mammals_taxon = None
if self.human_taxon is None:
return

mammals_rows = self.df.query(
f"name == 'Mammalia' and left < {self.human_taxon['left']} and "
f"right > {self.human_taxon['right']}"
)
if mammals_rows.empty:
return

self.mammals_taxon = mammals_rows.iloc[0]

@staticmethod
def children(df, taxon_id):
if taxon_id == 0:
Expand Down
6 changes: 6 additions & 0 deletions lib/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ <h2>Slim vs Legacy Model</h2>
<option value="major">Major Ranks (genus to kingdom)</option>
</select>
<br/>
<select name="human_exclusion">
<option>Exclude Humans</option>
<option value="limited">Limited Human Exclusion</option>
<option value="never_exclude">Never Exclude Humans</option>
</select>
<br/>
<br/>
<button type="submit">Submit</button>
</form>
Expand Down
1 change: 1 addition & 0 deletions lib/web_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ class ImageForm(FlaskForm):
aggregated = StringField("aggregated")
return_embedding = StringField("return_embedding")
common_ancestor_rank_type = StringField("common_ancestor_rank_type")
human_exclusion = StringField("human_exclusion")
format = StringField("format")