繁体   English   中英

TensorFlow - 显示来自 MNIST 数据集的图像

[英]TensorFlow - Show image from MNIST DataSet

我正在尝试学习 TensorFlow,我从以下链接实现了 MNIST 示例: http ://openmachin.es/blog/tensorflow-mnist 我希望能够实际查看训练/测试图像。 所以我试图添加代码来显示第一批的第一张火车图片:

x_i = batch_xs[0]
image = tf.reshape(x_i,[28,28])

现在,因为数据是 float32 类型(值在 [0,1] 范围内),我尝试将其转换为 uint16,然后将其编码为 png 以显示图像。 我尝试使用tf.image.convert_image_dtype and tf.image.encode_png ,但没有成功。 你们能帮我理解如何将原始数据转换为图像并显示图像吗?

阅读完教程后,您可以在numpy中完成所有操作,不需要TF:

import matplotlib.pyplot as plt
first_array=batch_xs[0]
#Not sure you even have to do that if you just want to visualize it
#first_array=255*first_array
#first_array=first_array.astype("uint8")
plt.imshow(first_array)
#Actually displaying the plot if you are not in interactive mode
plt.show()
#Saving plot
plt.savefig("fig.png")

您还可以使用PIL或您使用的任何可视化工具。

X = X.reshape([28, 28]);
plt.gray()
plt.imshow(X)

这很有效。

在ML初学者教程MNIST中的代码之上,您可以在mnist数据集中可视化图像:

import matplotlib.pyplot as plt
batch = mnist.train.next_batch(1)
plotData = batch[0]
plotData = plotData.reshape(28, 28)
plt.gray() # use this line if you don't want to see it in color
plt.imshow(plotData)
plt.show()

在此输入图像描述

在张量流 2.0 中:

import matplotlib.pyplot as plt
import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

plt.imshow(x_train[0], cmap='gray_r')

将表示MNIST图像的numpy数组传递给下面的函数,它将使用matplotlib显示一个数字。

def displayMNIST(imageAsArray):
    imageAsArray = imageAsArray.reshape(28, 28);
    plt.imshow(imageAsArray, cmap='gray')
    plt.show()

我们可以使用matplotlib.pyplot subplots

 # plotting the first 9 images in the train set of MNIST
 fig , axs = plt.subplots(3, 3)
 cnt = 0
 for i in range(3):
     for j in range(3):
         axs[i, j].imshow(X_train[cnt])
         cnt += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM