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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

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

Q&A

解決済

1回答

1593閲覧

StyleGANの実行時にトレーニングデータを読み取れない。

spa

総合スコア52

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

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

0グッド

0クリップ

投稿2021/04/04 07:22

編集2021/04/06 01:30

前提・実現したいこと

http://cedro3.com/ai/stylegan/
こちらのサイトを参考にしながらgoogle colab で StyleGANのコーディングを勉強しているのですが、
トレーニングデータを読み込むところでエラーが発生してしまいます。
アドバイスよろしくお願いします。

(見本のサイトでは、url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' だったのですが、直接ファイルを読み取れないようだったので、一度ダウンロードして自分のgoogleDriveにファイルをアップロードして読み込むようにしました。)

発生している問題・エラーメッセージ

--------------------------------------------------------------------------- --------------------------------------------------------------------------- UnpicklingError Traceback (most recent call last) <ipython-input-84-b4d8cef9c989> in <module>() 45 46 if __name__ == "__main__": ---> 47 main() <ipython-input-84-b4d8cef9c989> in main() 14 url = 'https://drive.google.com/file/d/1Ee0irzmTrTKL5Czm_bpB_6mB2lVAfA5B/view?usp=sharing' # karras2019stylegan-ffhq-1024x1024.pkl 15 with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: ---> 16 _G, _D, Gs = pickle.load(f) 17 # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. 18 # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. UnpicklingError: invalid load key, '\x0a'.

該当のソースコード

python3

1from google.colab import drive 2drive.mount('/content/drive') 3 4cd drive/My Drive 5!git clone https://github.com/NVlabs/stylegan.git 6cd stylegan 7 8!pip install tensorflow==1.15 9!pip install tensorflow-gpu==1.15 10 11import os 12import pickle 13import numpy as np 14import PIL.Image 15import dnnlib 16import dnnlib.tflib as tflib 17import config 18 19def main(): 20 # Initialize TensorFlow. 21 tflib.init_tf() 22 23 # Load pre-trained network. 24 url = 'https://drive.google.com/file/d/1Ee0irzmTrTKL5Czm_bpB_6mB2lVAfA5B/view?usp=sharing' # karras2019stylegan-ffhq-1024x1024.pkl 25 with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: 26 _G, _D, Gs = pickle.load(f) 27 # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. 28 # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. 29 # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. 30 31 # Print network details. 32 Gs.print_layers() 33 34 # Pick latent vector. 35 rnd = np.random.RandomState(10) # seed = 10 36 latents0 = rnd.randn(1, Gs.input_shape[1]) 37 latents1 = rnd.randn(1, Gs.input_shape[1]) 38 latents2 = rnd.randn(1, Gs.input_shape[1]) 39 latents3 = rnd.randn(1, Gs.input_shape[1]) 40 latents4 = rnd.randn(1, Gs.input_shape[1]) 41 latents5 = rnd.randn(1, Gs.input_shape[1]) 42 latents6 = rnd.randn(1, Gs.input_shape[1]) 43 44 num_split = 39 # 2つのベクトルを39分割 45 for i in range(40): 46 latents = latents6+(latents0-latents6)*i/num_split 47 # Generate image. 48 fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) 49 images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) 50 51 # Save image. 52 os.makedirs(config.result_dir, exist_ok=True) 53 png_filename = os.path.join(config.result_dir, 'photo'+'{0:04d}'.format(i)+'.png') 54 PIL.Image.fromarray(images[0], 'RGB').save(png_filename) 55 56if __name__ == "__main__": 57 main() 58

補足情報(FW/ツールのバージョンなど)

新しく出たエラー

Downloading https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ ............ failed --------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-20-ece5f04dab83> in <module>() 45 46 if __name__ == "__main__": ---> 47 main() 1 frames /content/drive/My Drive/stylegan/dnnlib/util.py in open_url(url, cache_dir, num_attempts, verbose) 376 raise IOError("Google Drive virus checker nag") 377 if "Google Drive - Quota exceeded" in content_str: --> 378 raise IOError("Google Drive quota exceeded") 379 380 match = re.search(r'filename="([^"]*)"', res.headers.get("Content-Disposition", "")) OSError: Google Drive quota exceeded

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

質問に書かれているコードから、下記を変更してGoogleコラボで動かしたら、実行できました

python

1cd drive/My Drive 2!git clone https://github.com/NVlabs/stylegan.git 3cd stylegan 4 5!pip install tensorflow==1.15 6!pip install tensorflow-gpu==1.15

↓ 変更

python

1%cd drive/My Drive 2!git clone https://github.com/NVlabs/stylegan.git 3%cd stylegan 4 5!pip install tensorflow==1.15

.

python

1 url = 'https://drive.google.com/file/d/1Ee0irzmTrTKL5Czm_bpB_6mB2lVAfA5B/view?usp=sharing' # karras2019stylegan-ffhq-1024x1024.pkl

↓ 変更

python

1 url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl

【追記】 上記に加えて、さらに下記を行なっても、Googleコラボで実行できました

Googleドライブ直下に「datasets」というディレクトリを作り、そこに「karras2019stylegan-ffhq-1024x1024.pkl」(pretrained model)を置く

python

1 url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl 2 with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: 3 _G, _D, Gs = pickle.load(f)

↓ 変更

python

1 _G, _D, Gs = pickle.load(open('/content/drive/My Drive/datasets/karras2019stylegan-ffhq-1024x1024.pkl','rb'))

投稿2021/04/05 13:49

編集2021/04/06 06:19
jbpb0

総合スコア7651

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

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

spa

2021/04/06 01:35

回答ありがとうございます。 教えていただいたとおり変更したところ、ドライブのマウントからTensorFlowのインストールまでうまくいきました。 ですがまたエラーが出てしまいまして。わかるようでしたら教えていただけるとありがたいです。 エラーメッセージ OSError: Google Drive quota exceeded (このURLを参考に https://teratail.com/questions/276412 トレーニングデータを一度ダウンロードして自分のGoogleドライブにアップして回してみましたが同じエラーメッセージが出ました。)
jbpb0

2021/04/06 06:22 編集

https://www.koi.mashykom.com/dcgan.html#fd05 の「StyleGAN の Python 実装」に、 「「 Google Drive quota exceeded 」と表示が出てランタイムが終了してしまいます。この難点を避けるために...」 とありますので、それを実施してみたらいかがでしょうか? 【追記】 やってみたら、そちらでも実行できたので、回答にやり方追記しました
spa

2021/04/06 11:40 編集

ありがとうございます。 追記の通りにしたところ無事に動きました! 追記の通りでうまくいったのですが、GoogleColabからファイルをアップロードすると途中でエラーが出るようでファイルが壊れるという現象も起きていました。それに関してはドライブに直接アップロードで解決しました。 何度もお付き合いいただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問