Skip to content

Commit c1fb90a

Browse files
authored
Minor tweaks
1 parent bc9fbe1 commit c1fb90a

1 file changed

Lines changed: 19 additions & 36 deletions

File tree

‎src/modules/ObjectDetectionCoral/tpu_runner.py‎

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,12 @@ def _interpreter_runner(self, seg_idx: int):
142142
working_tensors = in_q.get()
143143

144144
# Exit if the pipeline is done
145-
if not working_tensors:
145+
if working_tensors is False:
146146
logging.debug("Get EOF in tid {}".format(threading.get_ident()))
147147
self.interpreter = None
148148
self.input_details = None
149149
self.output_details = None
150-
if self.rebalancing_lock.locked():
151-
self.rebalancing_lock.release()
150+
self.rebalancing_lock.release()
152151
return
153152

154153
start_inference_time = time.perf_counter_ns()
@@ -194,13 +193,11 @@ def __del__(self):
194193
avg_time = 0.0
195194
avg_q = 0.0
196195
t_str += " {:5.1f}".format(avg_time)
197-
q_str += " {:5.1f}".format(avg_q)
196+
q_str += " {:4.1f}".format(avg_q)
198197
c_str += " {:7d}".format(c)
199-
logging.info(f"{self.tpu_name} time, queue len, & count:{t_str};{q_str};{c_str}")
198+
logging.info(f"{self.tpu_name} time, queue len, & count:{t_str}|{q_str}|{c_str}")
200199

201200
self.interpreter = None
202-
self.input_details = None
203-
self.output_details = None
204201
self.delegate = None
205202
self.queues = None
206203

@@ -209,19 +206,21 @@ class DynamicPipeline(object):
209206

210207
def __init__(self, tpu_list: list, fname_list: list):
211208
seg_count = len(fname_list)
212-
assert seg_count <= len(tpu_list), "seg_count: {} tpu_list: {}".format(seg_count, len(tpu_list))
209+
assert seg_count <= len(tpu_list), f"More segments than TPUs to run them! {seg_count} vs {len(tpu_list)}"
213210

214211
self.max_pipeline_queue_length = MAX_PIPELINE_QUEUE_LEN
215212

216213
self.fname_list = fname_list
217214
self.tpu_list = tpu_list
215+
self.interpreters = [[] for i in range(seg_count)]
218216

219-
# Input queues for each segment
217+
# Input queues for each segment; if we go over maxsize, something went wrong
220218
self.queues = [queue.Queue(maxsize=self.max_pipeline_queue_length) for i in range(seg_count)]
221219

222-
self.interpreters = [[] for i in range(seg_count)]
223-
self.moving_avg = [0.0 for i in range(seg_count)]
220+
# Lock for internal reorganization
224221
self.balance_lock = threading.Lock()
222+
223+
# Lock for interpreter use
225224
self.rebalancing_lock = threading.Lock()
226225

227226
# Read file data
@@ -322,7 +321,7 @@ def eval_timings(interpreter_counts):
322321
self.balance_lock.release()
323322
return
324323

325-
logging.info(f"Re-balancing from queue {min_i} to {max_i} (max from {current_max:5.2f} to {new_max:5.2f})")
324+
logging.info(f"Re-balancing from queue {min_i} to {max_i} (max from {current_max:.2f} to {new_max:.2f})")
326325

327326
# Sending False kills the processing loop
328327
self.rebalancing_lock.acquire()
@@ -712,38 +711,28 @@ def _periodic_check(self, options: Options, force: bool = False,
712711

713712
return (bool(self.pipe), error)
714713

715-
716714
def __del__(self):
717715
with self.runner_lock:
718716
self._delete()
719717
self.watchdog_shutdown = True
720718
self.watchdog_thread.join(timeout=self.watchdog_idle_secs*2)
721719

722-
723720
def _delete(self):
724721
# Close pipeline
725722
if self.pipe:
726723
self.pipe.delete()
727724
self.pipe = None
728-
gc.collect()
729-
730725

731726
def pipeline_ok(self) -> bool:
732727
""" Check we have valid interpreters """
733-
if self.pipe and any(self.pipe.interpreters):
734-
return True
735-
736-
return False
737-
728+
with self.runner_lock:
729+
return self.pipe and any(self.pipe.interpreters)
738730

739731
def process_image(self,
740732
options:Options,
741733
image: Image,
742734
score_threshold: float) -> (list, int, str):
743735
while True:
744-
# Sanity check
745-
assert self.pipe, "No processing pipeline, have we been initialized?"
746-
747736
try:
748737
return self._process_image(options, image, score_threshold)
749738
except queue.Empty:
@@ -772,12 +761,9 @@ def _process_image(self,
772761
Note that the image object is modified in place to resize it
773762
for in input tensor.
774763
"""
775-
776764
all_objects = []
777765
all_queues = []
778-
779766
_, m_height, m_width, _ = self.input_details['shape']
780-
i_width, i_height = image.size
781767

782768
# Potentially resize & pipeline a number of tiles
783769
for rs_image, rs_loc in self._get_tiles(options, image):
@@ -808,7 +794,7 @@ def _process_image(self,
808794
boxes, class_ids, scores, count = self._decode_result(result, score_threshold)
809795

810796
logging.debug("BBox scaling params: {}x{}, ({},{}), {:.2f}x{:.2f}".
811-
format(m_width, m_height,*rs_loc))
797+
format(m_width, m_height, *rs_loc))
812798

813799
# Create Objects for each valid result
814800
for i in range(int(count[0])):
@@ -817,14 +803,11 @@ def _process_image(self,
817803

818804
ymin, xmin, ymax, xmax = boxes[0][i]
819805

820-
bbox = detect.BBox(xmin=max(xmin, 0.0),
821-
ymin=max(ymin, 0.0),
822-
xmax=min(xmax, 1.0),
823-
ymax=min(ymax, 1.0)).\
824-
scale(m_width, m_height).\
825-
translate(rs_loc[0], rs_loc[1]).\
826-
scale(rs_loc[2], rs_loc[3])
827-
806+
bbox = detect.BBox(xmin=(max(xmin, 0.0)*m_width + rs_loc[0])*rs_loc[2],
807+
ymin=(max(ymin, 0.0)*m_height + rs_loc[1])*rs_loc[3],
808+
xmax=(min(xmax, 1.0)*m_width + rs_loc[0])*rs_loc[2],
809+
ymax=(min(ymax, 1.0)*m_height + rs_loc[1])*rs_loc[3])
810+
828811
all_objects.append(detect.Object(id=int(class_ids[0][i]),
829812
score=float(scores[0][i]),
830813
bbox=bbox.map(int)))

0 commit comments

Comments
 (0)