我想要将mnist
个数据集转换为(28, 28, 3)
个维度,以便适合tf.keras.applications.MobileNetV2
模型,但这个模型需要(x, y, 3)
个维度.
https://www.tensorflow.org/tutorials/images/transfer_learning个
第一个任务是将mnist (28,28,1)
扩展到mnist (28,28,3)
,然后将(28,28,3)
转换为(x,y,3)
.
下面是显示(28,28,1)
张图片的代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(28*28*1).reshape(28,28,1)
plt.figure()
plt.imshow(x)
plt.title(x.shape)
plt.show()
以下代码试图显示(28,28,3)
,但它是从(28,28,1)
转换成NOT
的:
y = np.arange(28*28*3).reshape(28,28,3)
plt.figure()
plt.imshow(y)
plt.title(y.shape)
plt.show()
如何将上面的(28,28,1)
图像转换成(28, 28, 3)
并显示在matplotlib
中?
Testing:个
以下是比较原始图像(x
)、NumPy RGB图像(y
)、TensorFlow RGB(z
)和填充零图像(pad_zero
)的测试:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
x = np.arange(28*28*1).reshape(28,28,1)
x = x / x.max()
y = np.repeat(x, 3, axis=2)
z = tf.image.grayscale_to_rgb(tf.convert_to_tensor(x)).numpy()
def pad_with_zeros(a):
a = a.copy()
for ii, i in enumerate(a):
for jj, j in enumerate(i):
for kk, k in enumerate(j):
if kk != 0:
a[ii, jj, kk] = 0
return a
pad_zero = pad_with_zeros(y)
fig, axes = plt.subplots(1, 4, figsize=(16, 4))
fig.subplots_adjust(wspace=0.1, hspace=0.1)
plt.subplot(1, 4, 1)
plt.imshow(x)
plt.title("x: {}".format(x.shape))
plt.subplot(1, 4, 2)
plt.imshow(y)
plt.title("np.repeat: {}".format(y.shape))
plt.subplot(1, 4, 3)
plt.imshow(z)
plt.title("tf.image: {}".format(z.shape))
plt.subplot(1, 4, 4)
plt.imshow(pad_zero)
plt.title("pad_zero: {}".format(pad_zero.shape))
plt.show()
为什么所有的图像 colored颜色 都是different
?
y
和z
的 colored颜色 应该看起来像x
,但它们是not
.有什么不对劲吗?