Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jkuruzovich/aebc07a3b554eb1253e6ef877d3c9b4f to your computer and use it in GitHub Desktop.
Save jkuruzovich/aebc07a3b554eb1253e6ef877d3c9b4f to your computer and use it in GitHub Desktop.
Rendering OpenAi Gym in Colaboratory.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Rendering OpenAi Gym in Colaboratory.ipynb",
"version": "0.3.2",
"provenance": [],
"private_outputs": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jkuruzovich/aebc07a3b554eb1253e6ef877d3c9b4f/rendering-openai-gym-in-colaboratory.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "GsSGgH2Y0apl",
"colab_type": "code",
"colab": {}
},
"source": [
"# _____ _____ _____ _____ _____ _____ \n",
"# /\\ \\ /\\ \\ /\\ \\ /\\ \\ /\\ \\ /\\ \\ \n",
"# /::\\ \\ /::\\ \\ /::\\ \\ /::\\ \\ /::\\ \\ /::\\ \\ \n",
"# /::::\\ \\ \\:::\\ \\ /::::\\ \\ /::::\\ \\ /::::\\ \\ \\:::\\ \\ \n",
"# /::::::\\ \\ \\:::\\ \\ /::::::\\ \\ /::::::\\ \\ /::::::\\ \\ \\:::\\ \\ \n",
"# /:::/\\:::\\ \\ \\:::\\ \\ /:::/\\:::\\ \\ /:::/\\:::\\ \\ /:::/\\:::\\ \\ \\:::\\ \\ \n",
"# /:::/__\\:::\\ \\ \\:::\\ \\ /:::/__\\:::\\ \\ /:::/__\\:::\\ \\ /:::/__\\:::\\ \\ \\:::\\ \\ \n",
"# \\:::\\ \\:::\\ \\ /::::\\ \\ /::::\\ \\:::\\ \\ /::::\\ \\:::\\ \\ /::::\\ \\:::\\ \\ /::::\\ \\ \n",
"# ___\\:::\\ \\:::\\ \\ /::::::\\ \\ /::::::\\ \\:::\\ \\ /::::::\\ \\:::\\ \\ /::::::\\ \\:::\\ \\ ____ /::::::\\ \\ \n",
"# /\\ \\:::\\ \\:::\\ \\ /:::/\\:::\\ \\ /:::/\\:::\\ \\:::\\ \\ /:::/\\:::\\ \\:::\\____\\ /:::/\\:::\\ \\:::\\ \\ /\\ \\ /:::/\\:::\\ \\ \n",
"#/::\\ \\:::\\ \\:::\\____\\ /:::/ \\:::\\____\\/:::/ \\:::\\ \\:::\\____\\/:::/ \\:::\\ \\:::| |/:::/ \\:::\\ \\:::\\____\\/::\\ \\/:::/ \\:::\\____\\\n",
"#\\:::\\ \\:::\\ \\::/ / /:::/ \\::/ /\\::/ \\:::\\ /:::/ /\\::/ |::::\\ /:::|____|\\::/ \\:::\\ /:::/ /\\:::\\ /:::/ \\::/ /\n",
"# \\:::\\ \\:::\\ \\/____/ /:::/ / \\/____/ \\/____/ \\:::\\/:::/ / \\/____|:::::\\/:::/ / \\/____/ \\:::\\/:::/ / \\:::\\/:::/ / \\/____/ \n",
"# \\:::\\ \\:::\\ \\ /:::/ / \\::::::/ / |:::::::::/ / \\::::::/ / \\::::::/ / \n",
"# \\:::\\ \\:::\\____\\ /:::/ / \\::::/ / |::|\\::::/ / \\::::/ / \\::::/____/ \n",
"# \\:::\\ /:::/ / \\::/ / /:::/ / |::| \\::/____/ /:::/ / \\:::\\ \\ \n",
"# \\:::\\/:::/ / \\/____/ /:::/ / |::| ~| /:::/ / \\:::\\ \\ \n",
"# \\::::::/ / /:::/ / |::| | /:::/ / \\:::\\ \\ \n",
"# \\::::/ / /:::/ / \\::| | /:::/ / \\:::\\____\\ \n",
"# \\::/ / \\::/ / \\:| | \\::/ / \\::/ / \n",
"# \\/____/ \\/____/ \\|___| \\/____/ \\/____/ "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "odNaDE1zyrL2",
"colab_type": "text"
},
"source": [
"# install dependancies, takes around 45 seconds\n",
"\n",
"Rendering Dependancies\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8-AxnvAVyzQQ",
"colab_type": "code",
"colab": {}
},
"source": [
"#remove \" > /dev/null 2>&1\" to see what is going on under the hood\n",
"!pip install gym pyvirtualdisplay > /dev/null 2>&1\n",
"!apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "8A-1LTSH88EE",
"colab_type": "text"
},
"source": [
"Pacman Dependancies"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TCelFzWY9MBI",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt-get update > /dev/null 2>&1\n",
"!apt-get install cmake > /dev/null 2>&1\n",
"!pip install --upgrade setuptools 2>&1\n",
"!pip install ez_setup > /dev/null 2>&1\n",
"!pip install gym[atari] > /dev/null 2>&1"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "APXSx7hg19TH",
"colab_type": "text"
},
"source": [
"# Imports and Helper functions\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pdb2JwZy4jGj",
"colab_type": "code",
"colab": {}
},
"source": [
"import gym\n",
"from gym import logger as gymlogger\n",
"from gym.wrappers import Monitor\n",
"gymlogger.set_level(40) #error only\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"import random\n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import math\n",
"import glob\n",
"import io\n",
"import base64\n",
"from IPython.display import HTML\n",
"\n",
"from IPython import display as ipythondisplay"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nQEtc28G4niA",
"colab_type": "code",
"colab": {}
},
"source": [
"from pyvirtualdisplay import Display\n",
"display = Display(visible=0, size=(1400, 900))\n",
"display.start()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "G9UWeToN4r7D",
"colab_type": "code",
"colab": {}
},
"source": [
"\"\"\"\n",
"Utility functions to enable video recording of gym environment and displaying it\n",
"To enable video, just do \"env = wrap_env(env)\"\"\n",
"\"\"\"\n",
"\n",
"def show_video():\n",
" mp4list = glob.glob('video/*.mp4')\n",
" if len(mp4list) > 0:\n",
" mp4 = mp4list[0]\n",
" video = io.open(mp4, 'r+b').read()\n",
" encoded = base64.b64encode(video)\n",
" ipythondisplay.display(HTML(data='''<video alt=\"test\" autoplay \n",
" loop controls style=\"height: 400px;\">\n",
" <source src=\"data:video/mp4;base64,{0}\" type=\"video/mp4\" />\n",
" </video>'''.format(encoded.decode('ascii'))))\n",
" else: \n",
" print(\"Could not find video\")\n",
" \n",
"\n",
"def wrap_env(env):\n",
" env = Monitor(env, './video', force=True)\n",
" return env"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "W3BGbWOu179M",
"colab_type": "text"
},
"source": [
"# Pacman!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "dGEFMfDOzLen",
"colab_type": "code",
"colab": {}
},
"source": [
"env = wrap_env(gym.make(\"MsPacman-v0\"))"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "7BmIlXhe9Q89",
"colab_type": "code",
"colab": {}
},
"source": [
"#check out the pacman action space!\n",
"print(env.action_space)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "8nj5sjsk15IT",
"colab_type": "code",
"colab": {}
},
"source": [
"observation = env.reset()\n",
"\n",
"while True:\n",
" \n",
" env.render()\n",
" \n",
" #your agent goes here\n",
" action = env.action_space.sample() \n",
" \n",
" observation, reward, done, info = env.step(action) \n",
" \n",
" \n",
" if done: \n",
" break;\n",
" \n",
"env.close()\n",
"show_video()"
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment