Skip to content

Rem runner loops; use OpenCV #2

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 29 additions & 17 deletions objectdetection_coral_multitpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import os
import threading
import time
import cv2
#import tracemalloc

from PIL import Image
Expand Down Expand Up @@ -218,7 +219,8 @@ def main():
options.downsample_by = 100

options.label_file = args.labels
image = Image.open(args.input)
#image = Image.open(args.input)
image = cv2.imread(args.input, cv2.IMREAD_COLOR)
init_detect(options, args.num_tpus)

print('----INFERENCE TIME----')
Expand All @@ -229,30 +231,32 @@ def main():

thread_cnt = 16
tot_infr_time = 0
q_infr_count = 0
q_wall_start = None

half_wall_start = None
half_infr_count = 0
start = time.perf_counter()

if args.count > 1:
with concurrent.futures.ThreadPoolExecutor(max_workers=thread_cnt) as executor:
start = time.perf_counter()
for chunk_i in range(0, args.count-1, thread_cnt*8):
fs = [executor.submit(_tpu_runner.process_image, options, copy.copy(image), args.threshold)
for i in range(min(thread_cnt*8, args.count-1 - chunk_i))]
for f in concurrent.futures.as_completed(fs):
_, infr_time, _ = f.result()
tot_infr_time += infr_time

# Start a timer for the last ~half of the run for more accurate benchmark
if chunk_i > (args.count-1) / 2.0:
half_infr_count += 1
if half_wall_start is None:
half_wall_start = time.perf_counter()
# Start a timer for the last ~quarter of the run for more accurate benchmark
if chunk_i > (args.count-1) * 3.0 / 4.0:
q_infr_count += 1
if q_wall_start is None:
q_wall_start = time.perf_counter()

# Uncomment for testing
# import random
# logging.info("Pause")
# time.sleep(random.randint(0,INTERPRETER_LIFESPAN_SECONDS*3))
else:
start = time.perf_counter()

# snapshot = tracemalloc.take_snapshot()
# top_stats = snapshot.statistics('lineno')
Expand All @@ -262,20 +266,27 @@ def main():
start_one = time.perf_counter()
objs, infr_time, _ = _tpu_runner.process_image(options, copy.copy(image), args.threshold)
tot_infr_time += infr_time
half_infr_count += 1
q_infr_count += 1
wall_time = time.perf_counter() - start

half_wall_time = 0.0
if half_wall_start is not None:
half_wall_time = time.perf_counter() - half_wall_start
q_wall_time = 0.0
mpps = 0.0
if q_wall_start is not None:
q_wall_time = time.perf_counter() - q_wall_start

mpps = (_tpu_runner.input_details['shape'][1] - options.tile_overlap) \
* (_tpu_runner.input_details['shape'][2] - options.tile_overlap) \
* q_infr_count \
/ (q_wall_time * 1000000)

logging.info('completed one run every %.2fms for %d runs; %.2fms wall time for a single run' %
(wall_time * 1000 / args.count, args.count,
(time.perf_counter() - start_one) * 1000))

logging.info('%.2fms avg time blocked across %d threads; %.3fms ea for final %d inferences' %
logging.info('%.2fms avg time blocked across %d threads; %.2fms ea for final %d inferences; %.2f tensor MPx / sec' %
(tot_infr_time / args.count, thread_cnt,
half_wall_time * 1000 / half_infr_count, half_infr_count))
q_wall_time * 1000 / q_infr_count, q_infr_count,
mpps))

logging.info('-------RESULTS--------')
if not objs:
Expand All @@ -290,10 +301,11 @@ def main():
logging.info(f' bbox: {obj.bbox}')

if args.output:
image = image.convert('RGB')
# image = image.convert('RGB')
image = Image.fromarray(image)
draw_objects(ImageDraw.Draw(image), objs, _tpu_runner.labels)
image.save(args.output, subsampling=2, quality=95)
#image.show()
image.show()


if __name__ == '__main__':
Expand Down
Loading