【问题标题】:load image dataset (folder or zip) located in Google Drive to Google Colab?将位于 Google Drive 中的图像数据集(文件夹或 zip)加载到 Google Colab?
【发布时间】:2018-08-27 07:27:50
【问题描述】:

我的 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.

  2. Then I found this answer, but the solution is not finished, or at least I did not understand how to go on from unzipping. Unfortunately I am unable to comment this answer as I don't have enough "* points"

  3. I also found this thread, but here all the answer use other tools, such as Github or dropbox

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

编辑1:

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

【问题讨论】:

    标签: python neural-network google-colaboratory


    【解决方案1】:

    更新答案。您现在可以通过 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

    【讨论】:

      【解决方案2】:

      正如@yl_low here所提到的那样

      第 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 步都需要填写 URL 提供的验证码

      第 4 步:

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

      第 5 步:

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

      【讨论】:

      • 感谢您的回答。一开始它对我很有用,但我不接受它以接受更新的版本,这对遇到此问题的人更有用。
      【解决方案3】:

      其他答案非常好,但它们需要每次都在 Google Drive 中进行身份验证,如果您想自上而下地运行笔记本,这不是很舒服。

      我也有同样的需求,我想从 Drive 下载一个包含数据集的 zip 文件到 Colab。我更喜欢获取该文件的可共享链接并运行以下单元格(用您的共享链接替换 ​​drive_url):

      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!')
      

      【讨论】:

        【解决方案4】:

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

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

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

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

        (压缩文件被加载到colab中)

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

        获取路径:

        • 选择 google colab 左侧的文件
        • 点击三个点浏览文件
        • 复制路径

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

        【讨论】:

          最近更新 更多