MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / AsyncPredictor

Class AsyncPredictor

demo/predictor.py:136–224  ·  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

134
135
136class AsyncPredictor:
137 """
138 A predictor that runs the model asynchronously, possibly on >1 GPUs.
139 Because rendering the visualization takes considerably amount of time,
140 this helps improve throughput a little bit when rendering videos.
141 """
142
143 class _StopToken:
144 pass
145
146 class _PredictWorker(mp.Process):
147 def __init__(self, cfg, task_queue, result_queue):
148 self.cfg = cfg
149 self.task_queue = task_queue
150 self.result_queue = result_queue
151 super().__init__()
152
153 def run(self):
154 predictor = DefaultPredictor(self.cfg)
155
156 while True:
157 task = self.task_queue.get()
158 if isinstance(task, AsyncPredictor._StopToken):
159 break
160 idx, data = task
161 result = predictor(data)
162 self.result_queue.put((idx, result))
163
164 def __init__(self, cfg, num_gpus: int = 1):
165 """
166 Args:
167 cfg (CfgNode):
168 num_gpus (int): if 0, will run on CPU
169 """
170 num_workers = max(num_gpus, 1)
171 self.task_queue = mp.Queue(maxsize=num_workers * 3)
172 self.result_queue = mp.Queue(maxsize=num_workers * 3)
173 self.procs = []
174 for gpuid in range(max(num_gpus, 1)):
175 cfg = cfg.clone()
176 cfg.defrost()
177 cfg.MODEL.DEVICE = "cuda:{}".format(gpuid) if num_gpus > 0 else "cpu"
178 self.procs.append(
179 AsyncPredictor._PredictWorker(cfg, self.task_queue, self.result_queue)
180 )
181
182 self.put_idx = 0
183 self.get_idx = 0
184 self.result_rank = []
185 self.result_data = []
186
187 for p in self.procs:
188 p.start()
189 atexit.register(self.shutdown)
190
191 def put(self, image):
192 self.put_idx += 1
193 self.task_queue.put((self.put_idx, image))

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected