elsampsa / valkka-live

OpenSource Video Surveillance Program
GNU Affero General Public License v3.0
31 stars 12 forks source link

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method #13

Closed hijinkim closed 1 year ago

hijinkim commented 1 year ago

Thanks for your work I'm trying to modify 'machine vision' tab using my own model. When I initialize my model like this

class myDetector(Analyzer):
    def __init__(self, **kwargs):
        ...model init...

and i got error RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method

So how can I solve this error?

elsampsa commented 1 year ago

Can you please show me where you import torch (or whatever DL library it is that uses cude) ?

hijinkim commented 1 year ago

Can you please show me where you import torch (or whatever DL library it is that uses cude) ?

I created my own directory objectdetector within valkka/valkka-live/mvision/ In mvision/objectdetector, I have __init__.py and base.py.

base.py

import torch

class myDetector(Analyzer):
    def __init__(self, **kwargs):
        self.device = torch.device('cuda:0')
        # load pretrained model
hijinkim commented 1 year ago

I think I've found the cause. I load pretrained model using python package, and I import the package at the top of the source code (outside of Analyzer class). I modified it to import within the Analyzer class and confirmed that the code is working correctly. Thank you :smiley: