My function works fine by itself but not when I run it as a worker












1















I am trying to run multiple Yolos in parallel in Python.



I have a function that I am trying to use as the worker. something like this:



import darknet as dn
def yolo(input_queue, net, meta):
while True:
task = input_queue.get_nowait()
c = task.shape[0]
h = task.shape[1]
w = task.shape[2]
task = (task/255.0)
task = task.flatten()
new_arr = np.copy(task).astype(np.float32)
new_arr = np.ctypeslib.as_ctypes(new_arr)
im = dn.IMAGE(w,h,c,new_arr)
dn.rgbgr_image(im)

print(dn.detect(net, meta, im))


You can find darknet file here.



Let's don't talk about the way I convert the array to a ctype array. it has been discussed here.



If I run this function as a normal function works like a charm. But if I run it as a worker like this:



p = Process(target=yolo, args=(input_queue, net, meta))
p.start()


it goes all the way down to the last line then it gives me this error:



CUDA Error: initialization error
python: ./src/cuda.c:36: check_error: Assertion `0' failed.


I couldn't figure out why exactly this error happens.
can anyone help? what's the difference between these two way of using this function that cause this problem?










share|improve this question




















  • 1





    Check this out.

    – GeeTransit
    Jan 3 at 1:18











  • You may may need to put the "entry point" of the main script within an if __name__ == '__main__': conditional as described in the "Safe importing of main module" section of the multiprocessing Programming guidelines documentation.

    – martineau
    Jan 3 at 1:19











  • @GeeTransit it is different but answer by sagarwal gave me the idea to solve the problem

    – Ehsan Fathi
    Jan 3 at 22:25


















1















I am trying to run multiple Yolos in parallel in Python.



I have a function that I am trying to use as the worker. something like this:



import darknet as dn
def yolo(input_queue, net, meta):
while True:
task = input_queue.get_nowait()
c = task.shape[0]
h = task.shape[1]
w = task.shape[2]
task = (task/255.0)
task = task.flatten()
new_arr = np.copy(task).astype(np.float32)
new_arr = np.ctypeslib.as_ctypes(new_arr)
im = dn.IMAGE(w,h,c,new_arr)
dn.rgbgr_image(im)

print(dn.detect(net, meta, im))


You can find darknet file here.



Let's don't talk about the way I convert the array to a ctype array. it has been discussed here.



If I run this function as a normal function works like a charm. But if I run it as a worker like this:



p = Process(target=yolo, args=(input_queue, net, meta))
p.start()


it goes all the way down to the last line then it gives me this error:



CUDA Error: initialization error
python: ./src/cuda.c:36: check_error: Assertion `0' failed.


I couldn't figure out why exactly this error happens.
can anyone help? what's the difference between these two way of using this function that cause this problem?










share|improve this question




















  • 1





    Check this out.

    – GeeTransit
    Jan 3 at 1:18











  • You may may need to put the "entry point" of the main script within an if __name__ == '__main__': conditional as described in the "Safe importing of main module" section of the multiprocessing Programming guidelines documentation.

    – martineau
    Jan 3 at 1:19











  • @GeeTransit it is different but answer by sagarwal gave me the idea to solve the problem

    – Ehsan Fathi
    Jan 3 at 22:25
















1












1








1








I am trying to run multiple Yolos in parallel in Python.



I have a function that I am trying to use as the worker. something like this:



import darknet as dn
def yolo(input_queue, net, meta):
while True:
task = input_queue.get_nowait()
c = task.shape[0]
h = task.shape[1]
w = task.shape[2]
task = (task/255.0)
task = task.flatten()
new_arr = np.copy(task).astype(np.float32)
new_arr = np.ctypeslib.as_ctypes(new_arr)
im = dn.IMAGE(w,h,c,new_arr)
dn.rgbgr_image(im)

print(dn.detect(net, meta, im))


You can find darknet file here.



Let's don't talk about the way I convert the array to a ctype array. it has been discussed here.



If I run this function as a normal function works like a charm. But if I run it as a worker like this:



p = Process(target=yolo, args=(input_queue, net, meta))
p.start()


it goes all the way down to the last line then it gives me this error:



