Torch gets frozen when using multiprocessing.Process in python

Santhoshnumberone: <p>I am trying to run video through <a href="https://github.com/ayooshkathuria/pytorch-yolo-v3/blob/master/video_demo.py" rel="nofollow noopener">YoloV3</a></p>
<p>using this post as reference <a href="https://towardsdatascience.com/a-hands-on-guide-to-multiprocessing-in-python-48b59bfcc89e" rel="nofollow noopener">A Hands on Guide to Multiprocessing in Python</a></p>
<p>I took out the part where the predictions happen from inside a loop and placed it in a function</p>
<pre><code class="lang-auto">def yolo_detect1(CUDA,inp_dim,frame,confidence,num_classes,frames,nms_thesh):
 
    img, orig_im, dim = prep_image(frame, inp_dim)
    print(img, orig_im, dim)    
    im_dim = torch.FloatTensor(dim).repeat(1,2)                        
    print('MIDDLE')
    if CUDA:
        im_dim = im_dim.cuda()
        img = img.cuda()
    
    with torch.no_grad():   
        output = model(Variable(img), CUDA)
    output = write_results(output, confidence, num_classes, nms = True, nms_conf = nms_thesh)
    
    im_dim = im_dim.repeat(output.size(0), 1)
    scaling_factor = torch.min(inp_dim/im_dim,1)[0].view(-1,1)
    
    output[:,[1,3]] -= (inp_dim - scaling_factor*im_dim[:,0].view(-1,1))/2
    output[:,[2,4]] -= (inp_dim - scaling_factor*im_dim[:,1].view(-1,1))/2
    
    output[:,1:5] /= scaling_factor

    for i in range(output.shape[0]):
        output[i, [1,3]] = torch.clamp(output[i, [1,3]], 0.0, im_dim[i,0])
        output[i, [2,4]] = torch.clamp(output[i, [2,4]], 0.0, im_dim[i,1])
    
    for x in enumerate(output):
        write(x[1], orig_im)

    print('DONE')
    cv2.imshow('frame1',orig_im)
    return orig_im
</code></pre>
<p>Loop</p>
<pre><code class="lang-auto">time_interval = 15
processes0 = []
processes1 = []
start_time1 = time.time()
frames = 0
start = time.time()
selection_setter = 0 
while cap.isOpened():
        
        ret, frame = cap.read()
        if ret:
            
            current_time = time.time()
            time_difference = current_time - start_time1
            process_selector = (int(time_difference)//time_interval)%2

            # odd
            if process_selector == 1:
                selection_setter = 2
            elif process_selector == 0:
                selection_setter = 1

            print('process_selector ',process_selector)
            print('selection_setter ',selection_setter)
            if selection_setter == 1:
 
                try:
                    if processes1:
                        for process in processes1:
                            process.join()
                    p = Process(target=yolo_detect1, args=(torch,CUDA,inp_dim,frame,confidence,num_classes,frames,nms_thesh))
                    processes0.append(p)
                    p.start()
                except Exception as e:
                    print(traceback.format_exc())
                
            elif selection_setter == 2:
                if processes0:
                    for process in processes0:
                        process.join()
                        
                p = Process(target=yolo_detect2, args=(torch,CUDA,inp_dim,frame,confidence,num_classes,frames,nms_thesh))
                processes1.append(p)
                p.start()

            # yolo_result = yolo_detect(frame,confidence,num_classes,frames,nms_thesh)
            # list(map(lambda x: write(x, orig_im), output))
            
            # cv2.imshow("frame", yolo_result)
            key = cv2.waitKey(1)
            if key &amp; 0xFF == ord('q'):
                break
            frames += 1
            # print("FPS of the video is {:5.2f}".format( frames / (time.time() - start)))
            
        else:
            break
</code></pre>
<p>The code stops without any warning, I tried to find out where the code got stuck</p>
<p>I altered this <a href="https://github.com/ayooshkathuria/pytorch-yolo-v3/blob/master/video_demo.py#L31" rel="nofollow noopener">prep_image</a> to know what exactly was the issue</p>
<pre><code class="lang-auto">def prep_image(img, inp_dim):
    """
    Prepare image for inputting to the neural network. 
    
    Returns a Variable 
    """
    print('PREP IMAGE')
    try:
        orig_im = img
        dim = orig_im.shape[1], orig_im.shape[0]
        print('HERE1')
        img = (letterbox_image(orig_im, (inp_dim, inp_dim)))
        print('HERE2')
        img_ = img[:,:,::-1].transpose((2,0,1)).copy()
        print('HERE3')
        img_ = torch.from_numpy(img_).float().div(255.0).unsqueeze(0)
        print('HERE4')
        print('RETURN VALUES')
        return img_, orig_im, dim
    except Exception:
        print(traceback.format_exc())
</code></pre>
<p>But this is all the code outputs and it gets stuck</p>
<pre><code class="lang-auto">process_selector  0
selection_setter  1
STARTED
PREP IMAGE
HERE1
HERE2
HERE3
process_selector  0
selection_setter  1
STARTED
PREP IMAGE
HERE1
HERE2
HERE3
</code></pre>
<p>The function is getting stuck at this line <code>img_ = torch.from_numpy(img_).float().div(255.0).unsqueeze(0)</code> inside <code>prep_image</code></p>

ArbFay: <p>Same issue.</p>
<p><a class="mention" href="/u/santhoshnumberone">@Santhoshnumberone</a> did you find a solution?</p>