【问题标题】:Padding MNIST images from (28, 28, 1) to (32, 32, 1)将 MNIST 图像从 (28, 28, 1) 填充到 (32, 32, 1)
【发布时间】:2020-04-19 18:19:10
【问题描述】:

我正在使用来自 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) 形状,而且它返回的数组填充了零!而不是 X_train 中的原始值。

你能帮忙吗?

我正在使用 Python 3.8、TensorFlow 2.0 和 numpy 1.18。

谢谢!

【问题讨论】:

    标签: python-3.x numpy tensorflow2.0


    【解决方案1】:

    你用错了numpy.pad

    1. 您的数组是 (6000,32,32),因此您只想填充轴 1 和 2,而不是轴 0。
    2. np.padpad_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]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2021-05-18
      • 2018-08-09
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多