我正在使用GAN教程中的代码在Tensorflow中生成MNIST数字。
(此处链接:https://www.tensorflow.org/beta/tutorials/generative/dcgan)
我知道了
Traceback (most recent call last):
File "GAN_MNIST_tutorial.py", line 66, in <module>
plt.imshow(np.array(generated_image[0, :, :, 0]), cmap='gray')
File "C:\venv\lib\site-packages\matplotlib\pyplot.py", line 2677, in imshow
None else {}), **kwargs)
File "C:\venv\lib\site-packages\matplotlib\__init__.py", line 1589, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\venv\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
return func(*args, **kwargs)
File "C:\venv\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
return func(*args, **kwargs)
File "C:\venv\lib\site-packages\matplotlib\axes\_axes.py", line 5660, in imshow
im.set_data(X)
File "C:\venv\lib\site-packages\matplotlib\image.py", line 678, in set_data
"float".format(self._A.dtype))
TypeError: Image data of dtype object cannot be converted to float
当我运行它时。
这是我的代码:
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
tf.__version__
import glob
import imageio
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
from tensorflow.keras import layers
import time
def make_generator_model():
model = tf.keras.Sequential()
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Reshape((7, 7, 256)))
assert model.output_shape == (None, 7, 7, 256) # Note: None is the batch size
model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
assert model.output_shape == (None, 7, 7, 128)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
assert model.output_shape == (None, 14, 14, 64)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
assert model.output_shape == (None, 28, 28, 1)
return model
generator = make_generator_model()
noise = tf.random.normal([1, 100])
generated_image = generator(noise, training=False)
plt.imshow(generated_image[0, :, :, 0], cmap='gray')
我尝试在dtype = 'float32
中添加generated_image
,并将generated_image
转换为一个numpy数组,但无济于事。有什么问题吗?
答案 0 :(得分:1)
我认为您使用tensorflow 1.x运行了代码。在TF1.x(未启用急切功能)中,操作(Ops)生成符号张量,其中不包含任何值,除非您在会话中运行这些张量。该会话将执行这些图模式符号张量并返回实际张量(可以从张量访问numpy数组)。
因此,我在代码的末尾添加了几行以执行这些符号张量。请检查下面的代码。如果您还有其他问题,请告诉我。
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
tf.__version__
import glob
import imageio
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
from tensorflow.keras import layers
import time
def make_generator_model():
model = tf.keras.Sequential()
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Reshape((7, 7, 256)))
assert model.output_shape == (None, 7, 7, 256) # Note: None is the batch size
model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
assert model.output_shape == (None, 7, 7, 128)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
assert model.output_shape == (None, 14, 14, 64)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
assert model.output_shape == (None, 28, 28, 1)
return model
generator = make_generator_model()
noise = tf.random.normal([1, 100])
generated_image = generator(noise, training=False)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
generated_image = sess.run(generated_image)
plt.imshow(generated_image[0, :, :, 0], cmap='gray')
如果您认为此答案有用,请接受此答案和/或对其进行投票。谢谢!
答案 1 :(得分:0)
问题是您将符号keras / tensorflow函数与数字函数(例如matplotlib)混合在一起。
如果要生成随机向量并进行模型预测,则应使用:
noise = np.random.normal(size=(1, 100))
generated_image = generator.predict(noise)
plt.imshow(generated_image[0, :, :, 0], cmap='gray')
答案 2 :(得分:0)
通过从TF1升级到TF2解决了此问题。我在tf1.14.X上运行了它,但在升级到tf 2.0之后代码无法正常工作。