2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

強化学習41 ロボットの強化学習への第1歩 Colaboratory+Pybullet

Posted at

 中学生から大学生のAI初学者を対象にしています。
本シリーズもここからは、対象が初学者なのかとの疑問もありますが、強化学習40までをやっていれば、中級くらいの内容かもしれません。
 ロボットの強化学習に突入です。
 この強化学習シリーズは、ここからロボットの機械学習と40までの内容をtensorflowで行うものを混ぜ込んでいきます。理由は、ロボット学習は機械学習の時間が数日かかるものもあるので、バックグランドで行いながら、数時間で終了するtensorflowも入れていくからです。
 
 さて、ロボットの機械学習ですが、chainerrlにあるroboschoolなるものをやろうと調べてみました。そうしたら、このプロジェクトは現在、deplecatedです。日本語に治すと非推奨?。これ以上やるなになっています。代わりに、pybulletに統合したので、そちらをやるように推奨されました。
 そこで、pybulletを調べましたが、OpenAIのgymに対応していることが分かりました。
 乱暴にローカルPCにpipをしたら、はじかれました。
sudoでやると通ります。

sudo pip install pybullet

Colaboratoryには、

!pip install pybullet

で数分間かけてインストールされました。途中でコンパイルしているみたいで、一度インストールすると、ランタイムを再起動しても残るみたいです。
なので、接続するたびに行えば済みます。
以下のようなcolaboratory notebookで動画を見ることができます。
しかし、パラメータを変更して、再度見ようとするとうまくいきません。ランタイムを再起動する必要があります。

画面のチェック

 最初に画面チェックするためのノートブックです。

 手順に沿ってやってみましょう。

 最終更新日 2019年12月23日

program install

必要なプログラムをインストールします。数分で終了します。
pybullet がかなりかかります。

!apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1
!pip install pyvirtualdisplay
!pip install pybullet

パラメータの作成


import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--env', type=str, default='HumanoidBulletEnv-v0')
parser.add_argument('--outdir', type=str, default='result')
parser.add_argument('--save_mp4', type=str, default='test.mp4')
parser.add_argument('--mode',type=str,default='human')
args = parser.parse_args([])

パラメータ設定

パラメータを設定します。


# @title Configuration
args.outdir="result" #@param{type:"string"}
args.env = "HumanoidBulletEnv-v0" #@param ["HumanoidBulletEnv-v0","Acrobot-v1","Pendulum-v0","CartPole-v0","MountainCar-v0"]{allow-input: true}
args.save_mp4=args.outdir+'/pretrain.mp4'#@param{type:"string"}
args.mode='rgb_array'#@param{type:"string"}
import os
if not os.path.exists(args.outdir):
    os.makedirs(args.outdir)

Evaluation

1回試行した結果が表示されます。動画は、outdir+'/pretrain.mp4'に出力されます。


from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()
%matplotlib inline
from IPython.display import HTML
from matplotlib import animation
import matplotlib.pyplot as plt
import gym
import pybullet_envs

env = gym.make(args.env)

frames = []
obs = env.reset()
done = False
R = 0
t = 0
while not done and t < 200:
  frames.append(env.render(mode = args.mode))
  action = env.action_space.sample()
  obs, r, done, _ = env.step(action)
  R += r
  t += 1
print('test episode:1',  'R:', R)
env.close()

plt.figure(figsize=(frames[0].shape[1]/72.0, frames[0].shape[0]/72.0),dpi=72)
patch = plt.imshow(frames[0])
plt.axis('off') 
def animate(i):
  patch.set_data(frames[i])
anim = animation.FuncAnimation(plt.gcf(), animate, frames=len(frames),interval=50)
# anim.save(save_mp4)
HTML(anim.to_jshtml())
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?