Hi all,
I’m having some trouble running the official reinforcement learning tutorial in the available colab notebook. I haven’t done anything beyond try to run the cells but I keep getting an error from (I believe) gym’s render function. I don’t know if colab won’t run the render function for some reason or if I am just doing something wrong, but some clarity would be great!
The code in the cell is:
resize = T.Compose([T.ToPILImage(),
T.Resize(40, interpolation=Image.CUBIC),
T.ToTensor()])
def get_cart_location(screen_width):
world_width = env.x_threshold * 2
scale = screen_width / world_width
return int(env.state[0] * scale + screen_width / 2.0) # MIDDLE OF CART
def get_screen():
# Returned screen requested by gym is 400x600x3, but is sometimes larger
# such as 800x1200x3. Transpose it into torch order (CHW).
screen = env.render(mode='rgb_array').transpose((2, 0, 1))
# Cart is in the lower half, so strip off the top and bottom of the screen
_, screen_height, screen_width = screen.shape
screen = screen[:, int(screen_height*0.4):int(screen_height * 0.8)]
view_width = int(screen_width * 0.6)
cart_location = get_cart_location(screen_width)
if cart_location < view_width // 2:
slice_range = slice(view_width)
elif cart_location > (screen_width - view_width // 2):
slice_range = slice(-view_width, None)
else:
slice_range = slice(cart_location - view_width // 2,
cart_location + view_width // 2)
# Strip off the edges, so that we have a square image centered on a cart
screen = screen[:, :, slice_range]
# Convert to float, rescale, convert to torch tensor
# (this doesn't require a copy)
screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
screen = torch.from_numpy(screen)
# Resize, and add a batch dimension (BCHW)
return resize(screen).unsqueeze(0).to(device)
env.reset()
plt.figure()
plt.imshow(get_screen().cpu().squeeze(0).permute(1, 2, 0).numpy(),
interpolation='none')
plt.title('Example extracted screen')
plt.show()
and the stack trace that I get is:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-6a36d3e0eceb> in <module>()
38 env.reset()
39 plt.figure()
---> 40 plt.imshow(get_screen().cpu().squeeze(0).permute(1, 2, 0).numpy(),
41 interpolation='none')
42 plt.title('Example extracted screen')
<ipython-input-10-6a36d3e0eceb> in get_screen()
12 # Returned screen requested by gym is 400x600x3, but is sometimes larger
13 # such as 800x1200x3. Transpose it into torch order (CHW).
---> 14 screen = env.render(mode='rgb_array').transpose((2, 0, 1))
15 print(screen)
16 # Cart is in the lower half, so strip off the top and bottom of the screen
/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/cartpole.py in render(self, mode)
172
173 if self.viewer is None:
--> 174 from gym.envs.classic_control import rendering
175 self.viewer = rendering.Viewer(screen_width, screen_height)
176 l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2
/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/rendering.py in <module>()
23
24 try:
---> 25 from pyglet.gl import *
26 except ImportError as e:
27 raise ImportError('''
/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py in <module>()
233 elif compat_platform == 'darwin':
234 from .cocoa import CocoaConfig as Config
--> 235 del base # noqa: F821
236
237
NameError: name 'base' is not defined
<Figure size 432x288 with 0 Axes>
Thanks!