colab数据的加载方式

1. colab挂载google硬盘

最方便。

#第一种方式
from google.colab import drive
drive.mount('/content/gdrive')

2. 第二种方式

通过代码进行文件的上传或者下载。

# 第二种方式,可以进行文件处理
!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({'title': 'model.zip'})
uploaded.SetContentFile('model.zip')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

# 或者下载文件(举例)  在google drive中的文件ID
download = drive.CreateFile({'id': '1BZOv422XJvxFUnGh-0xVeSvgFgqVY45q'})

download.GetContentFile('train_LbELtWX.zip')

!unzip train_LbELtWX.zip

3. 第三种方式

生成硬盘挂载文件夹gdrive,之后可以利用正常的linux环境下的文件处理命令,比较方便。

!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
from google.colab import auth
auth.authenticate_user()
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}

#生成gdrive文件夹
!mkdir -p gdrive
!google-drive-ocamlfuse gdrive 

4.将本地文件与虚拟机之间的上传或者下载

from google.colab import files

uploaded= files.upload()
for fn in uploaded.keys():
 print('User uploaded file
	"{name}" with length {length} bytes'.format(name=fn,
length=len(uploaded[fn])))

from google.colab import files
files.download('./weight.best.hdf5')
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值