在 Tensorflow 中调整 MNIST 的大小 [英] Resize MNIST in Tensorflow
问题描述
我一直在研究 MNIST 数据集,以学习如何在我的深度学习课程中使用 Tensorflow 和 Python.
I have been working on MNIST dataset to learn how to use Tensorflow and Python for my deep learning course.
我想将 MNIST 的大小调整为 22 &22 使用tensorflow,那我训练它,但是我不会怎么办?
I want to resize MNIST as 22 & 22 using tensorflow, then I train it, but I do not how to do?
你能帮我吗?
推荐答案
TheRevanchist 的回答是正确的.但是,对于 mnist 数据集,首先需要对 mnist 数组进行整形,然后再将其发送到 tf.image.resize_images():
TheRevanchist's answer is correct. However, for the mnist dataset, you first need to reshape the mnist array before you send it to tf.image.resize_images():
import tensorflow as tf
import numpy as np
import cv2
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
batch = mnist.train.next_batch(10)
X_batch = batch[0]
batch_tensor = tf.reshape(X_batch, [10, 28, 28, 1])
resized_images = tf.image.resize_images(batch_tensor, [22,22])
上面的代码取出一批 10 张 mnist 图像,并将它们从 28x28 图像重塑为 22x22 tensorflow 图像.
The code above takes out a batch of 10 mnist images and reshapes them from 28x28 images to 22x22 tensorflow images.
如果你想显示图像,你可以使用opencv和下面的代码.resized_images.eval() 将 tensorflow 图像转换为 numpy 数组!
If you want to display the images, you can use opencv and the code below. The resized_images.eval() converts the tensorflow image to a numpy array!
with tf.Session() as sess:
numpy_imgs = resized_images.eval(session=sess) # mnist images converted to numpy array
for i in range(10):
cv2.namedWindow('Resized image #%d' % i, cv2.WINDOW_NORMAL)
cv2.imshow('Resized image #%d' % i, numpy_imgs[i])
cv2.waitKey(0)
这篇关于在 Tensorflow 中调整 MNIST 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!