Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i convert mnist data to RGB format?

I am trying to convert MNIST dataset to RGB format, the actual shape of each image is (28, 28), but i need (28, 28, 3).

import numpy as np
import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, _), (x_test, _) = mnist.load_data()

X = np.concatenate([x_train, x_test])
X = X / 127.5 - 1

X.reshape((70000, 28, 28, 1))

tf.image.grayscale_to_rgb(
    X,
    name=None
)

But i get the following error:

ValueError: Dimension 1 in both shapes must be equal, but are 84 and 3. Shapes are [28,84] and [28,3].
like image 574
farshad1123 Avatar asked Nov 09 '19 11:11

farshad1123


People also ask

Is MNIST an RGB?

This is because the MNIST dataset contains grayscale images, not RGB images. The (60000, 28, 28) means the train image set contains 60,000 images of 28 x 28 px. In other words, it is an array containing 60,000 matrices of 28 x 28 integer values.

What is the format of MNIST dataset?

The source MNIST data files are stored in a proprietary binary format. The program loads the binary pixel and label training files into memory, converts the data to tab-delimited text and saves just the first 1,000 training images and their "0" to "9" labels.

How do you graph a MNIST image?

To plot an individual MNIST image, we will first store the individual image in an “image” variable. You can pass this variable to the imshow method as shown below. Next, we will initialize the figure and axes handles using matplotlib's subplots command, then iteratively display the digit images and labels.

How is MNIST data stored?

The primary repository for the MNIST files is currently located at yann.lecun.com/exdb/mnist. The training pixel data is stored in file train-images-idx3-ubyte. gz and the training label data is stored in file train-labels-idx1-ubyte.


1 Answers

You should store the reshaped 3D [28x28x1] images in an array:

X = X.reshape((70000, 28, 28, 1))

When converting, set an other array to the return value of the tf.image.grayscale_to_rgb() function :

X3 = tf.image.grayscale_to_rgb(
X,
name=None
)

Finally, to plot out one example from the resulting tensor images with matplotlib and tf.session():

import matplotlib.pyplot as plt

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    image_to_plot = sess.run(image)
    plt.figure()
    plt.imshow(image_to_plot)
    plt.grid(False)

The complete code:


import numpy as np
import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, _), (x_test, _) = mnist.load_data()

X = np.concatenate([x_train, x_test])
X = X / 127.5 - 1

# Set reshaped array to X 
X = X.reshape((70000, 28, 28, 1))

# Convert images and store them in X3
X3 = tf.image.grayscale_to_rgb(
    X,
    name=None
)

# Get one image from the 3D image array to var. image
image = X3[0,:,:,:]

# Plot it out with matplotlib.pyplot
import matplotlib.pyplot as plt

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    image_to_plot = sess.run(image)
    plt.figure()
    plt.imshow(image_to_plot)
    plt.grid(False)
like image 155
Aqwis01 Avatar answered Oct 13 '22 22:10

Aqwis01