如何上传多个文件到Google Colab?

2024-05-23 20:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

对于训练数据集,我有700个图像,大部分是256x256,我需要将它们上传到python numpy数组中以用于我的项目。我也有数千个相应的面具文件上传。它们目前存在于Google drive的许多子文件夹中,但无法上载到Google Colab以供在我的项目中使用。

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

我该怎么办?PyDrive会是去的路吗?一次上传一个文件夹结构或多个文件是否有代码?


Tags: 文件数据项目代码图像imagenumpyproject
3条回答

您可以将所有数据放入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')))

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

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

    !wget your_shareable_file_link

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

    !unzip downloaded_filename -d destination_folder

先压缩你的文件,然后上传到谷歌驱动器。

请参阅此简单命令以解压缩:

!unzip {file_location}

示例:

!unzip drive/models.rar

相关问题 更多 >