Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-morin committed Feb 15, 2024
1 parent 5503410 commit 60389be
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/inat_inferrer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

MINIMUM_GEO_SCORE = 0.005

TIME_DOWNLOAD = 0
TIME_RESIZE = 0
TIME_TOTAL = 0

class InatInferrer:

Expand Down Expand Up @@ -73,6 +76,7 @@ def setup_geo_model(self, config):
)

def prepare_image_for_inference(self, file_path):
START_TIME_RESIZE = time.time()
mime_type = magic.from_file(file_path, mime=True)
# attempt to convert non jpegs
if mime_type != "image/jpeg":
Expand All @@ -84,7 +88,11 @@ def prepare_image_for_inference(self, file_path):
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)
result = tf.expand_dims(image, 0)
END_TIME_RESIZE = time.time()
TIME_RESIZE = TIME_RESIZE + (END_TIME_RESIZE - START_TIME_RESIZE)
print("TIME-EXP: TIME_RESIZE "+str(TIME_RESIZE))
return result;

def vision_predict(self, image, debug=False):
if debug:
Expand Down
38 changes: 38 additions & 0 deletions lib/inat_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,40 @@ def h3_04_bounds_route(self):
return f'Unknown taxon_id {taxon_id}', 422
return results_dict

def bench_route(self):
start = request.args["start"]
count = request.args["count"]
self.inferrer.TIME_DOWNLOAD = 0
self.inferrer.TIME_RESIZE = 0
self.inferrer.TIME_TOTAL = 0

ALL_SCORES = ""

for observation_id in range(start, start + count):
print("TIME-EXP: score "+observation_id)
START_TIME_TOTAL = time.time()
image_uuid = "downloaded-obs-" + observation_id
START_TIME_DOWNLOAD = time.time()
file_path, lat, lng, iconic_taxon_id = self.download_observation(
observation_id, image_uuid)
END_TIME_DOWNLOAD = time.time()
self.inferrer.TIME_DOWNLOAD = self.inferrer.TIME_DOWNLOAD + (END_TIME_DOWNLOAD - START_TIME_DOWNLOAD)
print("TIME-EXP: TIME_DOWNLOAD "+str(self.inferrer.TIME_DOWNLOAD))
scores = self.score_image(form, file_path, lat, lng, iconic_taxon_id, geomodel)
END_TIME_TOTAL = time.time()
self.inferrer.TIME_TOTAL = self.inferrer.TIME_TOTAL + (END_TIME_TOTAL - START_TIME_TOTAL)
print("TIME-EXP: TIME_TOTAL "+str(self.inferrer.TIME_TOTAL))
ALL_SCORES = ALL_SCORES + "\n" + scores

result = "TOTAL = " + str(self.inferrer.TIME_TOTAL) + "\n" + \
"DOWNLOAD = " + str(self.inferrer.TIME_DOWNLOAD) + "\n" + \
"RESIZE = " + str(self.inferrer.TIME_RESIZE) + "\n" + \
ALL_SCORES

return result

def index_route(self):
START_TIME_TOTAL = time.time()
form = ImageForm()
if "observation_id" in request.args:
observation_id = request.args["observation_id"]
Expand All @@ -90,8 +123,11 @@ def index_route(self):
iconic_taxon_id = None
if observation_id:
image_uuid = "downloaded-obs-" + observation_id
START_TIME_DOWNLOAD = time.time()
file_path, lat, lng, iconic_taxon_id = self.download_observation(
observation_id, image_uuid)
END_TIME_DOWNLOAD = time.time()
self.inferrer.TIME_DOWNLOAD = self.inferrer.TIME_DOWNLOAD + (END_TIME_DOWNLOAD - START_TIME_DOWNLOAD)
else:
image_uuid = str(uuid.uuid4())
file_path = self.process_upload(form.image.data, image_uuid)
Expand All @@ -101,6 +137,8 @@ def index_route(self):
return render_template("home.html")

scores = self.score_image(form, file_path, lat, lng, iconic_taxon_id, geomodel)
END_TIME_TOTAL = time.time()
self.inferrer.TIME_TOTAL = self.inferrer.TIME_TOTAL + (END_TIME_TOTAL - START_TIME_TOTAL)
InatVisionAPI.write_logstash(
image_uuid, file_path, request_start_datetime, request_start_time)
return scores
Expand Down

0 comments on commit 60389be

Please sign in to comment.