【问题标题】:Accessing '.pickle' file in Google Colab在 Google Colab 中访问“.pickle”文件
【发布时间】:2018-08-18 18:38:17
【问题描述】:

对于使用 Google 的 Colab 作为 ML 的首选工具,我还很陌生。

在我的实验中,我必须使用“notMNIST”数据集,并且我已在我的 Google Drive 中名为 Data 的文件夹下将“notMNIST”数据设置为 notMNIST.pickle

话虽如此,我想在我的 Google Colab 中访问这个“.pickle”文件,以便我可以使用这些数据。

有什么方法可以访问它吗?

我已阅读有关 * 的文档和一些问题,但他们谈到了上传、下载文件和/或处理“表格”。

但是,我想要的是在环境中加载notMNIST.pickle 文件并使用它进行进一步处理。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 你是如何解决这个问题的?我有同样的问题,无法弄清楚。如果可以,请你帮助我。谢谢。
  • @user4704857 在下面查看我的答案。

标签: python tensorflow google-data-api google-colaboratory


【解决方案1】:

您可以尝试以下方法:

import pickle
drive.mount('/content/drive')
DATA_PATH = "/content/drive/Data"
infile = open(DATA_PATH+'/notMNIST.pickle','rb')
best_model2 = pickle.load(infile)

【讨论】:

    【解决方案2】:

    Google Drive 中的数据存储在云中,并且在 colaboratory 中,Google 提供了一个个人 linux 虚拟机,您的笔记本将在该虚拟机上运行。因此您需要从 google drive 下载到您的 colaboratory 虚拟机并使用它。可以关注this下载教程

    【讨论】:

      【解决方案3】:

      您可以为此使用 pydrive。首先,您需要找到文件的 ID。

      # Install the PyDrive wrapper & import libraries.
      # This only needs to be done once per notebook.
      !pip install -U -q PyDrive
      from pydrive.auth import GoogleAuth
      from pydrive.drive import GoogleDrive
      from google.colab import auth
      from oauth2client.client import GoogleCredentials
      
      # Authenticate and create the PyDrive client.
      # This only needs to be done once per notebook.
      auth.authenticate_user()
      gauth = GoogleAuth()
      gauth.credentials = GoogleCredentials.get_application_default()
      drive = GoogleDrive(gauth)
      
      # Download a file based on its file ID.
      #
      # A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
      listed = drive.ListFile({'q': "title contains '.pkl' and 'root' in parents"}).GetList()
      for file in listed:
          print('title {}, id {}'.format(file['title'], file['id']))
      

      然后您可以使用以下代码加载文件:

      from googleapiclient.discovery import build
      drive_service = build('drive', 'v3')
      
      import io
      import pickle
      from googleapiclient.http import MediaIoBaseDownload
      
      file_id = 'laggVyWshwcyP6kEI-y_W3P8D26sz'
      
      request = drive_service.files().get_media(fileId=file_id)
      downloaded = io.BytesIO()
      downloader = MediaIoBaseDownload(downloaded, request)
      done = False
      while done is False:
          # _ is a placeholder for a progress object that we ignore.
          # (Our file is small, so we skip reporting progress.)
          _, done = downloader.next_chunk()
      
      downloaded.seek(0)
      f = pickle.load(downloaded)
      

      【讨论】:

        【解决方案4】:

        谢谢你们的回答。 Google Colab 已迅速成长为一个更成熟的开发环境,我最喜欢的功能是“文件”选项卡。

        我们可以轻松地将模型上传到我们想要的文件夹并像在本地机器上一样访问它。

        这解决了问题。

        谢谢。

        【讨论】: