将位于 Google 的图像数据集(文件夹或 zip)加载到 Google Colab?

load image dataset (folder or zip) located in Google Drive to Google Colab?

我的 Google 驱动器上有一个图像数据集。我在压缩的 .zip 版本和未压缩的文件夹中都有此数据集。

我想使用 Google Colab 训练 CNN。我如何告诉 Colab 我的 Google 驱动器中的图像在哪里?

  1. official tutorial does not help me as it only shows how to upload single files, not a folder with 10000 images as in my case.

我希望有人能解释我需要做什么或告诉我在哪里可以找到帮助。

编辑 1:

I have found yet another thread asking the same question as mine: 遗憾的是,在 3 个答案中,有两个涉及 Kaggle,我不知道也没有使用。第三个答案提供了两个link。第一个 link 指的是我 link 编辑的第三个线程,第二个 link 仅说明如何手动上传单个文件。

如@yl_low所述

第 1 步:

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse

第 2 步:

from google.colab import auth
auth.authenticate_user()

第 3 步:

from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

第 2 步和第 3 步都需要填写网址提供的验证码

第 4 步:

!mkdir -p drive
!google-drive-ocamlfuse drive

第 5 步:

print('Files in Drive:')
!ls drive/

更新答案。您现在可以从 Google Colab

进行操作
# Load the Drive helper and mount
from google.colab import drive

# This will prompt for authorization.
drive.mount('/content/drive')

!ls "/content/drive/My Drive"

Google Documentation

其他答案很好,但它们每次都需要在 Google 驱动器中进行身份验证,如果你想 运行 top down 你的笔记本,那不是很舒服。

我有同样的需求,我想从 Drive 下载一个包含数据集的 zip 文件到 Colab。我更喜欢获取该文件的可共享 link 和下面的单元格 运行(将 drive_url 替换为您共享的 link):

import urllib

drive_url = 'https://drive.google.com/uc?export=download&id=1fBVMX66SlvrYa0oIau1lxt1_Vy-XYZWG'
file_name = 'downloaded.zip'

urllib.request.urlretrieve(drive_url, file_name)
print('Download completed!')

我看到并尝试了以上所有方法,但对我不起作用。所以这里有一个简单的解决方案和简单的解释,可以帮助您加载 .zip 图像文件夹并从中提取图像。

  • 连接到 google 驱动器
    from google.colab import drive
    drive.mount('/content/drive')
    

(您将获得 link 登录您的 google 帐户并复制代码并粘贴到 colab 中要求的代码上)

  • 安装并导入keras库
    !pip install -q keras
    import keras
    
    

(zip 文件已加载到 colab 中)

  • 解压文件夹
    ! unzip 'zip-file-path'
    

获取路径:

  • select 文件位于 google colab
  • 左侧
  • 浏览文件点击 3 个点
  • 复制路径

现在解压缩的图像文件夹已加载到您的 colab 中,您可以随意使用它