Skip to content

Commit 3759bce

Browse files
committed
Bug fixes
1 parent bc83573 commit 3759bce

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/modules/ObjectDetectionCoral/objectdetection_coral_multitpu.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def main():
231231

232232
thread_cnt = 16
233233
tot_infr_time = 0
234-
half_wall_start = 0
234+
half_wall_start = None
235235
half_infr_count = 0
236236
if args.count > 1:
237237
with concurrent.futures.ThreadPoolExecutor(max_workers=thread_cnt) as executor:
@@ -244,9 +244,9 @@ def main():
244244
tot_infr_time += infr_time
245245

246246
# Start a timer for the last ~half of the run for more accurate benchmark
247-
if chunk_i > (args.count-1 / 3):
247+
if chunk_i > (args.count-1) / 3.0:
248248
half_infr_count += 1
249-
if half_wall_start == 0:
249+
if half_wall_start is None:
250250
half_wall_start = time.perf_counter()
251251

252252
# Uncomment for testing
@@ -255,7 +255,6 @@ def main():
255255
# time.sleep(random.randint(0,INTERPRETER_LIFESPAN_SECONDS*3))
256256
else:
257257
start = time.perf_counter()
258-
half_wall_start = time.perf_counter()
259258

260259
# snapshot = tracemalloc.take_snapshot()
261260
# top_stats = snapshot.statistics('lineno')
@@ -267,15 +266,18 @@ def main():
267266
tot_infr_time += infr_time
268267
half_infr_count += 1
269268
wall_time = time.perf_counter() - start
270-
half_wall_time = time.perf_counter() - half_wall_start
269+
270+
half_wall_time = 0.0
271+
if half_wall_start is not None:
272+
half_wall_time = time.perf_counter() - half_wall_start
271273

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

276278
print('%.2fms avg time blocked across %d threads; %.2fms ea for final %d inferences' %
277279
(tot_infr_time / args.count, thread_cnt,
278-
half_wall_time * 1000 / half_infr_count, half_infr_count)
280+
half_wall_time * 1000 / half_infr_count, half_infr_count))
279281

280282
print('-------RESULTS--------')
281283
if not objs:

src/modules/ObjectDetectionCoral/tpu_runner.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import logging
2222
import queue
2323
import gc
24+
import math
2425

2526
from datetime import datetime
2627

@@ -147,7 +148,8 @@ def _interpreter_runner(self, seg_idx: int):
147148
self.interpreter = None
148149
self.input_details = None
149150
self.output_details = None
150-
self.rebalancing_lock.release()
151+
if self.rebalancing_lock.locked():
152+
self.rebalancing_lock.release()
151153
return
152154

153155
start_inference_time = time.perf_counter_ns()
@@ -466,7 +468,7 @@ def _watchdog(self):
466468
time.time() - self.watchdog_time > self.max_idle_secs_before_recycle:
467469
logging.warning("No work in {} seconds, watchdog shutting down TPUs.".format(self.max_idle_secs_before_recycle))
468470
self.runner_lock.acquire(timeout=MAX_WAIT_TIME)
469-
if self.pipe:
471+
if self.pipe.first_name:
470472
self.pipe.delete()
471473
self.runner_lock.release()
472474
# Pipeline will reinitialize itself as needed
@@ -726,7 +728,7 @@ def _delete(self):
726728
def pipeline_ok(self) -> bool:
727729
""" Check we have valid interpreters """
728730
with self.runner_lock:
729-
return self.pipe and any(self.pipe.interpreters)
731+
return bool(self.pipe and any(self.pipe.interpreters))
730732

731733
def process_image(self,
732734
options:Options,
@@ -1142,9 +1144,17 @@ def _resize_and_chop_tiles(self,
11421144
self.printed_shape_map[key] = True
11431145

11441146
# Do chunking
1147+
# Image will not be an even fit in at least one dimension; space tiles appropriately.
11451148
tiles = []
1146-
for x_off in range(0, image.width - options.tile_overlap, int(ceil((image.width - m_width)/tiles_x))):
1147-
for y_off in range(0, image.height - options.tile_overlap, int(ceil((image.height - m_height)/tiles_y))):
1149+
step_x = 1
1150+
if tiles_x > 1:
1151+
step_x = int(math.ceil((image.width - m_width)/(tiles_x-1)))
1152+
step_y = 1
1153+
if tiles_y > 1:
1154+
step_y = int(math.ceil((image.height - m_height)/(tiles_y-1)))
1155+
1156+
for x_off in range(0, max(image.width - m_width, 0) + tiles_x, step_x):
1157+
for y_off in range(0, max(image.height - m_height, 0) + tiles_y, step_y):
11481158
# Adjust contrast on a per-chunk basis; we will likely be quantizing the image during scaling
11491159
image_chunk = ImageOps.autocontrast(image.crop((x_off,
11501160
y_off,
@@ -1158,9 +1168,6 @@ def _resize_and_chop_tiles(self,
11581168

11591169
tiles.append((cropped_arr.astype(self.input_details['dtype']), resamp_info))
11601170

1161-
# Check again with various scales
1162-
#Image.fromarray((tiles[-1][0] + 128).astype(np.uint8)).save("test.png")
1163-
11641171

11651172
return tiles
11661173

0 commit comments

Comments
 (0)