前提・実現したいこと
Google Colaboratory で StyleGAN2 を試しています。(初学者です)
10月のどこかで CUDA10.0 が消えたりと Google Colab に改正があり、その影響で元は動いていたコードが動かなくなってしまいした。
以下のサイトを参考にして進めています。
・https://techpr.info/ml/stylegan2-colab/
発生している問題①
下記のコードを実行するとエラーがでます。
vector_size = Gs.input_shape[1:][0] seeds = seeder(range(8000, 8020), vector_size) generate_images(Gs, seeds, truncation_psi=0.5)
エラーメッセージ①
Python
1 2Generating image for seed 0/20 ... 3--------------------------------------------------------------------------- 4InvalidArgumentError Traceback (most recent call last) 5/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args) 6 1364 try: 7-> 1365 return fn(*args) 8 1366 except errors.OpError as e: 9 108 frames 11InvalidArgumentError: Cannot assign a device for operation Gs/_Run/Gs/latents_in: {{node Gs/_Run/Gs/latents_in}} was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0, /job:localhost/replica:0/task:0/device:XLA_GPU:0 ]. Make sure the device specification refers to a valid device. 12 [[Gs/_Run/Gs/latents_in]] 13 14During handling of the above exception, another exception occurred: 15 16InvalidArgumentError Traceback (most recent call last) 17/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args) 18 1382 '\nsession_config.graph_options.rewrite_options.' 19 1383 'disable_meta_optimizer = True') 20-> 1384 raise type(e)(node_def, op, message) 21 1385 22 1386 def _extend_graph(self): 23 24InvalidArgumentError: Cannot assign a device for operation Gs/_Run/Gs/latents_in: node Gs/_Run/Gs/latents_in (defined at /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py:1748) was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0, /job:localhost/replica:0/task:0/device:XLA_GPU:0 ]. Make sure the device specification refers to a valid device. 25 [[Gs/_Run/Gs/latents_in]]
発生している問題②
生成した画像(image○○○.png)を動画形式に変換するコードでエラーが出ます。
!ffmpeg -r 30 -i image%d.png -vcodec mpeg4 -y test.mp4
エラーメッセージ②
[image2 @ 0x55fd18c8e000] Could find no file with path 'image%d.png' and index in the range 0-4 image%d.png: No such file or directory
該当のソースコード
Python
1 2# install cuda10.0 3!apt-get --purge remove cuda nvidia* libnvidia-* 4!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge 5!apt-get remove cuda-* 6!apt autoremove 7 8!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb 9!sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb 10!sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub 11!sudo apt-get update 12!wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb 13!sudo apt install -y ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb 14!sudo apt-get update 15 16# Install NVIDIA driver 17#!sudo apt-get install --no-install-recommends nvidia-driver-418 18!sudo apt-get -y installnvidia-driver-418 19# Reboot. Check that GPUs are visible using the command: nvidia-smi 20 21# Install development and runtime libraries (~4GB) 22#!sudo apt-get install --no-install-recommends \ 23!sudo apt-get install -y \ 24 cuda-10-0 \ 25 libcudnn7=7.6.2.24-1+cuda10.0 \ 26 libcudnn7-dev=7.6.2.24-1+cuda10.0 27 28 29# Install TensorRT. Requires that libcudnn7 is installed above. 30# !sudo apt-get install -y --no-install-recommends libnvinfer5=5.1.5-1+cuda10.0 \ 31!sudo apt-get install -y libnvinfer5=5.1.5-1+cuda10.0 \ 32 libnvinfer-dev=5.1.5-1+cuda10.0 33 34!apt --fix-broken install 35 36 37# tensorflow 38!pip uninstall tensorflow -y 39!pip install tensorflow-gpu==1.15.2 40!pip install keras==2.2.4 41 42 43# Git clone 44!git clone https://github.com/NVlabs/stylegan2.git 45 46 47# 出力先フォルダの作成 48!mkdir out 49 50 51# 潜在変数を作成するための関数を用意 52def seeder(seeds, vector_size): 53 result = [] 54 55 for seed in seeds: 56 rnd = np.random.RandomState(seed) 57 result.append( rnd.randn(1, vector_size) ) 58 return result 59 60 61# 必要なパッケージのインストール 62import argparse 63import numpy as np 64import PIL.Image 65import dnnlib 66import dnnlib.tflib as tflib 67import re 68import sys 69import pretrained_networks 70 71 72# 画像生成プロセスの関数 73def generate_images(Gs, seeds, truncation_psi): 74 noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')] 75 76 Gs_kwargs = dnnlib.EasyDict() 77 Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) 78 Gs_kwargs.randomize_noise = False 79 if truncation_psi is not None: 80 Gs_kwargs.truncation_psi = truncation_psi 81 82 for seed_idx, seed in enumerate(seeds): 83 print('Generating image for seed %d/%d ...' % (seed_idx, len(seeds))) 84 rnd = np.random.RandomState() 85 tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width] 86 images = Gs.run(seed, None, **Gs_kwargs) # [minibatch, height, width, channel] 87 path = f"/content/out/image{seed_idx}.png" 88 PIL.Image.fromarray(images[0], 'RGB').save(path) 89 90 91# 顔画像の学習モデルを読み込み 92network_pkl = 'gdrive:networks/stylegan2-ffhq-config-a.pkl' 93print('Loading networks from "%s"...' % network_pkl) 94_G, _D, Gs = pretrained_networks.load_networks(network_pkl) 95 96 97# パラメータの指定 98vector_size = Gs.input_shape[1:][0] 99seeds = seeder(range(8000, 8020), vector_size) 100 101generate_images(Gs, seeds, truncation_psi=0.5) 102 103 104# 画像を300枚生成 105seeds = seeder([6660, 7770], vector_size) 106 107STEPS = 300 108diff = seeds[1] - seeds[0] 109step = diff / STEPS 110current = seeds[0].copy() 111 112seeds2 = [] 113for i in range(STEPS): 114 seeds2.append(current) 115 current = current + step 116 117generate_images(Gs, seeds2, truncation_psi=0.5) 118 119 120# 生成した画像を動画形式に変換 121!ffmpeg -r 30 -i image%d.png -vcodec mpeg4 -y test.mp4
補足情報(FW/ツールのバージョンなど)
GPUを認識できていると思うのですが、なぜ使用できないのでしょうか。
[name: "/device:CPU:0" device_type: "CPU" memory_limit: 268435456 locality { } incarnation: 2440703754907394877, name: "/device:XLA_CPU:0" device_type: "XLA_CPU" memory_limit: 17179869184 locality { } incarnation: 9295743096350903082 physical_device_desc: "device: XLA_CPU device", name: "/device:XLA_GPU:0" device_type: "XLA_GPU" memory_limit: 17179869184 locality { } incarnation: 3392596144835826399 physical_device_desc: "device: XLA_GPU device", name: "/device:GPU:0" device_type: "GPU" memory_limit: 14947935847 locality { bus_id: 1 links { } } incarnation: 428819314228490541 physical_device_desc: "device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5"]
回答1件
あなたの回答
tips
プレビュー