【问题标题】:Using Custom Libraries in Google Colab without Mounting Drive在 Google Colab 中使用自定义库而不安装驱动器
【发布时间】:2019-06-20 13:06:21
【问题描述】:

我正在使用 Google Colab,我想使用我存储在本地计算机上的自定义库/脚本。我目前的做法如下:

# (Question 1)
from google.colab import drive
drive.mount("/content/gdrive")

# Annoying chain of granting access to Google Colab
# and entering the OAuth token.

然后我使用:

# (Question 2)
!cp /content/gdrive/My\ Drive/awesome-project/*.py .

问题 1: 有没有办法完全避免安装驱动器?每当执行上下文发生变化时(例如,当我选择“硬件加速 = GPU”时,或者当我等待一个小时时),我必须重新生成并重新输入 OAuth 令牌。

问题 2: 有没有办法更优雅地在我的本地机器和我的 Google Colab 脚本之间同步文件?

关于问题 1 的部分(不是很满意的答案):我看到有人可以install and use Dropbox。然后,您可以将 API 密钥硬编码到应用程序中并完成挂载,无论它是否是新的执行上下文。我想知道是否也存在基于 Google Drive 的类似方法。

【问题讨论】:

    标签: python oauth google-drive-api google-colaboratory


    【解决方案1】:

    如果您的代码不是秘密的,您可以使用git 将您的本地代码同步到 github。然后,无需任何身份验证即可 git clone 到 Colab。

    【讨论】:

      【解决方案2】:

      问题 1。 很好的问题,是的,我一直在使用这种解决方法,如果您是一名研究人员并希望其他人能够重新运行您的代码,或者在处理更大的数据集时只是“合作”,这将特别有用。以下方法作为一个团队运作良好,每个人都有自己的数据集版本存在挑战。

      我经常在下载并解压缩到 colab 运行时的 30 + Gb 图像文件上使用它。

      文件 ID 在您从 Google Drive 共享时提供的链接中

      您还可以选择多个文件并选择全部共享,然后生成例如 .txt 或 .json 文件,您可以解析和提取文件 ID。

      from google_drive_downloader import GoogleDriveDownloader as gdd 
      
      #some file id/ list of file ids parsed from file urls.
      
      google_fid_id = '1-4PbytN2awBviPS4Brrb4puhzFb555g2'
      destination = 'dir/dir/fid'
      #if zip file ad kwarg unzip=true
      
      gdd.download_file_from_google_drive(file_id=google_fid_id,
      destination, unzip=True)
      

      从 url 列表中获取文件 id 的 url 解析函数可能如下所示:

      def parse_urls():
          with open('/dir/dir/files_urls.txt', 'r') as fb:
              txt = fb.readlines()
          return [url.split('/')[-2] for url in txt[0].split(',')]
      

      一个健康警告是,对于相同的文件,您只能在 24 小时内重复此操作少量次。

      这是 gdd git repo:

      https://github.com/ndrplz/google-drive-downloader

      这是一个工作示例(我自己的),说明它如何在更大的脚本中工作:

      https://github.com/fdsig/image_utils

      问题 2。

      您可以连接到本地运行时,但这也意味着使用本地资源 gpu/cpu 等。

      真的希望这会有所帮助:-)。

      F~

      【讨论】:

        猜你喜欢
        • 2021-09-01
        • 2020-06-09
        • 2020-11-10
        • 2020-07-25
        • 2021-12-17
        • 2019-07-17
        • 1970-01-01
        • 2021-10-08
        • 2018-10-14
        相关资源
        最近更新 更多