加速numpy数组连接

时间:2017-07-31 15:33:46

标签: python arrays numpy concatenation

我有以下代码。我将cp_X_train从RGB转换为灰度,并将其与X_train_gray连接。

X_train_gray = np.empty([0, 32, 32, 1])

start = timer()
for i in range(cp_X_train.shape[0]):
    if i % 1000 == 0:
        print(i)
        end = timer()
        print(end - start)
        start = timer()
    gray_img = cv2.cvtColor(cp_X_train[i], cv2.COLOR_BGR2GRAY)[None, :, :, None]
    X_train_gray = np.concatenate((X_train_gray, gray_img), axis=0)

我每1000个样本打印出处理时间。

0
0.00042258699977537617
1000
3.331055953000032
2000
9.222047281000414
3000
15.596254615000362
4000
21.37355997799932
5000
27.513121935999152
6000
33.477182841001195
7000
40.4089376539996
8000
47.39131554400046
9000
53.73745651799982

如您所见,一开始,处理时间很短。但随着X_train_gray变大,处理时间变得越来越大。我该如何解决这种情况?

0 个答案:

没有答案