从Google云端硬盘读取图像时,Google Colab太慢了

时间:2019-11-30 22:27:10

标签: keras deep-learning conv-neural-network google-colaboratory

我有自己的深度学习项目数据集。我将其上传到Google云端硬盘,并将其链接到Colab页面。但是Colab每秒只能读取2-3张图像,而我的计算机可以读取数十张图像。 (我使用imread读取图像。)

keras 的模型编译过程没有速度问题,仅从Google云端硬盘读取图像即可。有人知道解决方案吗?有人也遭受了这个问题的困扰,但是仍然没有解决:Google Colab very slow reading data (images) from Google Drive(我知道这是链接中问题的重复,但是我重新发布了它,因为它没有解决。我希望这不是一个违规行为。堆栈溢出规则。)

编辑:我用于读取图像的代码段:

def getDataset(path, classes, pixel=32, rate=0.8):
    X = []
    Y = []

    i = 0
    # getting images:
    for root, _, files in os.walk(path):
        for file in files:
            imagePath = os.path.join(root, file)
            className = os.path.basename(root)

            try:
                image = Image.open(imagePath)
                image = np.asarray(image)
                image = np.array(Image.fromarray(image.astype('uint8')).resize((pixel, pixel)))
                image = image if len(image.shape) == 3 else color.gray2rgb(image)
                X.append(image)
                Y.append(classes[className])
            except:
                print(file, "could not be opened")

    X = np.asarray(X, dtype=np.float32)
    Y = np.asarray(Y, dtype=np.int16).reshape(1, -1)

    return shuffleDataset(X, Y, rate)

3 个答案:

答案 0 :(得分:4)

我想提供有关解压缩文件实际外观的更详细的答案。这是加快读取数据速度的最佳方法,因为将文件解压缩到VM磁盘比从云端硬盘单独读取每个文件要快得多。

假设您在本地计算机的“数据”文件夹中拥有所需的图像或数据。压缩数据以获取Data.zip并将其上传到云端硬盘。

现在,安装驱动器并运行以下命令:

!unzip "/content/drive/My Drive/path/to/Data.Zip" -d "/content"

只需修改所有图像路径即可通过/ content / Data,读取图像会更快得多。

答案 1 :(得分:2)

将zip文件上传到驱动器。转移到colab后,将其解压缩。文件复制的开销很繁琐,因此您不应该复制大量文件,而应该复制单个zip和unzip。

答案 2 :(得分:1)

我建议您将文件上传到GitHub,然后将其克隆到Colab。它可以将我的培训时间从1小时减少到3分钟。