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

时间:2018-02-19 23:36:17

标签: python machine-learning jupyter google-colaboratory

我正在研究image segmentation machine learning project,我想在Google Colab上测试一下。

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

到目前为止,我一直尝试使用Google Fuse,它似乎上传速度非常慢,PyDrive给我带来了各种身份验证错误。我大部分时间都在使用Google Colab I / O示例代码。

我应该怎么做? PyDrive会成为可行的方式吗?是否有代码用于一次上传文件夹结构或许多文件?

7 个答案:

答案 0 :(得分:6)

您可以将所有数据放入Google云端硬盘中,然后装入云端硬盘。这就是我做到的。让我分一步解释。

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

第2步: 运行以下代码以安装google驱动器。

# 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

第3步: 运行以下行以检查是否可以在装入的驱动器中看到所需的数据。

!ls Drive

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

答案 1 :(得分:3)

以下是将大型数据集上传到Google Colab的几个步骤

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

    !wget your_shareable_file_link

那就是它! 您可以压缩zip或rar文件中的数据集,稍后在使用此命令在Google Colab中下载后将其解锁:

    !unzip downloaded_filename -d destination_folder

答案 2 :(得分:1)

首先压缩文件,然后将其上传到Google云端硬盘。

查看以下简单命令以解压缩:

!unzip {file_location}

示例:

!unzip drive/models.rar

答案 3 :(得分:1)

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

x = re.search(letters, guess)
if (x):
  print("yes")
else:
  print("no")

这将输出一个链接。单击该链接,单击允许,复制授权码,并将其粘贴在colab单元格中的框中,并在其上方加上“输入您的授权码:”文本。 此过程只是授予合作伙伴访问您的Google云端硬盘的权限。

步骤2 :将您的文件夹(根据文件夹的大小压缩或解压缩)上载到Google云端硬盘

第3步:现在,您可以进入云端硬盘目录和文件,找到已上传的文件夹/压缩文件。

此过程可能类似于以下内容: 开始时,colab中的当前工作目录为/ content /。 只是为了确保在单元格中运行以下命令:

from google.colab import drive
drive.mount('/content/drive')

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

!pwd

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

!ls

进入目录以找到您上载的文件夹或上载的.zip文件。

就这样,您已经准备好使用机器学习模型了! :)

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

答案 4 :(得分:0)

您可能需要尝试kaggle-cli模块,如所讨论的here

答案 5 :(得分:0)

有很多方法可以做到:

  1. 您可能希望将数据push放入github存储库,然后可以在Google Colab代码单元中运行:

    !git clone https://www.github.com/ {repo} .git

  2. 您可以将数据上传到Google drive,然后在代码单元中:

from google.colab import drive

drive.mount('/content/drive')

  1. 使用transfer.sh工具:您可以在此处查看其工作原理:

    transfer.sh

答案 6 :(得分:0)

Google Colab使用户(从本地计算机,Google驱动器或github)上传文件更加方便。您需要在笔记本左侧的窗格上单击Mount Drive Option,然后才能访问驱动器中存储的所有文件。

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

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

juice()

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