Training ML-Agents with Google Colab (& Live Streaming to Twitch)

Dhyey Thumar
3 min readFeb 15, 2021

Live Stream the ML-Agents training process from Colab to Twitch server.

Live streaming ML-Agents training process from google colab
Check out this video on training process which was live streamed from colab to my twitch server & then exported to my YT channel

A new version of this package has been released & this article is also updated according to that new version.

But still the older version is active & now it is also available on PyPI, so refer to this readme to use the older version.

And if you wish to migrate to the newer version, then refer to this doc on migration (because there are some breaking changes)

Refer to this GitHub release to know what are the new features introduced in the 2.0 version of mlagents-video-streamer

If you want to train the ML-Agents with Google Colab (without Live Streaming it) then please check Training ML-Agents with Google Colab.

In this article, I will go one step further and Live Stream the ML-Agents training process from Google Colab to Twitch server (so you can find out what went wrong while training the agents 😎).

Prerequisites for Colab Notebook

  • GPU session
  • Environment build for Linux (without headless/server builds)

Captured Video Configurations

  • Video resolution: 1920 x 1080
  • Frame rate: 60
  • Constant bitrate: 6Mbps

Note: In this article, I am showing how to capture the training process. And I am assuming that you know how to run the ML-Agents on Google Colab (if not, then first check Training ML-Agents with Google Colab & then check this article).

Now let’s get started with the steps required to set up the Live Streamer on Colab.

Step 1: Installation & Imports from package

!pip install mlagents-video-streamerfrom mlagents_video_streamer import SetupVirtualDisplay
from mlagents_video_streamer import VideoStreamer

Step 2: Setup the Virtual Display

SetupVirtualDisplay()

Step 3: Define your live stream information & Initialize the VideoStreamer

# stream_info dictionary should be in this format onlystream_info = {
"URL": "rtmp://live.twitch.tv/app/", # example of Twitch URL
"secret": "--- secret here ---"
}
videoStreamer = VideoStreamer(stream_info)
# If you don't pass stream_info then it will simply store the video locally in the videos directory.

Now install ML-Agents package & upload your environment build files to Colab.

Step 4: Start the video streamer before starting with the training process

videoStreamer.start()

Step 5: Now capture the training process

import subprocess
from random import randrange

try:
train = subprocess.run([
"mlagents-learn",
"config.yaml",
"--run-id=train-1",
"--env=3DBall_example/3DBall.x86_64",
"--base-port=" + str(randrange(9000, 9999))
],
cwd="/content/", stdout=subprocess.PIPE)
print("Training process has been successfully ended.")
except Exception as e:
print("You killed the training process in between.")
finally:
videoStreamer.close()
# At the end don't forget to close the video streamer by using close() method on videoStreamer object as shown in the above example.

Now sit back and watch your ML-Agents getting trained in your twitch channel 😀✌.

Here we come to the end of this article. And if you have any doubts, suggestions, improvements then please do let me know.

Check out this github repo for the reference (readme file have more or less the same explanation as given in this article).

--

--

Dhyey Thumar

Working with Reinforcement Learning, Backend web development, & Computer Vision.