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

新手上路,请多包涵

我正在从事 图像分割机器学习项目,我想在 Google Colab 上对其进行测试。

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

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

我该怎么办? PyDrive 会是可行的方法吗?某处是否有用于一次上传文件夹结构或多个文件的代码?

原文由 Jesse Cambon 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 776
1 个回答

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

第 1 步: 将您的数据传输到您的 Google 云端硬盘。

第 2 步: 运行以下代码来挂载您的 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

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

 !ls Drive

第四步:

现在将您的数据加载到 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 notebook 环境将数据下载到您的驱动器中,您可以运行以下代码。

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

原文由 Stupid420 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