前提・実現したいこと
Face Sketch to Image Generation using GANの実装を行う際に以下のエラーが発生しました
発生している問題・エラーメッセージ
AttributeError Traceback (most recent call last) <ipython-input-37-087d48d24ecb> in <module> 1 img_shape = (256, 256, 3) 2 ----> 3 d_model = discriminator(img_shape) 4 5 g_model = generator(img_shape) 7 frames <ipython-input-32-8e022b387e74> in discriminator(img_shape) 13 14 d1 = d_layer(merged, 64, norm=False) ---> 15 d2 = d_layer(d1, 128) 16 d3 = d_layer(d1, 256) 17 d4 = d_layer(d1, 512) <ipython-input-32-8e022b387e74> in d_layer(layer_in, n_filter, norm) 4 d = LeakyReLU(0.2)(d) 5 if norm: ----> 6 d = InstanceNormalization()(d) 7 return d 8 /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs) 661 kwargs.pop('training') 662 inputs, outputs = self._set_connectivity_metadata_( --> 663 inputs, outputs, args, kwargs) 664 self._handle_activity_regularization(inputs, outputs) 665 self._set_mask_metadata(inputs, outputs, previous_mask) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs) 1706 kwargs.pop('mask', None) # `mask` should not be serialized. 1707 self._add_inbound_node( -> 1708 input_tensors=inputs, output_tensors=outputs, arguments=kwargs) 1709 return inputs, outputs 1710 /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments) 1793 """ 1794 inbound_layers = nest.map_structure(lambda t: t._keras_history.layer, -> 1795 input_tensors) 1796 node_indices = nest.map_structure(lambda t: t._keras_history.node_index, 1797 input_tensors) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs) 513 514 return pack_sequence_as( --> 515 structure[0], [func(*x) for x in entries], 516 expand_composites=expand_composites) 517 /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0) 513 514 return pack_sequence_as( --> 515 structure[0], [func(*x) for x in entries], 516 expand_composites=expand_composites) 517 /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in <lambda>(t) 1792 `call` method of the layer at the call that created the node. 1793 """ -> 1794 inbound_layers = nest.map_structure(lambda t: t._keras_history.layer, 1795 input_tensors) 1796 node_indices = nest.map_structure(lambda t: t._keras_history.node_index, AttributeError: 'tuple' object has no attribute 'layer'
該当のソースコード
img_shape = (256, 256, 3) d_model = discriminator(img_shape) g_model = generator(img_shape) gan_model = GAN(g_model, d_model, img_shape)
d_model = discriminator(img_shape)←ここでエラー発生
試したこと
1.jupyter notebookで実行→AttributeError
2.google colabで実行→実行成功
3.一定時間操作なしで切断されたので、再度実行→jupyter notebookで実行したときと全く同じエラーが発生
google colabで実行して、一回目は実行できたのに二回目全く同じエラーが発生する原因が分かりません(もちろんGPU使用量上限に達したわけではなく、ソースコードもいじってない)
このエラーの原因について教えてください
補足情報(FW/ツールのバージョンなど)
また、下記のインポート文を実行すると
from __future__ import print_function, division from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization from keras.layers import Input, Dense, Reshape, Flatten, Dropout, Concatenate, BatchNormalization, Activation, ZeroPadding2D from keras.layers.advanced_activations import LeakyReLU from keras.layers.convolutional import UpSampling2D, Conv2D from keras.models import Sequential, Model from keras.optimizers import Adam from keras.preprocessing.image import img_to_array from keras.preprocessing.image import load_img from sklearn.utils import shuffle import matplotlib.pyplot as plt import numpy as np import datetime import natsort import scipy import sys import os import cv2
実行結果↓
WARNING:tensorflow:From /content/drive/My Drive/Colab Notebooks/senga-Face-Sketch-to-Image-Generation-using-GAN-master/keras-contrib/keras_contrib/layers/advanced_activations/srelu.py:43: The name tf.keras.initializers.RandomUniform is deprecated. Please use tf.compat.v1.keras.initializers.RandomUniform instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/initializers.py:119: calling RandomUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor
このような警告文が発生することがあります(これが出ると確定でAttributeErrorが発生し、出ないと実行が成功する)
動かそうとしてるのはこれですか?
https://github.com/Malikanhar/Face-Sketch-to-Image-Generation-using-GAN
それで合っています
https://github.com/Malikanhar/Face-Sketch-to-Image-Generation-using-GAN/blob/master/requirements.txt
にバージョン合わせてますか?
特にtensorflow関連とkeras関連
付属のrequire.txtを元にインストールしたので大丈夫なはずです(間違っていたら申し訳ありません)
tensorflow==1.14.0
keras==2.3.1
で実行しました
Google Colabで動かす場合はGPUを使うために、
> tensorflow==1.14.0
ではなくて「tensorflow-gpu 1.*」を入れてると思いますが、たとえば
!pip install tensorflow-gpu==1.15.2
を実行したら、「tensorflow-gpu 1.15.2」はそれで入りますが、元々入ってた「tensorflow 2.7.0」は削除されずに残ります
そのような状態になっていて、「tensorflow-gpu 1*」が使われたり、「tensorflow 2*」が使われたりして、うまく行ったりエラーが出たりする、ということになってませんでしょうか?
!pip list
を実行して、「tensorflow」が付くものが何が入ってるかをチェックしてみてください
> 1.jupyter notebookで実行→AttributeError
当方のWin 10(GPU有り)のPythonで、
https://github.com/Malikanhar/Face-Sketch-to-Image-Generation-using-GAN
から一式ダウンロードして、「Data Augmentation.ipynb」と「ContextualGAN.ipynb」をどちらも無修正でそのままjupyterで実行しましたが、エラーは出ずに「train(...」の行の実行開始までできました
(全部終わるまでに時間がかなりかかりそうなので、「========== Epoch 3 ==========」と表示されたところで中断しましたが)
環境
Python 3.7.11
tensorflow-gpu 1.15.2
Keras 2.3.1
keras-contrib 2.0.8
CUDA 10.0
jupyter notebookでpip listで確認したところ
tensorflow-gpu==1.15.2
tensorflow-estimator==1.14.0
tensorboard==1.14.0
Keras==2.3.1
keras-contrib==2.0.8
python==3.7.12
CUDA==10.0
でした。
ちなみにtensorflowをアンインストールすると、「ContextualGAN.ipynb」の
from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization
の文で
ModuleNotFoundError:No module named 'tensorflow'が起こり、tensorflow==2.7.0だと
from keras.optimizers import Adam で
ImportError:cannot import name 'Adam' from 'keras.optimizers'が起こりました(Adamはインストール済み)
> tensorflowをアンインストール
> No module named 'tensorflow'
がGoogle Colabのことなら、アンインストールした後に、「tensorflow-gpu 1*」をインストールします
【追記】
Google Colabではなく、パソコンのjupyterのことで、元々tensorflowが入って無ければ、もちろんアンインストールする必要はありません
必要なものをインストールするだけです
> jupyter notebookでpip listで確認
が、パソコンのjupyterのことなら、当方のWin 10(GPU有り)でエラー出ずに実行できた環境と基本同じですけど、それで質問者さんの環境ではエラー出るのですか?
google colabでtensorflowアンスト後、tensorflow-gpu 1*をインストールしたらうまく実行できました!
jupyterに現状入っているライブラリがこちらになります
Package Version
----------------------------- ----------
absl-py 0.8.1
adam 0.0.0.dev0
anyio 3.3.4
argcomplete 1.12.3
argon2-cffi 21.1.0
astor 0.8.0
astunparse 1.6.3
async-generator 1.10
attrs 19.3.0
Babel 2.9.1
backcall 0.1.0
backports.functools-lru-cache 1.6.4
bleach 3.1.1
brotlipy 0.7.0
cached-property 1.5.2
cachetools 3.1.1
certifi 2019.11.28
cffi 1.15.0
chardet 3.0.4
charset-normalizer 2.0.7
clang 5.0
colorama 0.4.3
cryptography 35.0.0
cycler 0.10.0
debugpy 1.4.1
decorator 4.4.1
defusedxml 0.6.0
entrypoints 0.3
flatbuffers 1.12
gast 0.2.2
google-auth 1.7.1
google-auth-oauthlib 0.4.1
google-pasta 0.1.8
grpcio 1.25.0
h5py 2.10.0
idna 2.8
imageio 2.6.1
importlib-metadata 1.3.0
install 1.3.4
ipykernel 5.1.3
ipython 7.10.2
ipython-genutils 0.2.0
ipywidgets 7.5.1
jedi 0.15.1
Jinja2 2.10.3
joblib 0.14.0
json5 0.9.5
jsonschema 3.2.0
jupyter 1.0.0
jupyter-client 5.3.4
jupyter-console 6.0.0
jupyter-core 4.6.1
jupyter-server 1.11.1
jupyterlab 3.2.1
jupyterlab-pygments 0.1.2
jupyterlab-server 2.8.2
Keras 2.3.1
Keras-Applications 1.0.8
keras-contrib 2.0.8
Keras-Preprocessing 1.1.0
kiwisolver 1.1.0
Markdown 3.1.1
MarkupSafe 1.1.1
matplotlib 3.1.2
matplotlib-inline 0.1.3
mistune 0.8.4
more-itertools 8.0.2
natsort 6.2.0
nbclassic 0.3.4
nbclient 0.5.4
nbconvert 5.6.1
nbformat 4.4.0
nest-asyncio 1.5.1
networkx 2.4
notebook 6.0.2
numpy 1.16.1
oauthlib 3.1.0
opencv-python 4.1.2.30
opt-einsum 3.1.0
packaging 21.0
pandocfilters 1.4.2
parso 0.5.2
pickleshare 0.7.5
Pillow 6.2.1
pip 21.3.1
prometheus-client 0.7.1
prompt-toolkit 2.0.10
protobuf 3.11.1
pyasn1 0.4.8
pyasn1-modules 0.2.7
pycparser 2.20
Pygments 2.5.2
pyOpenSSL 21.0.0
pyparsing 2.4.5
pyrsistent 0.15.6
PySocks 1.7.1
python-dateutil 2.8.1
pytz 2021.3
PyWavelets 1.1.1
pywin32 301
pywinpty 1.1.5
PyYAML 5.2
pyzmq 22.3.0
qtconsole 4.6.0
QtPy 1.11.2
requests 2.22.0
requests-oauthlib 1.3.0
requests-unixsocket 0.2.0
rsa 4.0
scikit-image 0.16.2
scikit-learn 0.22
scipy 1.3.3
Send2Trash 1.5.0
setuptools 58.2.0
six 1.13.0
sniffio 1.2.0
tensorboard 1.14.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit 1.8.0
tensorflow 1.14.0
tensorflow-estimator 1.14.0
tensorflow-gpu 1.15.2
termcolor 1.1.0
terminado 0.8.3
testpath 0.4.4
tornado 6.0.3
traitlets 4.3.3
typing-extensions 3.10.0.2
urllib3 1.25.7
wcwidth 0.1.7
webencodings 0.5.1
websocket-client 0.57.0
Werkzeug 0.16.0
wheel 0.37.0
widgetsnbextension 3.5.1
win-inet-pton 1.1.0
wincertstore 0.2
wrapt 1.11.2
zipp 0.6.0
基本的にrequire.txtに記載されているライブラリすべて入れています
tensorflow*1は後から追加したもので最初はなかったのですが、tensorflow*1をインストールしたのが原因なのでしょうか?
> google colabでtensorflowアンスト後、tensorflow-gpu 1*をインストールしたらうまく実行できました!
もし今後、また
> 3.一定時間操作なしで切断されたので、再度実行→jupyter notebookで実行したときと全く同じエラーが発生
となったら、「切断された後の再実行」で、メニューバーの[ランタイム]→[すべてのセルを実行]をしてみてください
(やってるとは思いますが、念の為に書きました)
> jupyterに現状入っているライブラリ
> tensorflow 1.14.0
> tensorflow-gpu 1.15.2
pipは「tensorflow」と「tensorflow-gpu」を別のもの扱いするのですが、当然重複するファイルがたくさんあるので、片方をアンインストールするともう片方がまともに動かなくなったりします
たぶん下記のようにしたら、余計なものが消えて、「tensorflow-gpu 1.15.2」だけが入ってる環境になると思います
(それぞれでインストール→アンインストールをしてキレイに消してから、最後に使いたいのをインストールする)
pip install --upgrade --no-deps --force-reinstall tensorflow-gpu==1.15.2
pip uninstall tensorflow-gpu -y
pip install --upgrade --no-deps --force-reinstall tensorflow==1.14.0
pip uninstall tensorflow -y
pip install tensorflow-gpu==1.15.2
実行できました!
一度anacondaで新しい環境を構築し直してjupyter上でpip listを実行すると、tensorflowが入っていませんでした。
そこから、tensorflowはインストールせず、require.txtに記載されているライブラリのみインストールしたら、train()...まで何の問題もなく実行できました。
全てはpip install tensorflowを実行してしまったのが原因だったようです...
長時間お付き合い頂き、本当にありがとうございました!!
(ローカルパソコンの)jupyterも、Google Colabも、「tensorflow」と「tensorflow-gpu」の両方が入ってる状態だとダメで、下記を行なって「tensorflow-gpu」しか入ってない状態にしたら大丈夫になった、というわけですね
・jupyter:「tensorflow」関連が何も入ってない状態で「tensorflow-gpu」のみインストール
・Google Colab:「tensorflow」をアンインストールしてから「tensorflow-gpu」をインストール
回答1件
あなたの回答
tips
プレビュー