Python 如何将许多文件上传到Google Colab?

Python 如何将许多文件上传到Google Colab?,python,machine-learning,jupyter,google-colaboratory,Python,Machine Learning,Jupyter,Google Colaboratory,我正在开发一个,我想在Google Colab上测试一下 对于培训数据集,我有700个图像,大部分是256x256,我需要将它们上传到项目的python numpy数组中。我还有数千个相应的掩码文件要上传。它们目前存在于GoogleDrive上的各种子文件夹中,但我无法将它们上传到GoogleColab以便在我的项目中使用 到目前为止,我已经尝试使用谷歌保险丝,它似乎有非常缓慢的上传速度和PyDrive,这给了我各种各样的身份验证错误。我大部分时间都在使用GoogleColab I/O示例代码

我正在开发一个,我想在Google Colab上测试一下

对于培训数据集,我有700个图像,大部分是
256x256
,我需要将它们上传到项目的python numpy数组中。我还有数千个相应的掩码文件要上传。它们目前存在于GoogleDrive上的各种子文件夹中,但我无法将它们上传到GoogleColab以便在我的项目中使用

到目前为止,我已经尝试使用谷歌保险丝,它似乎有非常缓慢的上传速度和PyDrive,这给了我各种各样的身份验证错误。我大部分时间都在使用GoogleColab I/O示例代码


我该怎么做?PyDrive会是一条路吗?是否有代码用于一次上载文件夹结构或多个文件?

您可能需要尝试
kaggle cli
模块,如前所述

您可以将所有数据放入谷歌硬盘,然后安装硬盘。我就是这样做的。让我分步解释

第1步: 将您的数据传输到您的谷歌硬盘

# Install a Drive FUSE wrapper.
# https://github.com/astrada/google-drive-ocamlfuse
!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



# Generate auth tokens for Colab
from google.colab import auth
auth.authenticate_user()


# Generate creds for the Drive FUSE library.
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}


# Create a directory and mount Google Drive using that directory.
!mkdir -p My Drive
!google-drive-ocamlfuse My Drive


!ls My Drive/

# Create a file in Drive.
!echo "This newly created file will appear in your Drive file list." > My Drive/created.txt
第二步: 运行以下代码装载您的google drive

# Install a Drive FUSE wrapper.
# https://github.com/astrada/google-drive-ocamlfuse
!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



# Generate auth tokens for Colab
from google.colab import auth
auth.authenticate_user()


# Generate creds for the Drive FUSE library.
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}


# Create a directory and mount Google Drive using that directory.
!mkdir -p My Drive
!google-drive-ocamlfuse My Drive


!ls My Drive/

# Create a file in Drive.
!echo "This newly created file will appear in your Drive file list." > My Drive/created.txt
第4步:

现在将数据加载到numpy数组中,如下所示。我有我的exel文件,有我的训练、履历和测试数据

train_data = pd.read_excel(r'Drive/train.xlsx')
test = pd.read_excel(r'Drive/test.xlsx')
cv= pd.read_excel(r'Drive/cv.xlsx')
我希望这能有所帮助

编辑

要将数据从colab笔记本环境下载到驱动器中,可以运行以下代码

# Install the PyDrive wrapper & import libraries.
# This only needs to be done once in a 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 in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)



# Create & upload a file.
uploaded = drive.CreateFile({'data.xlsx': 'data.xlsx'})
uploaded.SetContentFile('data.xlsx')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

下面是将大型数据集上传到GoogleColab的几个步骤

1.将数据集上传到免费云存储,如dropbox、openload等(我使用dropbox)
2.创建上传文件的可共享链接并复制。
3.在Google Colab中打开笔记本,并在其中一个单元格中运行此命令:

就这样
您可以使用以下命令将数据集压缩到zip或rar文件中,然后在Google Colab中下载数据集后将其unizp:

    !unzip downloaded_filename -d destination_folder

先压缩文件,然后上传到谷歌硬盘

请参见以下简单的解压命令:

!unzip {file_location}
例如:

!unzip drive/models.rar

步骤1:通过运行以下命令安装驱动器:

from google.colab import drive
drive.mount('/content/drive')
这将输出一个链接。单击链接,点击allow,复制授权码并将其粘贴到colab单元格中的框中,框上写有“输入您的授权码:”字样。 这个过程只是允许colab访问您的Google驱动器

步骤2:将文件夹(根据文件夹大小进行压缩或解压缩)上载到Google Drive

步骤3:现在进入驱动器目录和文件,找到上传的文件夹/压缩文件

此过程可能如下所示: 启动时,colab中的当前工作目录为/content/ 为了确保这一点,请在单元格中运行以下命令:

!pwd
它将显示您当前所在的目录。(pwd代表“打印工作目录”) 然后使用如下命令:

!ls
列出您所在目录中的目录和文件 和命令:

!cd /directory/name/of/your/choice
移动到目录中以查找上载的文件夹或上载的.zip文件

就这样,你已经准备好用你的机器学习模型弄脏你的手了!:)


希望这些简单的步骤可以避免您花费太多不必要的时间去弄清楚colab是如何工作的,而您实际上应该花大部分时间来弄清楚机器学习模型、其超参数、预处理…

有很多方法可以做到这一点:

  • 您可能希望
    将数据推送到github存储库中,然后在Google Colab code cell中运行:

    !!git克隆{repo}.git

  • 您可以将数据上载到
    谷歌硬盘
    ,然后在您的代码单元中:

  • 从google.colab导入驱动器

    drive.mount('/content/drive')

  • 使用transfer.sh工具:您可以访问此处了解其工作原理:


  • Google Colab让用户上传文件(从本地机器、Google drive或github)更加方便。您需要单击笔记本左侧的窗格,就可以访问存储在驱动器中的所有文件

    选择文件->右键单击->复制路径

    使用python导入方法从此路径导入文件,例如:

    import pandas as pd
    data = pd.read_csv('your copied path here')
    

    要一次性导入多个文件,您可能需要编写一个函数。

    由于某些原因,此方法不再有效,URL地址不会显示,只显示输入密钥的字段。@rraallvv…我正在执行此操作,它对我有效。我建议重新启动您的colab笔记本,然后再试一次。您可以尝试重新启动浏览器。如果仍然不起作用,请告诉我。@rraallvv..只需复制(在框中)
    AD5F235DF639B041
    或类似此键的内容,如果您运行我前面提到的第一段代码,它将出现在第三行。@abdulkarimkan感谢您的代码片段。如果我使用的数据集不在我的本地计算机上怎么办。我可以在安装了驱动器的情况下使用colab下载数据集并将其保存在驱动器上的文件夹中吗?@WassimSeifeddine..要将数据下载到您的google驱动器中,请查看我的更新答案。数据仅从您的谷歌硬盘上传和下载。然后,您可以将该数据传输到本地计算机。或者使用github保留所有文件并执行:
    git clone[your_REPO]
    能否请您专门为google colab详细说明,而不仅仅是编写链接?@subtleseek请查看此笔记本示例:不起作用。相反,先用wget下载zip文件,然后再解压。
    import pandas as pd
    data = pd.read_csv('your copied path here')