imagededup
imagededup copied to clipboard
multiprocessing code looks to be broken on windows
phasher = PHash()
encodings = phasher.encode_images(image_dir)
duplicates = phasher.find_duplicates(image_dir)
->
2023-05-03 08:55:03,150: INFO Start: Calculating hashes...
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\runpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "E:\Projects\pydedup\pydedup.py", line 52, in <module>
encodings = phasher.encode_images(image_dir)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\methods\hashing.py", line 161, in encode_images
hashes = parallelise(function=self.encode_image, data=files, verbose=self.verbose, num_workers=num_enc_workers)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\utils\general_utils.py", line 65, in parallelise
pool = Pool(processes=num_workers)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\context.py", line 119, in Pool
return Pool(processes, initializer, initargs, maxtasksperchild,
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\pool.py", line 215, in __init__
self._repopulate_pool()
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\pool.py", line 306, in _repopulate_pool
return self._repopulate_pool_static(self._ctx, self.Process,
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\pool.py", line 329, in _repopulate_pool_static
w.start()
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\context.py", line 336, in _Popen
return Popen(process_obj)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
and then
Traceback (most recent call last):
File "E:\Projects\pydedup\pydedup.py", line 52, in <module>
duplicates = phasher.find_duplicates(image_dir)
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\methods\hashing.py", line 303, in find_duplicates
result = self._find_duplicates_dir(
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\methods\hashing.py", line 364, in _find_duplicates_dir
results = self._find_duplicates_dict(
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\methods\hashing.py", line 230, in _find_duplicates_dict
result_set = HashEval(
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\handlers\search\retrieval.py", line 82, in __init__
self._fetch_nearest_neighbors_bktree()
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\handlers\search\retrieval.py", line 153, in _fetch_nearest_neighbors_bktree
built_tree = BKTree(self.test, self.distance_invoker) # construct bktree
File "C:\Users\jbell\mambaforge\envs\pydedup\lib\site-packages\imagededup\handlers\search\bktree.py", line 38, in __init__
self.ROOT = self.all_keys[0]
IndexError: list index out of range
apparently this is a "feature" of the underlying multiprocessing library, changing my code to
if __name__ == '__main__':
...
phasher = PHash()
encodings = phasher.encode_images(image_dir)
duplicates = phasher.find_duplicates(image_dir)
fixes it. might want to drop a warning in the docs tho.
Thanks for the report, will make a note in the documentation.
@jbellis, I'm sorry, could you help my smooth brain understand what I have to modify from the workflow to fix this?
from imagededup.methods import PHash
phasher = PHash()
encodings = phasher.encode_images(image_dir='path/to/image/directory')
duplicates = phasher.find_duplicates(encoding_map=encodings)
from imagededup.utils import plot_duplicates
plot_duplicates(image_dir='path/to/image/directory',
duplicate_map=duplicates,
filename='ukbench00120.jpg')
Tried to integrate what you wrote but I don't understand where to put it and what to replace
if __name__ == '__main__':
...
phasher = PHash()
encodings = phasher.encode_images(image_dir)
duplicates = phasher.find_duplicates(image_dir)
Just put everything (or everything except the imports, either way) in an "if name == 'main'" block. https://stackoverflow.com/questions/419163/what-does-if-name-main-do
this prevents multiprocessing from entering an infinite loop when it imports the script in the subprocesses: https://stackoverflow.com/questions/20222534/python-multiprocessing-on-windows-if-name-main