[英]TypeError: Image data of dtype object cannot be converted to float . When showing image from neural net
When I run the code I get this error. 当我运行代码时,出现此错误。 I am using the guide from the Tensorflow Website for the Deep Convolutional Generative Adversarial Network, https://www.tensorflow.org/beta/tutorials/generative/dcgan#what_are_gans I am copy/pasting from that site.
我正在使用来自Tensorflow网站的深度卷积生成对抗网络指南, https: //www.tensorflow.org/beta/tutorials/generative/dcgan#what_are_gans我正在从该站点复制/粘贴。
I've gone through other questions on stackoverflow but none of them answer my question. 我在stackoverflow上经历了其他问题,但没有一个回答我的问题。
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import glob
import imageio
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
from tensorflow.keras import layers
import time
from IPython import display
(train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5 # Normalize the images to [-1, 1]
BUFFER_SIZE = 60000
BATCH_SIZE = 256
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')
It should show a blurry image. 它应该显示模糊的图像。
Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance.
提前致谢。
Run above code by enabling eager execution at the start of the program. 通过在程序启动时启用急切执行来运行以上代码。 Add below line after the tensorflow import statement:
在tensorflow import语句之后添加以下行:
tf.enable_eager_execution()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.