Python 写入HDF5并洗牌大数据数组
我已经下载了加州理工101。其结构是:Python 写入HDF5并洗牌大数据数组,python,memory,hdf5,h5py,Python,Memory,Hdf5,H5py,我已经下载了加州理工101。其结构是: #加州理工学院101分院 #一级主管 #一级jpg图片 #二级主管 #class2 JPG的图像 ... #100级目录 #class100 JPG的图像 我的问题是,我无法在内存中保存两个np数组x和y,它们的形状(9144240180,3)和(9144)。因此,我的解决方案是过度分配一个h5py数据集,将它们分为两块加载,然后逐个写入文件。确切地说: from __future__ import print_function import os imp
#加州理工学院101分院
#一级主管
#一级jpg图片
#二级主管
#class2 JPG的图像
...
#100级目录
#class100 JPG的图像
我的问题是,我无法在内存中保存两个np数组x
和y
,它们的形状(9144240180,3)
和(9144)
。因此,我的解决方案是过度分配一个h5py数据集,将它们分为两块加载,然后逐个写入文件。确切地说:
from __future__ import print_function
import os
import glob
from scipy.misc import imread, imresize
from sklearn.utils import shuffle
import numpy as np
import h5py
from time import time
def load_chunk(images_dset, labels_dset, chunk_of_classes, counter, type_key, prev_chunk_length):
# getting images and processing
xtmp = []
ytmp = []
for label in chunk_of_classes:
img_list = sorted(glob.glob(os.path.join(dir_name, label, "*.jpg")))
for img in img_list:
img = imread(img, mode='RGB')
img = imresize(img, (240, 180))
xtmp.append(img)
ytmp.append(label)
print(label, 'done')
x = np.concatenate([arr[np.newaxis] for arr in xtmp])
y = np.array(ytmp, dtype=type_key)
print('x: ', type(x), np.shape(x), 'y: ', type(y), np.shape(y))
# writing to dataset
a = time()
images_dset[prev_chunk_length:prev_chunk_length+x.shape[0], :, :, :] = x
print(labels_dset.shape)
print(y.shape, y.shape[0])
print(type(y), y.dtype)
print(prev_chunk_length)
labels_dset[prev_chunk_length:prev_chunk_length+y.shape[0]] = y
b = time()
print('Chunk', counter, 'written in', b-a, 'seconds')
return prev_chunk_length+x.shape[0]
def write_to_file(remove_DS_Store):
if os.path.isfile('caltech101.h5'):
print('File exists already')
return
else:
# the name of each dir is the name of a class
classes = os.listdir(dir_name)
if remove_DS_Store:
classes.pop(0) # removes .DS_Store - may not be used on other terminals
# need the dtype of y in order to initialize h5 dataset
s = ''
key_type_y = s.join(['S', str(len(max(classes, key=len)))])
classes = np.array(classes, dtype=key_type_y)
# number of chunks in which the dataset must be divided
nb_chunks = 2
nb_chunks_loaded = 0
prev_chunk_length = 0
# open file and allocating a dataset
f = h5py.File('caltech101.h5', 'a')
imgs = f.create_dataset('images', shape=(9144, 240, 180, 3), dtype='uint8')
labels = f.create_dataset('labels', shape=(9144,), dtype=key_type_y)
for class_sublist in np.array_split(classes, nb_chunks):
# loading chunk by chunk in a function to avoid memory overhead
prev_chunk_length = load_chunk(imgs, labels, class_sublist, nb_chunks_loaded, key_type_y, prev_chunk_length)
nb_chunks_loaded += 1
f.close()
print('Images and labels saved to \'caltech101.h5\'')
return
dir_name = '../Datasets/Caltech101'
write_to_file(remove_DS_Store=True)
这很有效,而且阅读速度也很快。问题是我需要洗牌数据集
意见:
- 洗牌数据集对象:显然速度很慢,因为它们在磁盘上
- 创建无序索引数组并使用高级numpy索引。这意味着从文件读取的速度较慢
- 在写入文件之前洗牌会很好,但问题是:我每次只有大约一半的数据集在内存中。我会受到不适当的洗牌
你能想出一个在写作前洗牌的方法吗?我也愿意接受重新思考写入过程的解决方案,只要它不占用大量内存。在读取图像数据之前,您可以洗牌文件路径 创建属于数据集的所有文件路径的列表,而不是在内存中洗牌图像数据。然后洗牌文件路径列表。现在您可以像以前一样创建HDF5数据库了 例如,您可以使用
glob
创建文件列表以进行随机播放:
import glob
import random
files = glob.glob('../Datasets/Caltech101/*/*.jpg')
shuffeled_files = random.shuffle(files)
然后,您可以从以下路径检索类标签和图像名称:
import os
for file_path in shuffeled_files:
label = os.path.basename(os.path.dirname(file_path))
image_id = os.path.splitext(os.path.basename(file_path))[0]
这种方法的问题是,目录的名称是我的标签,我应该创建一个地图图像->标签。好的,您刚刚回答了我:DI添加了一个示例,说明如何从路径中检索id和标签。刚刚意识到,您也想存储标签(当然):)