为什么在继承multiprocessing.process的类中不能在__init__方法下初始化cuda模型



这是我的代码:

from MyDetector import Helmet_Detector
from multiprocessing import Process
class Processor(Process):
def __init__(self):
super().__init__()
self.helmet_detector = Helmet_Detector()
def run(self):
print(111)

if __name__ == '__main__':
p=Processor()
p.start()

正如您所看到的,类"Processor"继承了多处理。过程,并且Helmet_Detector是使用cuda的YOLO模型。但当我运行它时,错误发生如下:

THCudaCheck FAIL file=C:w1stmp_conda_3.7_075911condaconda-bldpytorch_1579075223148worktorch/csrc/generic/StorageSharing.cpp line=245 error=71 : operation not supported
Traceback (most recent call last):
File "E:/python-tasks/WHU-CSTECH/Processor.py", line 17, in <module>
p.start()
File "C:Anacondalibmultiprocessingprocess.py", line 112, in start
self._popen = self._Popen(self)
File "C:Anacondalibmultiprocessingcontext.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:Anacondalibmultiprocessingcontext.py", line 322, in _Popen
return Popen(process_obj)
File "C:Anacondalibmultiprocessingpopen_spawn_win32.py", line 89, in __init__
reduction.dump(process_obj, to_child)
File "C:Anacondalibmultiprocessingreduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:Anacondalibsite-packagestorchmultiprocessingreductions.py", line 242, in reduce_tensor
event_sync_required) = storage._share_cuda_()
RuntimeError: cuda runtime error (71) : operation not supported at C:w1stmp_conda_3.7_075911condaconda-bldpytorch_1579075223148worktorch/csrc/generic/StorageSharing.cpp:245

然后我尝试初始化运行中的Helmet_Detector方法:

def run(self):
print(111)
self.helmet_detector = Helmet_Detector()

没有发生错误。有人能告诉我原因吗?我该如何解决这个问题?非常感谢。

发生错误是因为在python中,多处理要求Process类对象是可拾取的,以便将数据传输到正在创建的进程,即对象的串行化和反序列化。为了解决这个问题,建议懒惰地实例化Helmet_Detector对象(提示:在python中尝试属性(。

编辑:

根据@jodag的评论,您应该使用pytorch的多处理库,而不是标准的多处理程序库

示例:

import torch.multiprocessing as mp
class Processor(mp.Process):
.
.
.

相关内容

  • 没有找到相关文章

最新更新