teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

「コードの挿入」を用いて、コードを書き直しました

2020/10/03 02:51

投稿

KentaNakamoto
KentaNakamoto

スコア23

title CHANGED
File without changes
body CHANGED
@@ -2,15 +2,19 @@
2
2
 
3
3
 
4
4
  Google Colaboratoryにおいて、StyleGANを利用しようとしています。
5
+
5
- http://cedro3.com/ai/stylegan/ を参考にしながらやってみたのですが、importの部分で'tensorflow' has no attribute 'Dimension'というエラーがでてしまいます(このエラーについてググってみましたが、どうすれば解決するのかよくわかりませんでした)。
6
+ [StyleGANの学習済みモデルでサクッと遊んでみる](http://cedro3.com/ai/stylegan/)を参考にしながらやってみたのですが、importの部分で'tensorflow' has no attribute 'Dimension'というエラーがでてしまいます(このエラーについてググってみましたが、どうすれば解決するのかよくわかりませんでした)。
6
7
  ちなみに、Ubuntuのバージョンは、18.04.5で、Pythonのバージョンは3.6.9です。
7
8
  TensorFlowのバージョンは、2.3.0でして、1.14.0に変更もしてやってみましたが、こちらもNo module named 'dnnlib'というエラーが出てしまっています(こちらも、どのように解決すればいいのかわかりませんでした)。
9
+
10
+ コードが悪いのかと思いまして[Google ColaboratoryでStyleGANを使ってみた。](https://qiita.com/Phoeboooo/items/12d21916de56d125f0be)も参考にしてみましたが、同じエラーがでました(ライブラリのimportがうまくいっていないので、そりゃ同じエラーが出ますよね)
11
+
8
12
  Python初心者でして、何がいけないのかよくわかっておりません。
9
13
  申し訳ありませんが、よろしくお願いいたします。
10
14
 
11
15
 
12
- ### 発生している問題・エラメッセージ
16
+ ###
13
-
17
+ ```Python
14
18
  import os
15
19
  import pickle
16
20
  import numpy as np
@@ -20,17 +24,21 @@
20
24
  import config
21
25
 
22
26
  def main():
23
-
27
+ # Initialize TensorFlow.
24
28
  tflib.init_tf()
25
29
 
26
-
30
+ # Load pre-trained network.
27
31
  url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl
28
32
  with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f:
29
33
  _G, _D, Gs = pickle.load(f)
30
- snapshot.
34
+ # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run.
35
+ # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run.
36
+ # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot.
31
37
 
38
+ # Print network details.
32
39
  Gs.print_layers()
33
40
 
41
+ # Pick latent vector.
34
42
  rnd = np.random.RandomState(10) # seed = 10
35
43
  latents0 = rnd.randn(1, Gs.input_shape[1])
36
44
  latents1 = rnd.randn(1, Gs.input_shape[1])
@@ -43,19 +51,23 @@
43
51
  num_split = 39 # 2つのベクトルを39分割
44
52
  for i in range(40):
45
53
  latents = latents6+(latents0-latents6)*i/num_split
46
-
54
+ # Generate image.
47
55
  fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
48
56
  images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)
49
57
 
58
+ # Save image.
50
59
  os.makedirs(config.result_dir, exist_ok=True)
51
60
  png_filename = os.path.join(config.result_dir, 'photo'+'{0:04d}'.format(i)+'.png')
52
61
  PIL.Image.fromarray(images[0], 'RGB').save(png_filename)
53
62
 
54
63
  if __name__ == "__main__":
55
64
  main()
65
+ ```
56
66
 
57
67
  ---------------------------------------------------------------------------
58
68
 
69
+ ### 発生している問題・エラーメッセージ
70
+ ```Python
59
71
  AttributeError Traceback (most recent call last)
60
72
  <ipython-input-6-ece5f04dab83> in <module>()
61
73
  4 import PIL.Image
@@ -86,12 +98,13 @@
86
98
  36 return [dim.value for dim in shape]
87
99
 
88
100
  AttributeError: module 'tensorflow' has no attribute 'Dimension'
101
+ ```
89
102
 
90
103
  ---------------------------------------------------------------------------
91
104
 
92
105
  また、1.14.0では、以下のようなエラーが出てきます
93
106
 
94
-
107
+ ```Python
95
108
  ModuleNotFoundError Traceback (most recent call last)
96
109
  <ipython-input-1-ece5f04dab83> in <module>()
97
110
  3 import numpy as np
@@ -100,4 +113,5 @@
100
113
  6 import dnnlib.tflib as tflib
101
114
  7 import config
102
115
 
103
- ModuleNotFoundError: No module named 'dnnlib'
116
+ ModuleNotFoundError: No module named 'dnnlib'
117
+ ```