CUDA Error: initialization error
python: ./src/cuda.c:36: check_error: Assertion `0' failed.


I couldn't figure out why exactly this error happens.
can anyone help? what's the difference between these two way of using this function that cause this problem?










share|improve this question
















I am trying to run multiple Yolos in parallel in Python.



I have a function that I am trying to use as the worker. something like this:



import darknet as dn
def yolo(input_queue, net, meta):
while True:
task = input_queue.get_nowait()
c = task.shape[0]
h = task.shape[1]
w = task.shape[2]
task = (task/255.0)
task = task.flatten()
new_arr = np.copy(task).astype(np.float32)
new_arr = np.ctypeslib.as_ctypes(new_arr)
im = dn.IMAGE(w,h,c,new_arr)
dn.rgbgr_image(im)

print(dn.detect(net, meta, im))


You can find darknet file here.



Let's don't talk about the way I convert the array to a ctype array. it has been discussed here.



If I run this function as a normal function works like a charm. But if I run it as a worker like this:



p = Process(target=yolo, args=(input_queue, net, meta))
p.start()


it goes all the way down to the last line then it gives me this error:



CUDA Error: initialization error
python: ./src/cuda.c:36: check_error: Assertion `0' failed.


I couldn't figure out why exactly this error happens.
can anyone help? what's the difference between these two way of using this function that cause this problem?







python multiprocessing yolo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 2:15









talonmies

59.8k17132199




59.8k17132199










asked Jan 3 at 1:05









Ehsan FathiEhsan Fathi

378




378








  • 1





    Check this out.

    – GeeTransit
    Jan 3 at 1:18











  • You may may need to put the "entry point" of the main script within an if __name__ == '__main__': conditional as described in the "Safe importing of main module" section of the multiprocessing Programming guidelines documentation.

    – martineau
    Jan 3 at 1:19











  • @GeeTransit it is different but answer by sagarwal gave me the idea to solve the problem

    – Ehsan Fathi
    Jan 3 at 22:25
















  • 1





    Check this out.

    – GeeTransit
    Jan 3 at 1:18











  • You may may need to put the "entry point" of the main script within an if __name__ == '__main__': conditional as described in the "Safe importing of main module" section of the multiprocessing Programming guidelines documentation.

    – martineau
    Jan 3 at 1:19











  • @GeeTransit it is different but answer by sagarwal gave me the idea to solve the problem

    – Ehsan Fathi
    Jan 3 at 22:25










1




1





Check this out.

– GeeTransit
Jan 3 at 1:18





Check this out.

– GeeTransit
Jan 3 at 1:18













You may may need to put the "entry point" of the main script within an if __name__ == '__main__': conditional as described in the "Safe importing of main module" section of the multiprocessing Programming guidelines documentation.

– martineau
Jan 3 at 1:19





You may may need to put the "entry point" of the main script within an if __name__ == '__main__': conditional as described in the "Safe importing of main module" section of the multiprocessing Programming guidelines documentation.

– martineau
Jan 3 at 1:19













@GeeTransit it is different but answer by sagarwal gave me the idea to solve the problem

– Ehsan Fathi
Jan 3 at 22:25







@GeeTransit it is different but answer by sagarwal gave me the idea to solve the problem

– Ehsan Fathi
Jan 3 at 22:25














1 Answer
1






active

oldest

votes


















0














Thanks to this I found the problem is that you can't share the context in CUDA between processes with different PIDs which makes sense. So I found that if I run related things (in my case, net) in the same process then it works okay. before this I was loading weights before I start the worker.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015121%2fmy-function-works-fine-by-itself-but-not-when-i-run-it-as-a-worker%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks to this I found the problem is that you can't share the context in CUDA between processes with different PIDs which makes sense. So I found that if I run related things (in my case, net) in the same process then it works okay. before this I was loading weights before I start the worker.






    share|improve this answer




























      0














      Thanks to this I found the problem is that you can't share the context in CUDA between processes with different PIDs which makes sense. So I found that if I run related things (in my case, net) in the same process then it works okay. before this I was loading weights before I start the worker.






      share|improve this answer


























        0












        0








        0







        Thanks to this I found the problem is that you can't share the context in CUDA between processes with different PIDs which makes sense. So I found that if I run related things (in my case, net) in the same process then it works okay. before this I was loading weights before I start the worker.






        share|improve this answer













        Thanks to this I found the problem is that you can't share the context in CUDA between processes with different PIDs which makes sense. So I found that if I run related things (in my case, net) in the same process then it works okay. before this I was loading weights before I start the worker.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 22:31









        Ehsan FathiEhsan Fathi

        378




        378
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015121%2fmy-function-works-fine-by-itself-but-not-when-i-run-it-as-a-worker%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith