- Google Colab Free GPU Tutorial
- Author: Fuat
- Translation from: The Gold Project
- This article is permalink: github.com/xitu/gold-m…
- Translator: haiyang – tju
- Proofreader: DevMcryYu
Google Colab free GPU tutorial
Now you can use Google Colaboratory (with the free Tesla K80 GPU) to develop deep learning programs using Keras, Tensorflow, and PyTorch.
Everybody is good! I’m going to show you how to use Google Colab, Google’s free cloud service for AI developers. On Colab, you can use a free GPU to develop deep learning applications.
Thank you so much for KDnuggets.
I’m pleased to announce that this blog post has been selected as KDnuggets’ silver blog post for February 2018! The article can be found at KDnuggets.
What is Google Colab?
Google Colab is a free cloud service, and now it also supports free Gpus!
You can:
- Improve your Python coding skills.
- Develop deep learning applications using popular libraries such as Keras, TensorFlow, PyTorch, and OpenCV.
The most important difference between Colab and other free cloud services is that Colab offers gpus that are completely free.
Detailed information about this service can be found on the FAQ page.
Get ready to use Google Colab
Create folders on Google Drive
Since Colab works on Google Drive, we need to specify the working folder first. I created a folder on Google Drive called “Apps.” Of course, you can use a different name or opt for the default Colab Notebooks folder instead of the App folder.
I created an empty “app” folder
Create a new Colab Notebook
Create a new note by right-clicking on the > More > Colaboratory step.
Right click > More > Colaboratory
Rename the note by clicking on the file name
Set up a free GPU
You can change the default hardware from the CPU to the GPU, or vice versa, in very simple steps. Follow the steps below to Edit > Notebook Settings or go to Runtime > Change Runtime Type and select GPU as Hardware Accelerator.
Run basic Python code using Google Colab
Now we can start using Google Colab.
I’ll run some code from the Python Numpy tutorial on basic data types.
It works! 🙂 if you are not familiar with Python, the most popular programming language in AI, I recommend this tutorial.
Run or import the.py file in Google Colab
Run the code first to install some of the necessary libraries and perform authorization.
from google.colab import drive
drive.mount('/content/drive/')
Copy the code
Running the above code gives the following result:
Click the link, copy the validation code and paste it into the text box below.
After completing the authorization process, you should see:
Now you can access your Google Drive with the following command:
! ls"/content/drive/My Drive/"
Copy the code
Install Keras:
! pip install -q kerasCopy the code
Upload the file mnist_cnn.py to your Google Drive apps folder.
Mnist_cnn. py Specifies the content of the file
Run the following code on the MNIST dataset to train a simple convnet.
! python3"/content/drive/My Drive/app/mnist_cnn.py"
Copy the code
As you can see from the results, each epoch runs in just 11 seconds.
Download the Titanic dataset (.csv file) and display the first 5 lines of the file
If you want to download a.csv file from a URL into the “app” folder, simply run the following command:
! Wget raw.githubusercontent.com/vincentarel… -P “/content/drive/My Drive/app”
Instead of using the wget method, you can upload your.csv file directly to the “app” folder.
Read the.csv file in the “app” folder and display the first 5 lines:
Import Pandas as pd Titanic = pd.read_csv(" /content/drive/My drive/ app/Titanic. CSV ") Titanic. Head (5)Copy the code
Clone the GitHub repository to Google Colab
It’s easy to clone a GitHub repository using Git.
Step 1: Find the GitHub repository and get the “Git” link
Find the GitHub repository you need.
Such as: github.com/wxs/keras-m…
Click Clone or Download > Copy the Link!
2. Use Git clones
Run the following command:
! Git clone github.com/wxs/keras-m…
3. Open the corresponding folder in Google Drive
Of course, the corresponding folder in Google Drive has the same name as the GitHub repository.
4. Open your notes
Right click > Open With > Colaboratory
5. Run
Now you can run your GitHub repository code in Google Colab.
Some helpful tips
1. How do I install third-party libraries?
Keras
! pip install -q keras import kerasCopy the code
PyTorch
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{} {} {} -'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
Copy the code
! PIP install – q download.pytorch.org/whl/ {accele… torchvision import torch
Or try this:
! pip3 install torch torchvision
MxNet
! Apt install libnvrtc8.0! pip install mxnet-cu80 import mxnet as mxCopy the code
OpenCV
! apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python import cv2Copy the code
XGBoost
! PIP install -q xgboost==0.4a30 import xgboostCopy the code
GraphViz
! apt-get -qq install -y graphviz && pip install -q pydot import pydotCopy the code
7 zip reader
! apt-get -qq install -y libarchive-dev && pip install -q -U libarchive import libarchiveCopy the code
Other libraries
! PIP install or! Apt-get install Installs other libraries.
2. Check whether the GPU works properly.
To see if the GPU is being used correctly in Colab, run the following code for cross-validation:
import tensorflow as tf
tf.test.gpu_device_name()
Copy the code
3. Which GPU am I using?
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
Copy the code
Currently, Colab offers only the Tesla K80.
4. Output RAM information?
! cat /proc/meminfoCopy the code
5. Output CPU information.
! cat /proc/cpuinfoCopy the code
6. Change the working folder
In general, when you run the following command:
! lsCopy the code
You’ll see the Datalab and Drive folders.
Therefore, when defining each file name, you need to add Drive /app before it.
To fix this, simply change the working directory. (In this tutorial, I changed it to the App folder) You can use the following code:
import os
os.chdir("drive/app")
Note: After mounting the web disk directory, you have not changed the directory before, so this should be entered
# os.chdir("drive/My Drive/app")
Copy the code
After running the above code, if you run it again
! lsCopy the code
You’ll see the contents of the App folder, so you don’t have to keep adding drive/app.
7.”No backend with GPU available
“Wrong solution
If you encounter this error:
Failed to assign a backend No backend with GPU available. Would you like to use a runtime with no accelerator? # Failed to specify backend. No GPU back-end is available. Do I need to use a run time without an accelerator?
You can try again later. A lot of people are using Gpus now, and when all gpus are in use, you get this error message.
Reference here
8. How to clear the run output of all cell lines?
Go to Tools>>Command Palette>>Clear All Outputs
9. “apt-key output should not be parsed (stdout is not a terminal)” warning
If you encounter this warning:
Warning: apt-key output should not be parsed (stdout is not a terminal) # warning: apt-key output cannot be parsed (current stdout is not terminal)
Copy the code
This means that you have completed the authorization. Just mount Google Drive:
! mkdir -p drive ! google-drive-ocamlfuse driveCopy the code
10. How to use Tensorboard in Google Colab?
I recommend this repository code:
Github.com/mixuala/col…
11. How do I restart Google Colab?
To restart (or reset) your virtual machine, run the following command:
!kill -9 -1
Copy the code
12. How to add a Form to Google Colab?
To avoid changing the hyperparameter every time in your code, you can simply add the form to Google Colab.
For example, I added a form with the learning_rate variable and the optimizer string.
13. How do I view method parameters?
To view the parameters of a method in TensorFlow, Keras, and other frameworks, you can add a question mark identifier (?) after the method name. :
This allows you to see the original document without having to click on the TensorFlow website.
14. How to send large files from Colab to Google Drive?
Which file do I need to send?
file_name = "REPO.tar"
from googleapiclient.http import MediaFileUpload
from googleapiclient.discovery import build
auth.authenticate_user()
drive_service = build('drive'.'v3')
def save_file_to_drive(name, path):
file_metadata = {'name': name, 'mimeType': 'application/octet-stream'}
media = MediaFileUpload(path, mimetype='application/octet-stream', resumable=True)
created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
return created
save_file_to_drive(file_name, file_name)
Copy the code
15. How to run Tensorboard in Google Colab?
If you want to run Tensorboard in Google Colab, run the code below.
# You can change the directory name
LOG_DIR = 'tb_logs'! wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip ! unzip ngrok-stable-linux-amd64.zip import osif not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
get_ipython().system_raw(
'tensorBoard --logdir {} --host 0.0.0.0 --port 6006&'
.format(LOG_DIR))
get_ipython().system_raw('./ngrok http 6006 &')
!curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
Copy the code
You can track the Tensorboard logs by creating a link to ngrok.io. You can find this URL at the end of the output.
Note that your Tensorboard logs will be stored in the tb_logs directory. Of course, you can change the directory name.
After that, we can see the Tensorboard! Run the following code to track the Tensorboard log via the nGROk URL link.
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import TensorBoard
batch_size = 128
num_classes = 10
epochs = 12
Enter the image dimension
img_rows, img_cols = 28, 28
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Transform the category vector into a binary classification matrix
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu') Model.add (MaxPooling2D(pool_size=(2, 2))) Model.add (Dense(128, Dense)) model.add(Dense(128, Dense)) activation='relu')) model. The add (Dropout (0.5)) model. The add (Dense (num_classes, activation ='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
tbCallBack = TensorBoard(log_dir=LOG_DIR,
histogram_freq=1,
write_graph=True,
write_grads=True,
batch_size=batch_size,
write_images=True)
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[tbCallBack])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Copy the code
Tensorboard 🙂
conclusion
I think Colab will bring a new flavor to deep learning and AI research around the world.
If you find this article helpful, then please give it some applause 👏 and share it with others, it will be very meaningful. Leave a comment below.
You can find me on Twitter.
Finally, please note that
The original English text will be updated continuously. Please refer to the original English text if necessary.
If you find any errors in the translation or other areas that need improvement, you are welcome to revise and PR the translation in the Gold Translation program, and you can also get corresponding bonus points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
Diggings translation project is a community for translating quality Internet technical articles from diggings English sharing articles. The content covers the fields of Android, iOS, front end, back end, blockchain, products, design, artificial intelligence and so on. For more high-quality translations, please keep paying attention to The Translation Project, official weibo and zhihu column.