首页 文章

如何从本地驱动器上传大数据并将其保存到Google Colaboratory?

提问于
浏览
5

我从这个Kaggle链接下载了大图像训练数据作为zip

https://www.kaggle.com/c/yelp-restaurant-photo-classification/data

我如何有效地实现以下目标?

  • 在Google Colaboratory中创建项目文件夹

  • 将zip文件上传到项目文件夹

  • 解压缩文件

谢谢

编辑:我尝试了下面的代码,但它崩溃了我的大型zip文件 . 有没有更好/更有效的方法来执行此操作,我可以在本地驱动器中指定文件的位置?

from google.colab import files
uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

5 回答

  • 3
    !pip install kaggle
    api_token = {"username":"USERNAME","key":"API_KEY"}
    import json
    import zipfile
    import os
    with open('/content/.kaggle/kaggle.json', 'w') as file:
        json.dump(api_token, file)
    !chmod 600 /content/.kaggle/kaggle.json
    !kaggle config set -n path -v /content
    !kaggle competitions download -c jigsaw-toxic-comment-classification-challenge
    os.chdir('/content/competitions/jigsaw-toxic-comment-classification-challenge')
    for file in os.listdir():
        zip_ref = zipfile.ZipFile(file, 'r')
        zip_ref.extractall()
        zip_ref.close()
    

    第9行有一些细微的变化,没有遇到错误 . 来源:https://gist.github.com/jayspeidell/d10b84b8d3da52df723beacc5b15cb27无法添加为评论原因代表 .

  • 0

    你可以参考这些主题:

    另请查看I/O example notebook . 例如,要访问 xls 文件,您需要将文件上传到Google表格 . 然后,您可以在同一I / O示例笔记本中使用 gspread 配方 .

  • 2

    您可能需要使用 kaggle-cli 模块来帮助下载 .

    它在this fast.ai thread中讨论过 .

  • 1

    我刚刚编写了这个脚本,可以将Kaggle API中的数据下载并提取到Colab笔记本中 . 您只需粘贴用户名,API密钥和竞争名称即可 .

    https://gist.github.com/jayspeidell/d10b84b8d3da52df723beacc5b15cb27

    Colab中的手动上传功能现在有点儿麻烦,最好通过wget或API服务下载文件,因为每次打开笔记本时都要使用新的VM . 这样数据将自动下载 .

  • 0

    另一个选择是将数据上传到Dropbox(如果它适合),获取下载链接 . 然后在笔记本上做

    !wget link -0 new-name && ls
    

相关问题