Skip to content

Instantly share code, notes, and snippets.

@BramVanroy
Last active June 15, 2020 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BramVanroy/3700fed9dd56fadde36a763bf280bdbe to your computer and use it in GitHub Desktop.
Save BramVanroy/3700fed9dd56fadde36a763bf280bdbe to your computer and use it in GitHub Desktop.
Using web-serving tool from a remote server

Oftentimes, you may want to use a web-based tool during programming, e.g. a Jupyter notebook, Tensorboard, Streamlit, and others. It is easy to set these tools up locally, on your own machine, but this computer may not be as powerful as a server that you have available. Here is a small guide to show you how to easily use the web-based tool remotely. As an example, we will use Tensorboard, allowing us to remotely monitor the live-updated progress of our machine learning system during training. This gist is simply an extension of the following Stack Overflow post. This gist does not cover how to use Tensorboard itself. To get started with that, read through the documentation (works for Tensorflow as well as PyTorch).

If we would start Tensorboard on our own machine, it would create a local server that is accessible through a specific port. Your command line would typically show something like (do not run this yet):

$ tensorboard --logdir run/
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.2.2 at http://localhost:6006/ (Press CTRL+C to quit)

This tells you that Tensorboard created a local server (localhost) that is only accessible locally, and that is available on port 6006. However, if we run this command on the server (where our machine learning process will be running), then that means that the Tensorboard instance is running locally on the server and not accessible to us remotely. Luckily, we can forward the used port to our local machine at the very beginning when connecting to the server via SSH by providing the -L option (see below). However, this will only be the last step. First, we should start training and launch the Tensorboard server. You can start by simply ssh'ing into your server as usual. (Note: if you are on Windows I highly recommend using WSL or PowerShell 7.)

ssh bram@myserver

Then, I recommend to run two screens in the background - one for the training process, and one for the Tensorboard server. Your commands would look something like this:

screen -S train  # create screen named `train`
# launch training. This script should include the Tensorboard logging to a given dir, e.g. `run/`
python train.py  
# !Press CTRL+a+d to put the screen into background mode (detach)
screen -S tb  # create screen named `tb` (tensorboard)
tensorboard --logdir run/  # launch Tensorboard server
# !Press CTRL+a+d to put the screen into background mode (detach)
exit  # close ssh connection to the server

Now the training process is running on the remote server and a Tensorboard server is serving the updates. As mentioned before, though, that server is not exposed to us yet but is only serving its content locally. To get access to that server via our browser, we open a new ssh connection by using the -L option that takes host port 6006 of the host's address 127.0.0.1 and forwards it to the local machine at port 16006. Additionally, we also tell the ssh command that we will not be executing remote commands on this ssh connection -N and that we would like to run the ssh connection in the background -f.

# modified from: https://stackoverflow.com/a/42445070/1150683
ssh -N -f -L 16006:localhost:6006 bram@myserver

After launching this process you can close your terminal. Since we are forwarding 6006 to 16006 on our own machine, we can now open our browser and go to http://127.0.0.1:16006/ or http://localhost:16006/, which should bring you to the Tensorflow overview page. Tensorboard can be closed and restarting whenever you like - it does not affect the training process, it simply visualizes it and serves it from a server instance.

The same methodology presented here can be used to run a Jupyter server and then connecting to it remotely. This is particularly useful if you are running computation-heavy code. Your local machine can then show the notebook and the results, but the actual computation is done on the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment