简体   繁体   English

TypeError:dtype对象的图像数据无法转换为float。 当显示来自神经网络的图像时

[英]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.

相关问题 TypeError: dtype object 的图像数据无法转换为浮点数修复 - TypeError: Image data of dtype object cannot be converted to float Fix 类型错误:dtype 对象的图像数据无法转换为浮点数 - TypeError: Image data of dtype object cannot be converted to float 使用 matplotlib.pyplot.imshow() 绘制二维直方图时出现“TypeError:dtype 对象的图像数据无法转换为浮点数” - "TypeError: Image data of dtype object cannot be converted to float" when drawing 2d histogram using matplotlib.pyplot.imshow() plt.imshow()给出TypeError:无法使用PyTorch将dtype对象的图像数据转换为float - plt.imshow() gives TypeError: Image data of dtype object cannot be converted to float using PyTorch TypeError: dtype object 的图像数据无法转换为浮点数 - 使用 Seaborn 的 HeatMap Plot 问题 - TypeError: Image data of dtype object cannot be converted to float - Issue with HeatMap Plot using Seaborn TypeError: 图片数据不能转换为float // 导入.png图片时 - TypeError: Image data cannot be converted to float // When importing .png images 类型错误:图像数据无法转换为浮点数 - TypeError: Image data cannot be converted to float 在Tensorflow 2.0中,dtype对象的图像数据无法转换为浮动错误。 是什么原因造成的? - Image data of dtype object cannot be converted to float error in Tensorflow 2.0. What's causing this? matplotlib.image.imsave TypeError:无法将图像数据转换为float - matplotlib.image.imsave TypeError: Image data cannot be converted to float 类型错误:无法将图像数据转换为浮点数 无法将图像数据转换为浮点数 - TypeError: Image data cannot be converted to float Image data cannot be converted to float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM