【发布时间】:2021-06-29 00:40:10
【问题描述】:
我正在为水下航行器编写推理系统,有人告诉我研究异步以改进对象跟踪。这个想法是有一个 tflite 对象检测模型,该模型检测对象并为它检测到的每个对象返回一个框/坐标,然后均值偏移(或其他一些跟踪算法然后用于跟踪对象)。但是,tflite 模型大约需要 1 秒来进行检测,这太慢了。所以我想在 meanshift 跟踪时在单独的线程中运行它,每当 tflite 模型完成时,它都会更新要跟踪的框。这样我会假设我们可以顺利检测和跟踪。
我发现异步有点棘手,不能完全正确。出于测试目的,我创建了一个延迟为 5 秒的推理函数,以清楚地模拟推理所需的时间,并创建了一个连续运行以模拟均值偏移的跟踪函数。这是我目前所拥有的:
async def inference(x):
await asyncio.sleep(x)
return "===========Inference Done================= "
def tracking():
print("tracking...")
def start_loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever()
new_loop = asyncio.new_event_loop()
t = Thread(target=start_loop, args=(new_loop,))
t.start()
# start the object detection model before
box = asyncio.run_coroutine_threadsafe(inference(5), new_loop)
while True:
if box: # if the object detection has detected an object and return a bounding box, track it
tracking()
if box.done(): # if the tflite has completed its detection, start the next detection
box = asyncio.run_coroutine_threadsafe(inference(5), new_loop)
print(box.result())
这里的预期输出是tracking... 连续打印,===========Inference Done================= 每 5 秒打印一次。但是,tracking... 会连续运行 5 秒,然后它开始这样做:
tracking...
tracking...
tracking...
tracking...
tracking...
tracking...
tracking...
tracking...
===========Inference Done=================
tracking...
===========Inference Done=================
tracking...
我该如何解决这个问题?
【问题讨论】:
-
你好,你想用 asyncio 实现什么?如果您的
find box ops受 CPU 限制,则将 asyncio 保持在具有大量 CPU 限制操作的同一进程中是不正确的。 -
好吧,我想要的是 while 循环不间断地运行,并且我想调用一个单独的函数,而 while 循环不必等待。因此,while 循环只是运行,并且推理函数只要准备好就会更新。希望这是有道理的。
标签: python opencv python-asyncio mean-shift