tf.pad returns 数组用零填充,MNIST 数据集从 (28,28) 图像大小填充到 (32,32)
tf.pad returns array filled with zero, MNIST dataset padding from (28,28) image size to (32,32)
我需要在 MNIST 数据集上训练 LeNet5 CNN。关于 LeNet5 的原始论文,我想在输入图像大小 32 x 32 而不是图像大小 28 x 28 上进行训练,以具有相同数量的参数。
我尝试使用 tf.pad 和 numpy.pad 函数,但我遇到了同样的问题:我可以使用 (31,32) 或 (32,31) returned图像大小,但当我尝试 (32,32) 或更多时,函数 return 数组填充零而不是 X_train 原始值 + 填充值。这里的代码和结果:
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
print(X_train.shape)
print('Before padding',X_train[0])
X_train_pad = tf.pad(X_train,([0,0],[2,2],[2,2]), mode='CONSTANT', constant_values=0, name=None)
print('After padding',X_train_pad[0])
(60000, 28, 28)
Before padding [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136
175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253
225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251
93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119
25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253
150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252
253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249
253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253
253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253
250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201
78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]]
After padding tf.Tensor(
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]], shape=(32, 32), dtype=uint8)
当我用 1 填充时,我可以看到填充值和 0 而不是 X_train 的原始值:
X_train_pad = tf.pad(X_train,([0,0],[2,2],[2,2]), mode='CONSTANT', constant_values=1, name=None)
After padding tf.Tensor(
[[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 0 ... 0 1 1]
...
[1 1 0 ... 0 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]], shape=(32, 32), dtype=uint8)
你知道我的错误是什么吗?
谢谢
没有错
填充工作正常。
您正在目视检查它并得出它不正确的结论,而不是使用您的计算机来简单地测试它。
import numpy as np
import tensorflow as tf
# get data
(X_train, _), (_, _) = tf.keras.datasets.mnist.load_data()
# pad
X_train_pad = tf.pad(X_train, [[0, 0], [2, 2], [2, 2]], mode="CONSTANT")
# extract the original image by manually removing the
# padding from the borders
extract_original_image = X_train_pad[:, 2:-2, 2:-2]
print(extract_original_image.shape)
# TensorShape([60000, 28, 28])
# check padded image with padding removed equals original
np.testing.assert_equal(extract_original_image.numpy(), X_train)
# check padding is all zeros
assert tf.math.reduce_sum(X_train_pad[:, :2, :2]).numpy() == 0
assert tf.math.reduce_sum(X_train_pad[:, -2:, -2:]).numpy() == 0
# check the sums are the same
assert tf.math.reduce_sum(X_train_pad) == tf.math.reduce_sum(X_train)
我需要在 MNIST 数据集上训练 LeNet5 CNN。关于 LeNet5 的原始论文,我想在输入图像大小 32 x 32 而不是图像大小 28 x 28 上进行训练,以具有相同数量的参数。
我尝试使用 tf.pad 和 numpy.pad 函数,但我遇到了同样的问题:我可以使用 (31,32) 或 (32,31) returned图像大小,但当我尝试 (32,32) 或更多时,函数 return 数组填充零而不是 X_train 原始值 + 填充值。这里的代码和结果:
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
print(X_train.shape)
print('Before padding',X_train[0])
X_train_pad = tf.pad(X_train,([0,0],[2,2],[2,2]), mode='CONSTANT', constant_values=0, name=None)
print('After padding',X_train_pad[0])
(60000, 28, 28)
Before padding [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136
175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253
225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251
93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119
25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253
150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252
253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249
253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253
253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253
250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201
78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]]
After padding tf.Tensor(
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]], shape=(32, 32), dtype=uint8)
当我用 1 填充时,我可以看到填充值和 0 而不是 X_train 的原始值:
X_train_pad = tf.pad(X_train,([0,0],[2,2],[2,2]), mode='CONSTANT', constant_values=1, name=None)
After padding tf.Tensor(
[[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 0 ... 0 1 1]
...
[1 1 0 ... 0 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]], shape=(32, 32), dtype=uint8)
你知道我的错误是什么吗?
谢谢
没有错
填充工作正常。
您正在目视检查它并得出它不正确的结论,而不是使用您的计算机来简单地测试它。
import numpy as np
import tensorflow as tf
# get data
(X_train, _), (_, _) = tf.keras.datasets.mnist.load_data()
# pad
X_train_pad = tf.pad(X_train, [[0, 0], [2, 2], [2, 2]], mode="CONSTANT")
# extract the original image by manually removing the
# padding from the borders
extract_original_image = X_train_pad[:, 2:-2, 2:-2]
print(extract_original_image.shape)
# TensorShape([60000, 28, 28])
# check padded image with padding removed equals original
np.testing.assert_equal(extract_original_image.numpy(), X_train)
# check padding is all zeros
assert tf.math.reduce_sum(X_train_pad[:, :2, :2]).numpy() == 0
assert tf.math.reduce_sum(X_train_pad[:, -2:, -2:]).numpy() == 0
# check the sums are the same
assert tf.math.reduce_sum(X_train_pad) == tf.math.reduce_sum(X_train)