簡體   English   中英

如何將 mnist 數據轉換為 RGB 格式?

[英]How can i convert mnist data to RGB format?

我正在嘗試將 MNIST 數據集轉換為 RGB 格式,每個圖像的實際形狀是 (28, 28),但我需要 (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
)

但我收到以下錯誤:

ValueError: Dimension 1 in both shapes must be equal, but are 84 and 3. Shapes are [28,84] and [28,3].

您應該將重塑后的 3D [28x28x1] 圖像存儲在一個數組中:

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

轉換時,將另一個數組設置為tf.image.grayscale_to_rgb() function 的返回值:

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

最后,從matplotlibtf.session()生成的張量圖像中,對 plot 給出一個示例:

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)

完整代碼:


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)

如果您在 tf.image.grayscale_to_rgb 之前打印 X 的形狀,您將看到 output 尺寸為 (70000, 28, 28)。 tf.image.grayscale 的輸入大小必須為 1,因為它是最終維度。

擴展X的最終尺寸使其與function兼容

tf.image.grayscale_to_rgb(tf.expand_dims(X, axis=3))

除了@DMolony 和@Aqwis01 答案之外,另一個簡單的解決方案可能是使用numpy.repeat方法多次復制張量的最后一個維度:

X = X.reshape((70000, 28, 28, 1))
X = X.repeat(3, -1)  # repeat the last (-1) dimension three times
X_t = tf.convert_to_tensor(X)
assert X_t.shape == (70000, 28, 28, 3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM