从 Google Drive 读取图像时,Google Colab 速度太慢

Atr*_*dex 6 deep-learning conv-neural-network keras google-colaboratory

我有自己的深度学习项目数据集。我将其上传到 Google Drive 并将其链接到 Colab 页面。但是 Colab 一秒钟只能读取 2-3 张图片,而我的电脑可以读取几十张图片。(我使用 imread 来读取图像。)

keras 的模型编译过程没有速度问题,但只能从 Google Drive 读取图像。有人知道解决方案吗?有人也遇到过这个问题,但仍未解决:Google Colab 从 Google Drive 读取数据(图像)的速度非常慢(我知道这是链接中问题的重复,但我重新发布了它,因为它仍未解决。我希望这不会违反 Stack Overflow 规则。)

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

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)
Run Code Online (Sandbox Code Playgroud)

Ale*_*ick 11

我想提供一个更详细的答案,了解解压缩文件的实际情况。这是加速读取数据的最佳方法,因为将文件解压缩到 VM 磁盘比从 Drive 单独读取每个文件要快得多。

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

现在,挂载您的驱动器并运行以下命令:

!unzip "/content/drive/My Drive/path/to/Data.Zip" -d "/content"
Run Code Online (Sandbox Code Playgroud)

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