How to do it...

The strategy that we discussed earlier is coded as follows:

  1. Download the ROM that contains the Space Invaders game and also install the retro package:
$ wget http://www.atarimania.com/roms/Roms.rar && unrar x Roms.rar && unzip Roms/ROMS.zip$ pip3 install gym-retro$ python3 -m retro.import ROMS/
  1. Create the environment and extract the observation space:
env=retro.make(game='SpaceInvaders-Atari2600')env.observation_space# Box(210,160,3)
  1. Build a function that preprocesses the frame (image/screenshot of the Space Invaders game):
def preprocess_frame(frame):     # Greyscale frame      gray = rgb2gray(frame)     # Crop the screen (remove the part below the player)     # [Up: Down, Left: right]     cropped_frame = gray[8:-12,4:-12] # Normalize ...

Get Neural Networks with Keras Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.