如何在谷歌合作实验室上传数据集?
问题描述
我需要在 google colaboratory 中上传图像数据集.它里面有包含图像的子文件夹.我在网上找到的都是单个文件.
I need to upload dataset of images in google colaboratory. It has subfolder inside it which contains images. Whatever I found on the net was for the single file.
from google.colab import files
uploaded = files.upload()
有什么办法吗?
解决方案
要上传数据到 Colab,你有三个方法.
For uploading data to Colab, you have three methods.
方法一
您可以在 Colab UI 中直接上传文件或目录
You can directly upload file or directory in Colab UI
数据保存在 Colab 本地机器中.在我的实验中,有三个特点:1)上传速度不错.2)它将保持目录结构,但不会直接解压缩.您需要在 Colab 单元中执行此代码
The data is saved in Colab local machine. In my experiment, there are three features: 1) the upload speed is good. 2) it will remain directory structure but it will not unzip directly. You need to execute this code in Colab cell
!makedir {dir_name}
!unzip {zip_file} -d {dir_name}
3) 最重要的是,当 Colab 崩溃时,数据会被删除.
3) Most importantly, when Colab crashes, the data will be deleted.
方法二
在 Colab 单元格中执行代码:
Execute the code in Colab cell:
from google.colab import files
uploaded = files.upload()
在我的实验中,当您运行单元格时,它会出现上传按钮.当单元格执行指示器仍在运行时,您选择一个文件.1) 执行后,文件名会出现在结果面板中.2)刷新 Colab 文件,您将看到该文件.3) 或者执行!ls
,你会看到你的文件.如果不是,则文件没有成功上传.
In my experiment, when you run the cell, it appears the upload button. and when the cell executing indicator is still running, you choose a file. 1) After execution, the file name will appear in the result panel. 2)Refresh Colab files, you will see the file. 3) Or execute !ls
, you shall see you file. If not, the file is not uploaded successfully.
方法三
如果您的数据来自 kaggle,您可以使用 Kaggle API 将数据下载到 Colab 本地目录.
If your data is from kaggle, you can use Kaggle API to download data to Colab local directory.
方法四
将数据上传到 Google Drive,您可以使用 1)Google Drive Web Browser 或 2) Drive API (https://developers.google.com/drive/api/v3/quickstart/python).要访问驱动器数据,请在 Colab 中使用以下代码.
Upload data to Google Drive, you can use 1)Google Drive Web Browser or 2) Drive API (https://developers.google.com/drive/api/v3/quickstart/python). To access drive data, use the following code in Colab.
from google.colab import drive
drive.mount('/content/drive')
我建议将数据上传到 Google Drive,因为它是永久性的.
I would recommend uploading data to Google Drive because it is permanent.
相关文章