MCPcopy Create free account
hub / github.com/UVA-Computer-Vision-Lab/FrameINO / AsyncPredictor

Class AsyncPredictor

preprocess/oneformer_code/demo/predictor.py:102–190  ·  view source on GitHub ↗

A predictor that runs the model asynchronously, possibly on >1 GPUs. Because rendering the visualization takes considerably amount of time, this helps improve throughput a little bit when rendering videos.

Source from the content-addressed store, hash-verified

100
101
102class AsyncPredictor:
103 """
104 A predictor that runs the model asynchronously, possibly on >1 GPUs.
105 Because rendering the visualization takes considerably amount of time,
106 this helps improve throughput a little bit when rendering videos.
107 """
108
109 class _StopToken:
110 pass
111
112 class _PredictWorker(mp.Process):
113 def __init__(self, cfg, task_queue, result_queue):
114 self.cfg = cfg
115 self.task_queue = task_queue
116 self.result_queue = result_queue
117 super().__init__()
118
119 def run(self):
120 predictor = DefaultPredictor(self.cfg)
121
122 while True:
123 task = self.task_queue.get()
124 if isinstance(task, AsyncPredictor._StopToken):
125 break
126 idx, data = task
127 result = predictor(data)
128 self.result_queue.put((idx, result))
129
130 def __init__(self, cfg, num_gpus: int = 1):
131 """
132 Args:
133 cfg (CfgNode):
134 num_gpus (int): if 0, will run on CPU
135 """
136 num_workers = max(num_gpus, 1)
137 self.task_queue = mp.Queue(maxsize=num_workers * 3)
138 self.result_queue = mp.Queue(maxsize=num_workers * 3)
139 self.procs = []
140 for gpuid in range(max(num_gpus, 1)):
141 cfg = cfg.clone()
142 cfg.defrost()
143 cfg.MODEL.DEVICE = "cuda:{}".format(gpuid) if num_gpus > 0 else "cpu"
144 self.procs.append(
145 AsyncPredictor._PredictWorker(cfg, self.task_queue, self.result_queue)
146 )
147
148 self.put_idx = 0
149 self.get_idx = 0
150 self.result_rank = []
151 self.result_data = []
152
153 for p in self.procs:
154 p.start()
155 atexit.register(self.shutdown)
156
157 def put(self, image):
158 self.put_idx += 1
159 self.task_queue.put((self.put_idx, image))

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected