前提・実現したいこと
Python・プログラミング初心者なので、質問が的を得ていなかったらすみません。
画像から詩を作成する機械学習のGitHub上のプロジェクト(こちら)を試しており、
おそらく並列処理(multiprocessing)の過程でランタイムエラーが発生し、解消できなくて困っています。
発生している問題・エラーメッセージ
(py27) C:\Users\admin\img2poem\code\src>python test.py Loading Extracting Feature Module... Traceback (most recent call last): File "test.py", line 8, in <module> extract_feature = nn_process.create('extract_feature') File "C:\Users\admin\img2poem\code\src\nn_process.py", line 19, in create proc.start() File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\process.py", line 130, in start self._popen = Popen(self) File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\forking.py", line 277, in __init__ dump(process_obj, to_child, HIGHEST_PROTOCOL) File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\forking.py", line 199, in dump ForkingPickler(file, protocol).dump(obj) File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 224, in dump self.save(obj) File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 425, in save_reduce save(state) File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 655, in save_dict self._batch_setitems(obj.iteritems()) File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 687, in _batch_setitems save(v) File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "C:\Users\admin\Anaconda3\envs\py27\lib\pickle.py", line 754, in save_global (obj, module, name)) pickle.PicklingError: Can't pickle <function pipe_process at 0x000000000358A978>: it's not found as nn_process.pipe_process (py27) C:\Users\admin\img2poem\code\src>Loading Extracting Feature Module... Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\forking.py", line 380, in main prepare(preparation_data) File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\forking.py", line 510, in prepare '__parents_main__', file, path_name, etc File "C:\Users\admin\img2poem\code\src\test.py", line 8, in <module> extract_feature = nn_process.create('extract_feature') File "C:\Users\admin\img2poem\code\src\nn_process.py", line 19, in create proc.start() File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\process.py", line 130, in start self._popen = Popen(self) File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\forking.py", line 258, in __init__ cmd = get_command_line() + [rhandle] File "C:\Users\admin\Anaconda3\envs\py27\lib\multiprocessing\forking.py", line 358, in get_command_line is not going to be frozen to produce a Windows executable.''') RuntimeError: Attempt to start a new process before the current process has finished its bootstrapping phase. This probably means that you are on Windows 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 a Windows executable.
該当のソースコード
Python
1import nn_process 2import time 3import os 4import sys 5 6print ('Loading Extracting Feature Module...') 7extract_feature = nn_process.create('extract_feature') 8print ('Loading Generating Poem Module...') 9generate_poem = nn_process.create('generate_poem') 10 11# default path to an image 12DEFAULT_PATH = '../images/test.jpg' 13 14if sys.version_info[0] >= 3: 15 raw_input = input 16else: 17 FileNotFoundError = IOError 18 19def get_poem(image_file): 20 """Generate a poem from the image whose filename is `image_file` 21 Parameters 22 ---------- 23 image_file : str 24 Path to the input image 25 Returns 26 ------- 27 str 28 Generated Poem 29 """ 30 # Check whether the file exists 31 assert os.path.exists(image_file), FileNotFoundError( 32 'File `{}` not found.'.format(image_file)) 33 assert not os.path.isdir(image_file), FileNotFoundError( 34 'The path should be a filename instead of `{}`.'.format(image_file)) 35 img_feature = extract_feature(image_file) 36 return generate_poem(img_feature) 37 38if __name__ == '__main__': 39 while 1: 40 try: 41 s = raw_input("Please input the path to an image [default='%s']: " % DEFAULT_PATH) 42 if not s: 43 # s is empty, and use the DEFAULT_PATH 44 s = DEFAULT_PATH 45 tic = time.time() 46 print ('\n' + get_poem(s)[0].replace('\n', '\n') + '\n') 47 print ("Cost Time: %f" % (time.time() - tic)) 48 except Exception as e: 49 print (e)
試したこと
こちらを見て、if name == 'main':の下に freeze_support()を入れたり、
こちらを真似して、if name == 'main':以下のコードを関数main()として外で定義してから実行したり、
上記を組み合わせたりしてみたのですが、同じエラーが出続けます。
補足情報(FW/ツールのバージョンなど)
Windows10, Anaconda3
を使っています。
Python2.7の仮想環境を作って、Anaconda Promptから実行しています
どうぞよろしくお願いいたします。