在包含图像的数组中创建批次

Creating batches within an array containing images

我有一个包含 9957 张图像的数组 X_train。我正在制作一个卷积 network.The 用于输入模型的阵列的所需形状是(批次大小、通道、高度、宽度)

X_train.shape #gives (9957, 60, 80, 3)
X_train[1].shape #gives (60, 80, 3)

如果我们使用

np.reshape(X_train,(-1, 3, 60, 80)) #it gives (9957, 3, 60, 80)

如何获得形状为 (batchsize, 3, 60, 80) 的每个数组和形状为 (9957, batchsize, 3, 60, 80) 的用于训练的最终图像数组?

您可以从第i张图片到第i + batchsize张图片如下。

batchsize = 16
i = 0

X_batch = X_train[i: i+batchsize]
print('X_batch.shape: ', X_batch.shape)  # it should be (16, 3, 60, 80)

请用 for 循环更改 i 以获取每张图像。例如,

for i in range(0, len(X_train), batchsize):
    X_batch = X_train[i: i+batchsize]

    # --- Do something with X_batch ---