将 MNIST 图像从 (28, 28, 1) 填充到 (32, 32, 1)
Padding MNIST images from (28, 28, 1) to (32, 32, 1)
我正在使用来自 TensorFlow 2.0 的 MNIST 数据集,并试图用零填充它并将图像大小从 (28, 28, 1) 增加到 (32, 32, 1)。代码是:
# Load MNIST dataset-
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train.shape, y_train.shape
# ((60000, 28, 28), (60000,))
X_test.shape, y_test.shape
# ((10000, 28, 28), (10000,))
# Pad with 2 zeros on left and right hand sides-
X_train_padded = np.pad(X_train[:,], (2, 2), 'constant')
X_train_padded.shape
# (60004, 32, 32)
但是,上面使用的 'np.pad()' 函数并没有给我所需的形状 (6000, 32, 32) 而且,它 returns 数组用零填充!而不是 X_train.
中的原始值
你能帮忙吗?
我正在使用 Python 3.8、TensorFlow 2.0 和 numpy 1.18。
谢谢!
您使用的 numpy.pad
有误。
- 你的数组是
(6000,32,32)
,所以你只想填充轴 1 和 2,而不是轴 0。
np.pad
的 pad_width
参数是这样工作的:((axis 1 pad before, axis 1 pad after), ...)
所以如果你想在每边填充 1 个像素,你应该做 ((0,0), (1,1), (1,1))
。 (您的代码在每一侧填充所有轴 2。)
这是一个玩具示例:
z = np.arange(12).reshape(3,2,2)
print(z.shape)
# (3, 2, 2)
z = np.pad(z, ((0,0),(1,1),(1,1)), 'constant')
print(z.shape)
# (3, 4, 4)
# as an example, take a look at the second "image"
print(z[1])
# [[0 0 0 0]
# [0 4 5 0]
# [0 6 7 0]
# [0 0 0 0]]
我正在使用来自 TensorFlow 2.0 的 MNIST 数据集,并试图用零填充它并将图像大小从 (28, 28, 1) 增加到 (32, 32, 1)。代码是:
# Load MNIST dataset-
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train.shape, y_train.shape
# ((60000, 28, 28), (60000,))
X_test.shape, y_test.shape
# ((10000, 28, 28), (10000,))
# Pad with 2 zeros on left and right hand sides-
X_train_padded = np.pad(X_train[:,], (2, 2), 'constant')
X_train_padded.shape
# (60004, 32, 32)
但是,上面使用的 'np.pad()' 函数并没有给我所需的形状 (6000, 32, 32) 而且,它 returns 数组用零填充!而不是 X_train.
中的原始值你能帮忙吗?
我正在使用 Python 3.8、TensorFlow 2.0 和 numpy 1.18。
谢谢!
您使用的 numpy.pad
有误。
- 你的数组是
(6000,32,32)
,所以你只想填充轴 1 和 2,而不是轴 0。 np.pad
的pad_width
参数是这样工作的:((axis 1 pad before, axis 1 pad after), ...)
所以如果你想在每边填充 1 个像素,你应该做((0,0), (1,1), (1,1))
。 (您的代码在每一侧填充所有轴 2。)
这是一个玩具示例:
z = np.arange(12).reshape(3,2,2)
print(z.shape)
# (3, 2, 2)
z = np.pad(z, ((0,0),(1,1),(1,1)), 'constant')
print(z.shape)
# (3, 4, 4)
# as an example, take a look at the second "image"
print(z[1])
# [[0 0 0 0]
# [0 4 5 0]
# [0 6 7 0]
# [0 0 0 0]]