使用Google Colab.md 1.6 KB

1.基础篇

Google Colab的基础使用


2.Google Colab初始化

2.1安装必要的库

!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}

2.2挂在目录

google colab要挂载在当前目录

!ls

得到当前的目录image-20210416184645628

进入当前文件夹,更换目录

import sys
from google.colab import drive
# 这个命令是将Drive挂载到‘/content/drive’下,查看此时的目录就知道了
drive.mount('/content/drive')
# 加入系统路径
sys.path.append('/content/drive/MyDrive/Palmprint')
# 进入当前路径
%cd /content/drive/MyDrive/Palmprint


3.查看gup使用情况

torch.cuda.is_available()
cuda是否可用;

torch.cuda.device_count()
返回gpu数量;

torch.cuda.get_device_name(0)
返回gpu名字,设备索引默认从0开始;

torch.cuda.current_device()
返回当前设备索引;

!/opt/bin/nvidia-smi
查看GPU情况