Google Colab-- zero cost Fun depth study

Foreword

Recent study depth study HyperLPR when the project, because of the lack of appropriate training device model depth study, so the Internet to find models to provide training, after a period of searching, finally found a Google product - Google Colaboratory . It almost zero cost Fun deep learning, achieve rapid training model.

Google Colaboratory is Google's open depth study of a research tool, mainly for the development and research of deep learning. This tool is now free to use, but still can not determine the time being is not permanent free. Google Colab biggest benefit is to the majority of AI developers to provide a free GPU and TPU use! GPU model is Tesla K80 ! You can easily run in the example above: Keras, Tensorflow, Pytorch and other frameworks.

 

Google Colab basic operations

Website: Google Colab

Into the Google Colab website - "New Project

New ProjectNew Project

After you have created the project Colab we can enter the main interface.

Add code blockAdd code block

 

Now, we can enter some code in the code box. Note here that if we direct input code, the system will be treated as Python code execution. For example, we enter:

a = 1
print(a)

After running output box will print out "1."

operation resultOperating results

 

If you want to execute system commands, just add an exclamation point in front of the command !. For example, we enter: 

!ls

Results are as follows: 

operation resultOperating results

 

After performing the output box will display all files in the current directory folder. This is the command-line operation in not like Linux?

其实在Google Colab中连接的云端的那台虚拟机正是使用的Ubuntu操作系统,我们可以将自己在Google Colab中的一切操作看作是在用命令行控制云端的那台Ubuntu虚拟机。你可以用它来直接跑代码,也可以使用一些系统命令(我们后面要安装第三方软件都需要借助一系列的系统命令)。

 

前期配置

1. 修改笔记本环境

每新建一个Colab项目,都需要先对笔记本环境进行配置,运行类型选择是Python2还是Python3,硬件类型选择CPU、GPU或者TPU。(其中Python2是2.7版本,Python3是3.6版本)

Notebook Set​笔记本设置

 修改完后点击保存即可。

 

2. 安装必要的包和软件

在代码段中输入以下代码:

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

运行代码,运行中会提示输入验证码,点击程序给出的网址进行验证即可。

3. 挂载Google Drive

其实完成前面的操作我们就可以在Google Colab中敲写代码或者输入一些系统命令了,但是我们现在连接的虚拟机是和Google Drive脱离的,也就是说我们跑的程序无法使用谷歌云盘里的文件,这就非常受限制了。所以我们一般需要将谷歌云盘看作是虚拟机中的一个硬盘挂载,这样我们就可以使用虚拟机轻松访问谷歌云盘。
挂载Google Drive代码:

!mkdir -p drive
!google-drive-ocamlfuse drive

 运行挂载Google Drive代码会出现应认证的链接

Load Google Drive​装载Google Drive

 点击链接获得应用认证码

Application authentication code​应用认证码

 将应用认证码复制输入到下面的文本框中,点击回车键即可

Enter the application authentication code​输入应用认证码

加载成功之后在左边的文件中多了一个dirve文件夹

Loaded successfully​加载成功

 

 挂载完后在虚拟机中会多出一个文件夹"drive",我们可以用

!ls

命令查看。

 

更改工作目录

在Colab中cd命令是无效的,切换工作目录使用chdir函数。

import os
os.chdir("drive")

Execute the code above, the current working directory will be entered into the drive folder. We then use the !lscommand will find the system output is the directory under the drive folder.

Back to the parent directory:

os.chdir('../')

 

Run your own code

Well, various preparations are done, how we run their own code to write it directly on the Colab? Actually very simple, just like on your computer, use the command

!python XXX.py

On it! Detail steps are as follows:

1. Upload the .py file and other necessary files to Google Drive

Upload fast, do not worry about the speed issue -

 

2. Change the working directory to the directory where the file .py

import os
os.chdir('drive/Colab/Your project folder')

If you do not trust it with complete after switching !lscommand is not to look at the specified directory.

 

3. Run the code

!python XXX.py

 

4. Notes

Linux System file path using the '/' instead of '\'

 

to sum up

  1. Google Colab can be seen as an Ubuntu virtual machine with a GPU or the TPU , but we can only operate it with the command line. You can choose to perform system commands, you can write to run python code directly.

  2. End mount Google Drive, will generate a folder drive in the virtual machine directly to Google Drive as a hard disk can be. Access drive folder in the file is a file in your Google Drive access inside.

  3. Colab continuous use up to 12 hours , over time the system will be forced to cut off the running program and to recover occupied by the virtual machine. (Like after reconnected to the virtual machine, the virtual machine is emptied state, need to re-configure and install the library, etc.)

  4. Please use the science - of access.

Well, Google Colab to use it first introduced here, I am new to the author shortly, wrote this article using summary to share with you. And if there be a problem of the text, but please bear with me, I can point out mistakes, learn from each other in the comments area.


 

zeusee.com  Zhiyun view

Guess you like

Origin www.cnblogs.com/huiwei13/p/11797142.html