質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

並列処理

複数の計算が同時に実行される手法

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

5319閲覧

並列処理でのランタイムエラー( if __name__ == '__main__': freeze_support()...)

OMIM

総合スコア6

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

並列処理

複数の計算が同時に実行される手法

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2019/09/09 13:58

編集2019/09/10 07:40

前提・実現したいこと

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から実行しています

どうぞよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

quickquip

2019/09/10 03:07 編集

Tracebackを隠しているのはなぜでしょう
OMIM

2019/09/10 07:45

すみません、Tracebackをすべて載せました。 よく見たら、いちどpickle.PicklingErrorが出ていて、その後再度処理がなされているようです
guest

回答1

0

解決するかわかりませんが

Python

1import nn_process 2import time 3import os 4import sys 5 6def get_poem(image_file): 7 """Generate a poem from the image whose filename is `image_file` 8 Parameters 9 ---------- 10 image_file : str 11 Path to the input image 12 Returns 13 ------- 14 str 15 Generated Poem 16 """ 17 # Check whether the file exists 18 assert os.path.exists(image_file), FileNotFoundError( 19 'File `{}` not found.'.format(image_file)) 20 assert not os.path.isdir(image_file), FileNotFoundError( 21 'The path should be a filename instead of `{}`.'.format(image_file)) 22 img_feature = extract_feature(image_file) 23 return generate_poem(img_feature) 24 25if __name__ == '__main__': 26 freeze_support() 27 28 print ('Loading Extracting Feature Module...') 29 extract_feature = nn_process.create('extract_feature') 30 print ('Loading Generating Poem Module...') 31 generate_poem = nn_process.create('generate_poem') 32 33 # default path to an image 34 DEFAULT_PATH = '../images/test.jpg' 35 36 if sys.version_info[0] >= 3: 37 raw_input = input 38 else: 39 FileNotFoundError = IOError 40 41 while 1: 42 try: 43 s = raw_input("Please input the path to an image [default='%s']: " % DEFAULT_PATH) 44 if not s: 45 # s is empty, and use the DEFAULT_PATH 46 s = DEFAULT_PATH 47 tic = time.time() 48 print ('\n' + get_poem(s)[0].replace('\n', '\n') + '\n') 49 print ("Cost Time: %f" % (time.time() - tic)) 50 except Exception as e: 51 print (e) 52

としたら現象変わりますか?

投稿2019/09/28 23:49

nomuken

総合スコア1627

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問