[PYTHON] Use a free GPU in your favorite environment

I want to use GPU but I can't buy it because it's expensive ...

If you are studying machine learning and have a GPU, you can study faster and more efficiently. But it's too expensive to buy ...

There is also a Google Colaboratory, but I just can't become a Jupyter Notebook and want to work in my usual environment (a little selfish).

At that time, I found this article!

Access Colab with ssh and use GPU

Get auth token from ngrok

Go to ngrok and open LOGIN-> Authentication-> Your Auth token. Then, the auth token is displayed on the screen below, so copy it.

Run ngrok on Google Colabora try

Open Google Colab, open Runtime-> Runtime Type Settings, and set the hardware accelerator to GPU.

Paste the following code into a code block and execute it. At this time, paste the auth token you copied earlier in the place of YOUR AUTH TOKEN on the 13th line.

# Install useful stuff
! apt install --yes ssh screen nano htop ranger git > /dev/null
# SSH setting
! echo "root:password" | chpasswd
! echo "PasswordAuthentication yes" > /etc/ssh/sshd_config
! echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config
! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
! service ssh restart > /dev/null
# Download ngrok
! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
! unzip -qq -n ngrok-stable-linux-amd64.zip
# Run ngrok
authtoken = "YOUR AUTHTOKEN"
get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')
! sleep 3
# Get the address for SSH
import requests
from re import sub
r = requests.get('http://localhost:4040/api/tunnels')
str_ssh = r.json()['tunnels'][0]['public_url']
str_ssh = sub("tcp://", "", str_ssh)
str_ssh = sub(":", " -p ", str_ssh)
str_ssh = "ssh root@" + str_ssh
print(str_ssh)

If the execution is successful, the following will be displayed.

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

ssh [email protected] -p XXXXX

Open it with VSCode and try it out

Try sshing to ssh [email protected] -p XXXXX using VS Code's Remote-SSH feature. You will be asked for a password, but you can type password specified in! Echo "root: password" | chpasswd.

Make main.py etc. appropriately and check that GPU can be used with the following code

import torch

print(torch.cuda.is_available())
# True

I was able to use it!

Verification

CIFAR10 I verified it with the task of learning. The code I actually used is here.

The following environment was used for comparison, and it was carried out with 5 epochs.

With GPU No GPU
OS Ubuntu 18.04.3 LTS macOS Catalina v.10.15.5
torch 1.4.0 1.5.1

It was lazy that I couldn't match the torch version.

Result (never correct as it is only run once)

speed
With GPU 256.162
No GPU 320.412

The speed is about 20% faster!

Finally

In Colab, environment construction such as Pytorch and Keras is pre-built, so that is also convenient!

Recommended Posts

Use a free GPU in your favorite environment
Use the latest pip in a virtualenv environment
Use Python in your environment from Win Automation
Use Sudachipy's learned word2vec in a low memory environment
Use Anaconda in pyenv environment
Collaborate in a remote environment
Use django-debug-toolbar in VirtualBox / Vagrant environment
Install Django in a pipenv virtual environment
Use print in a Python2 lambda expression
Use tensorflow in an environment without root
Easily use your own functions in Python
Get a list of packages installed in your current environment with python
Use a scikit-learn model trained in PySpark
Create a python environment on your Mac
Use Python3's Subprocess.run () in a CGI script
I tried using NVDashboard (for those who use GPU in jupyter environment)
Creating a virtual environment in an Anaconda environment
Install CaboCha in a non-Anaconda environment (Win)
I started Node.js in a virtual environment
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Build a LAMP environment on your local Docker
Build a LAMP environment in a very short time
Create a Vim + Python test environment in 1 minute
Put Linux in your Chromebook and use R ...
System switching occurs in a CentOS 7 cluster environment
Ubuntu18.04.05 Creating a python virtual environment in LTS
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
Build a Django environment with Vagrant in 5 minutes
Build a Python development environment on your Mac
Set a fixed IP in the Linux environment
Think about building a Python 3 environment in a Mac environment
Don't use readlines () in your Python for statement!
Work in a virtual environment with Python virtualenv.
Use jupyter-lab installed in python virtual environment (venv)
Use Python in Anaconda environment with VS Code
Build a Minecraft plugin development environment in Eclipse
Use pydantic when reading environment variables in Python
Use os.getenv to get environment variables in Python
Allow Keras 2.0 and OpenCV 3.2 to work in GPU environment
How to run AutoGluon in Google Colab GPU environment
[Bilingual, it's a waste] Which is your favorite Fibonacci?
Create an Anaconda virtual environment in your project folder
A memorandum on how to use keras.preprocessing.image in Keras
Building a TensorFlow environment that uses GPU on Windows 10
Set up a free server on AWS in 30 minutes
Build a Python environment on your Mac using pyenv
Convenient to use matplotlib subplots in a for statement
How to use jupyter lab in Windows 10 local environment
Setting up Jupyter Lab in a Python 3.9 venv environment
[Linux] How to put your IP in a variable
Try to make a blackjack strategy by reinforcement learning (③ Reinforcement learning in your own OpenAI Gym environment)