如何在python中加载图像数据集?'Websocket ping timeout'是什么意思?



我在window7中使用python3(anaconda(。

为了研究深度学习,我下载了图像数据集。
(http://www.cs.princeton.edu/~andyz/downloads/MMU2IrisDatabase.zip(
图像数据集:995张图像,65.4MB。

但是,在加载数据集时,我遇到了 2 个问题。

首先,我尝试了solution_1。

# Solution_1
import os
import numpy as np
import zipfile
from PIL import Image
from io import StringIO, BytesIO
from scipy import misc
def import_caltec100(dataset = 'MMU2IrisDatabase.zip'): # file: 'MMU2IrisDatabase.zip'
X_array = np.empty((1, 238, 320, 3))
Y_list = list()
with zipfile.ZipFile(dataset) as caltec:
for i, file in enumerate(caltec.namelist()):
if file.split('.')[-1] == 'bmp' and len(file.split('/')[2].split('.')[0]) == 6:
img_array = misc.imread(BytesIO(caltec.read(file)))
X_array = np.vstack((X_array, img_array.reshape(1, 238, 320, 3)))
Y_list.append(int(file.split('/')[2].split('.')[0][:2]))
elif file.split('.')[-1] == 'bmp' and len(file.split('/')[2].split('.')[0]) == 7:
img_array = misc.imread(BytesIO(caltec.read(file)))
X_array = np.vstack((X_array, img_array.reshape(1, 238, 320, 3)))
Y_list.append(int(file.split('/')[2].split('.')[0][:3]))
X_array = X_array[1:]
Y_array = np.array(Y_list)
return X_array, Y_array

它在 jupyter 中返回"119995ms 之后的 Websocket ping 超时"。

# Solution_2
X_array = np.empty((1, 238, 320, 3))
Y_list = list()
for file in os.listdir('MMU2IrisDatabase'):
if file.split('.')[-1] == 'bmp' and len(file.split('.')[0]) == 6:
img_array = misc.imread('MMU2IrisDatabase/{}'.format(file))
X_array = np.vstack((X_array, img_array.reshape(1, 238, 320, 3)))
Y_list.append(int(file.split('.')[0][:2]))
elif file.split('.')[-1] == 'bmp' and len(file.split('.')[0]) == 7:
img_array = misc.imread('MMU2IrisDatabase/{}'.format(file))
X_array = np.vstack((X_array, img_array.reshape(1, 238, 320, 3)))
Y_list.append(int(file.split('.')[0][:3]))
X_array = X_array[1:]
Y_array = np.array(Y_list)

没有错误,但花了太长时间(壁时间:32分43秒(。

请解决我的困难。

首先,我想知道"119995ms 之后的 Websocket ping 超时"是什么意思。 其次,有没有更好的方法来加载图像数据集?

第二个解决方案的问题在于您一直在重新创建整个数组。我所做的是这样的:

img_data = []
for file in os.listdir(dir):
if file.endswith('bmp'):
img_data.append(misc.imread(os.path.join(dir, file)).reshape(238, 320, 3))
X_array = np.array(img_data, dtype=np.float32)

无论如何,如果你想知道是什么让程序花了这么长时间,你应该尝试分析器(https://docs.python.org/2/library/profile.html(。

最新更新