Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while using mtcnn with Multiprocessing lib "multiprocessing" and "concurrent.futures" - cv2.resize() error #97

Open
torta24x opened this issue Aug 2, 2020 · 0 comments

Comments

@torta24x
Copy link

torta24x commented Aug 2, 2020

@ipazc
While I was using mtcnn with multiprocessing lib to do parallel inferences,
the code gave an ERROR with opencv-python version 4.2.0.32 and above

Error

File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/process.py", line 476, in _chain_from_iterable_of_lists
    for element in iterable:
  File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/_base.py", line 586, in result_iterator
    yield fs.pop().result()
  File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/_base.py", line 425, in result
    return self.__get_result()
  File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

Implementation
I have tried two ways of multiprocessing on which I received similar error:

 with concurrent.futures.ProcessPoolExecutor() as ex:
        result = ex.map(extract_face, file_list)

#------------------AND -------------------

p1= multiprocessing.Process(target=extract_face,args=(file_list[0],))
p2= multiprocessing.Process(target=extract_face,args=(file_list[1],))
p1.start()
p2.start()

p1.join()
p2.join()

Solution
Eventually, I was able to figure out the reason, though I am not sure why is this problem occured.
I will be glad if someone could give an explanation for this.

Error occurred due to cv2.resize operation on line:124 in mtcnn.py file
Older Code

    def __scale_image(image, scale: float):
       
        height, width, _ = image.shape

        width_scaled = int(np.ceil(width * scale))
        height_scaled = int(np.ceil(height * scale))

        im_data = cv2.resize(image, (width_scaled, height_scaled), interpolation=cv2.INTER_AREA)

        # Normalize the image's pixels
        im_data_normalized = (im_data - 127.5) * 0.0078125

        return im_data_normalized

Modified Code

    def __scale_image(image, scale: float):
        
        height, width, _ = image.shape

        width_scaled = int(np.ceil(width * scale))
        height_scaled = int(np.ceil(height * scale))
        
        # ---------Modification: Resizing via PIL-----------
        i_m = Image.fromarray(image)
        im = im.resize((width_scaled,height_scaled))
        im_data = np.asarray(im)
        
        #----------Remove cv2.resize()-----------
        # im_data = cv2.resize(image, (width_scaled, height_scaled), interpolation=cv2.INTER_AREA)

        # Normalize the image's pixels
        im_data_normalized = (im_data - 127.5) * 0.0078125

        return im_data_normalized

Is this the correct way to do it, or am I missing something?
Thanks

BONUS
A similar error occurs with mtcnn implementation by PFLD and others (tf and pytorch) implementations on Github
PFLD: https://github.com/guoqiangqi/PFLD.

@torta24x torta24x changed the title [BUG] Issue while using mtcnn with Multiprocessing lib "multiprocessing" and "concurrent.futures" Issue while using mtcnn with Multiprocessing lib "multiprocessing" and "concurrent.futures" - cv2.resize() error Aug 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant