python - Google Cloud Machine Learning 如何处理大量 HDF5 文件?

标签 python tensorflow h5py google-cloud-ml

我有大约 5k 个原始数据输入文件和 15k 个原始数据测试文件,总共有几个 GB。由于这些是原始数据文件,我必须在 Matlab 中迭代处理它们以获得我想要在 (CNN) 上训练我的实际分类器的特征。因此,我为每个原始数据文件生成了一个 HDF5 mat 文件。我使用 Keras 在本地开发了我的模型,并修改了 DirectoryIterator,其中我有类似

的内容
for i, j in enumerate(batch_index_array):
            arr = np.array(h5py.File(os.path.join(self.directory, self.filenames[j]), "r").get(self.variable))
            # process them further

文件结构是

|  
|--train  
|    |--Class1
|    |    |-- 2,5k .mat files  
|    |      
|    |--Class2
|         |-- 2,5k .mat files  
|--eval  
|    |--Class1
|    |    |-- 2k .mat files  
|    |      
|    |--Class2
|         |-- 13k .mat files

这是我现在在我的 Google ML 存储桶中的文件结构。它在本地使用带有小型模型的 python,但现在我想利用 Google ML 超参数调整功能,因为我的模型要大得多。问题是我在 Internet 上读到 HDF5 文件无法直接且轻松地从 Google ML 存储中读取。我试图像这样修改我的脚本:

import tensorflow as tf
from tensorflow.python.lib.io import file_io

for i, j in enumerate(batch_index_array):
    with file_io.FileIO((os.path.join(self.directory, self.filenames[j], mode='r') as input_f:
        arr = np.array(h5py.File(input_f.read(), "r").get(self.variable))
        # process them further

但它给我类似的错误 error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte只是与其他十六进制和位置 512。
我也有这样的事情:

import tensorflow as tf
from tensorflow.python.lib.io import file_io

for i, j in enumerate(batch_index_array):
    with file_io.FileIO((os.path.join(self.directory, self.filenames[j], mode='rb') as input_f:
        arr = np.fromstring(input_f.read())
        # process them further

但它也不起作用。

问题
如何修改我的脚本以便能够读取 Google ML 中的那些 HDF5 文件?我知道数据 pickling 的做法,但问题是将 15k 文件(几 GB)创建的 pickle 加载到内存似乎效率不高。

最佳答案

HDF 是一种非常常见的文件格式,不幸的是,它在云中并不是最佳选择。有关原因的一些解释,请参阅 this博文。

鉴于云上 HDF 的固有复杂性,我推荐以下之一:

  1. 将您的数据转换为另一种文件格式,例如 CSV 或 tf.Example 的 TFRecord
  2. 将数据复制到本地/tmp

转换最多可能是不方便的,而且对于某些数据集,可能需要体操。在 Internet 上粗略地搜索一下,发现了有关如何执行此操作的多个教程。 Here's one你可能会引用。

同样,有多种方法可以将数据复制到本地机器上,但请注意,在复制数据之前,您的作业不会开始进行任何实际训练。此外,如果其中一名 worker 死亡,它将在再次启动时必须重新复制所有数据。如果 master 死了,而你正在做分布式训练,这会导致很多工作丢失。

就是说,如果您觉得这对您的情况来说是一种可行的方法(例如,您没有进行分布式训练和/或您愿意等待如上所述的数据传输),只需使用以下命令启动 Python像这样的东西:

import os
import subprocess

if os.environ.get('TFCONFIG', {}).get('task', {}).get('type') != 'ps':
  subprocess.check_call(['mkdir', '/tmp/my_files'])
  subprocess.check_call(['gsutil', '-m', 'cp', '-r', 'gs://my/bucket/my_subdir', '/tmp/myfiles']) 

关于python - Google Cloud Machine Learning 如何处理大量 HDF5 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48986640/

相关文章:

python - 如何查看动态库文件(.dylib)的函数定义?

python - 简化以下代码的最佳方法是什么?

python - 在Python中解码JSON

c++ - 使用 C API 更改 Tensorflow 推理的线程数

python - 使用多处理和 h5py

c++ - 将 C++ 字符串传递给 Python

python - 无法将函数转换为张量或运算。 tensorflow 错误

python - 无法导入tensorflow(使用pycharm IDE)

python - 将多个具有层次结构的 pd.DataFrame 保存到 hdf5

python - 有没有办法在使用 Group.visititems 时获取数据集或组的父级?