Я использую код из учебника GAN по генерации цифр 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, но безрезультатно. В чем проблема?