Skip to content

Commit

Permalink
Merge pull request #15 from inaturalist/ci
Browse files Browse the repository at this point in the history
Adding Tests
  • Loading branch information
pleary authored Feb 6, 2024
2 parents 9b6b3a4 + cd895fe commit 278647f
Show file tree
Hide file tree
Showing 28 changed files with 556 additions and 456 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: inatVisionAPI CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4
- name: Use Python
uses: actions/setup-python@v4
with:
python-version: '3.11.6'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Test with pytest
run: |
coverage run -m pytest -s && coverage report --show-missing
notify:
name: Notify Slack
needs: build
if: ${{ success() || failure() }}
runs-on: ubuntu-20.04
steps:
- uses: iRoachie/[email protected]
if: env.SLACK_WEBHOOK_URL != null
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_BUILDS_WEBHOOK_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
14 changes: 7 additions & 7 deletions .github/workflows/CICD-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ on:

jobs:
build-and-test:
name: Build/Test
name: Build/Test
runs-on: ubuntu-20.04
steps:
steps:
- uses: actions/checkout@v4

build-and-push-dev-docker-image:
name: Build/Push Dev Docker Image
name: Build/Push Dev Docker Image
runs-on: ubuntu-20.04
steps:
steps:
- uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build/Push dev vision-api
uses: docker/build-push-action@v5
with:
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/CICD-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ on:

jobs:
build-and-test:
name: Build/Test
name: Build/Test
runs-on: ubuntu-20.04
steps:
steps:
- uses: actions/checkout@v4

build-and-push-staging-docker-image:
name: Build/Push Staging Docker Image
name: Build/Push Staging Docker Image
runs-on: ubuntu-20.04
steps:
steps:
- uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build/Push staging vision-api
uses: docker/build-push-action@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion docker/logrotate-cron.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
/usr/sbin/logrotate -s /var/vision/script/logrotate.status /var/vision/script/logrotate.conf
/usr/sbin/logrotate -s /var/vision/script/logrotate.status /var/vision/script/logrotate.conf
43 changes: 26 additions & 17 deletions lib/inat_inferrer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class InatInferrer:
def __init__(self, config):
self.config = config
self.setup_taxonomy(config)
self.setup_synonyms(config)
self.setup_vision_model(config)
self.setup_elevation_dataframe(config)
self.setup_geo_model(config)
Expand All @@ -35,8 +36,15 @@ def setup_taxonomy(self, config):
config["tf_elev_thresholds"] if "tf_elev_thresholds" in config else None
)

def setup_synonyms(self, config):
self.synonyms = None
if "synonyms_path" in config:
if not os.path.exists(config["synonyms_path"]):
return None
self.synonyms = pd.read_csv(config["synonyms_path"])

def setup_vision_model(self, config):
self.vision_inferrer = VisionInferrer(config["vision_model_path"], self.taxonomy)
self.vision_inferrer = VisionInferrer(config["vision_model_path"])

def setup_elevation_dataframe(self, config):
self.geo_elevation_cells = None
Expand Down Expand Up @@ -72,20 +80,6 @@ def setup_geo_model(self, config):
elevation=list(self.geo_elevation_cells.elevation)
)

def prepare_image_for_inference(self, file_path):
mime_type = magic.from_file(file_path, mime=True)
# attempt to convert non jpegs
if mime_type != "image/jpeg":
im = Image.open(file_path)
image = im.convert("RGB")
else:
image = tf.io.read_file(file_path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.central_crop(image, 0.875)
image = tf.image.resize(image, [299, 299], tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return tf.expand_dims(image, 0)

def vision_predict(self, image, debug=False):
if debug:
start_time = time.time()
Expand Down Expand Up @@ -125,7 +119,7 @@ def predictions_for_image(self, file_path, lat, lng, filter_taxon, score_without
debug=False):
if debug:
start_time = time.time()
image = self.prepare_image_for_inference(file_path)
image = InatInferrer.prepare_image_for_inference(file_path)
raw_vision_scores = self.vision_predict(image, debug)
raw_geo_scores = self.geo_model_predict(lat, lng, debug)
top100 = self.combine_results(raw_vision_scores, raw_geo_scores, filter_taxon,
Expand Down Expand Up @@ -359,6 +353,21 @@ def h3_04_bounds(self, taxon_id):
"nelng": geomodel_results["lng"].max()
}

@staticmethod
def prepare_image_for_inference(file_path):
mime_type = magic.from_file(file_path, mime=True)
# attempt to convert non jpegs
if mime_type != "image/jpeg":
im = Image.open(file_path)
image = im.convert("RGB")
else:
image = tf.io.read_file(file_path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.central_crop(image, 0.875)
image = tf.image.resize(image, [299, 299], tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return tf.expand_dims(image, 0)

@staticmethod
def add_lat_lng_to_h3_geo_dataframe(geo_df):
geo_df = geo_df.h3.h3_to_geo()
Expand All @@ -373,7 +382,7 @@ def filter_geo_dataframe_by_bounds(geo_df, bounds):
# centroid outside the bounds while part of the polygon is within the bounds. Add
# a small buffer to ensure this returns any cell whose polygon is
# even partially within the bounds
buffer = 0.6
buffer = 1.3

# similarly, the centroid may be on the other side of the antimedirian, so lookup
# cells that might be just over the antimeridian on either side
Expand Down
Loading

0 comments on commit 278647f

Please sign in to comment.