-
Notifications
You must be signed in to change notification settings - Fork 3
/
retro_env.py
345 lines (295 loc) · 12.4 KB
/
retro_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import sys
import math
import random
import collections
import gc
import gym
import gzip
import gym.spaces
import json
import librosa
import numpy as np
import os
import retro
import retro.data
import scipy.io.wavfile as wv
from gym.utils import seeding
import cv2
gym_version = tuple(int(x.split('-')[0]) for x in gym.__version__.split('.'))
__all__ = ['RetroEnv']
class RetroEnv(gym.Env):
"""
Gym Retro environment class
Provides a Gym interface to classic video games
"""
metadata = {'render.modes': ['human', 'rgb_array'],
'video.frames_per_second': 60.0}
def __init__(self, game, state=retro.State.DEFAULT, scenario=None, info=None, use_restricted_actions=retro.Actions.FILTERED,
record=False, players=1, inttype=retro.data.Integrations.STABLE, obs_type=retro.Observations.IMAGE, naudio_samples=None, make_video=False, is_baseline=False):
if not hasattr(self, 'spec'):
self.spec = None
self._obs_type = obs_type
self.img = None
self.ram = None
self.viewer = None
self.gamename = game
self.statename = state
self.initial_state = None
self.players = players
self.naudio_samples = naudio_samples
self.audio_clip = []
self.make_video = make_video
self.is_baseline = is_baseline
metadata = {}
rom_path = retro.data.get_romfile_path(game, inttype)
metadata_path = retro.data.get_file_path(game, 'metadata.json', inttype)
if state == retro.State.NONE:
self.statename = None
elif state == retro.State.DEFAULT:
self.statename = None
try:
with open(metadata_path) as f:
metadata = json.load(f)
if 'default_player_state' in metadata and self.players <= len(metadata['default_player_state']):
self.statename = metadata['default_player_state'][self.players - 1]
elif 'default_state' in metadata:
self.statename = metadata['default_state']
else:
self.statename = None
except (IOError, json.JSONDecodeError):
pass
if self.statename:
self.load_state(self.statename, inttype)
self.data = retro.data.GameData()
if info is None:
info = 'data'
if info.endswith('.json'):
# assume it's a path
info_path = info
else:
info_path = retro.data.get_file_path(game, info + '.json', inttype)
if scenario is None:
scenario = 'scenario'
if scenario.endswith('.json'):
# assume it's a path
scenario_path = scenario
else:
scenario_path = retro.data.get_file_path(game, scenario + '.json', inttype)
self.system = retro.get_romfile_system(rom_path)
# We can't have more than one emulator per process. Before creating an
# emulator, ensure that unused ones are garbage-collected
gc.collect()
self.em = retro.RetroEmulator(rom_path)
self.em.configure_data(self.data)
self.em.step()
core = retro.get_system_info(self.system)
self.buttons = core['buttons']
self.num_buttons = len(self.buttons)
self.button_combos = self.data.valid_actions()
try:
assert self.data.load(info_path, scenario_path), 'Failed to load info (%s) or scenario (%s)' % (info_path, scenario_path)
except Exception:
del self.em
raise
if use_restricted_actions == retro.Actions.DISCRETE:
combos = 1
for combo in self.button_combos:
combos *= len(combo)
self.action_space = gym.spaces.Discrete(combos ** players)
elif use_restricted_actions == retro.Actions.MULTI_DISCRETE:
self.action_space = gym.spaces.MultiDiscrete([len(combos) if gym_version >= (0, 9, 6) else (0, len(combos) - 1) for combos in self.button_combos] * players)
else:
self.action_space = gym.spaces.MultiBinary(self.num_buttons * players)
kwargs = {}
if gym_version >= (0, 9, 6):
kwargs['dtype'] = np.uint8
if self._obs_type == retro.Observations.RAM:
shape = self.get_ram().shape
else:
img = [self.get_screen(p) for p in range(players)]
shape = img[0].shape
self.observation_space = gym.spaces.Box(low=0, high=255, shape=shape, **kwargs)
self.use_restricted_actions = use_restricted_actions
self.movie = None
self.movie_id = 0
self.movie_path = None
if record is True:
self.auto_record()
elif record is not False:
self.auto_record(record)
self.seed()
if gym_version < (0, 9, 6):
self._seed = self.seed
self._step = self.step
self._reset = self.reset
self._render = self.render
self._close = self.close
def _update_obs(self):
if self._obs_type == retro.Observations.RAM:
self.ram = self.get_ram()
return self.ram
elif self._obs_type == retro.Observations.IMAGE:
self.img = self.get_screen()
return self.img
else:
raise ValueError('Unrecognized observation type: {}'.format(self._obs_type))
def action_to_array(self, a):
actions = []
for p in range(self.players):
action = 0
if self.use_restricted_actions == retro.Actions.DISCRETE:
for combo in self.button_combos:
current = a % len(combo)
a //= len(combo)
action |= combo[current]
elif self.use_restricted_actions == retro.Actions.MULTI_DISCRETE:
ap = a[self.num_buttons * p:self.num_buttons * (p + 1)]
for i in range(len(ap)):
buttons = self.button_combos[i]
action |= buttons[ap[i]]
else:
ap = a[self.num_buttons * p:self.num_buttons * (p + 1)]
for i in range(len(ap)):
action |= int(ap[i]) << i
if self.use_restricted_actions == retro.Actions.FILTERED:
action = self.data.filter_action(action)
ap = np.zeros([self.num_buttons], np.uint8)
for i in range(self.num_buttons):
ap[i] = (action >> i) & 1
actions.append(ap)
return actions
def step(self, a):
if self.img is None and self.ram is None:
raise RuntimeError('Please call env.reset() before env.step()')
for p, ap in enumerate(self.action_to_array(a)):
if self.movie:
for i in range(self.num_buttons):
self.movie.set_key(i, ap[i], p)
self.em.set_button_mask(ap, p)
if self.movie:
self.movie.step()
self.em.step()
self.data.update_ram()
ob = self._update_obs()
rew, done, info = self.compute_step()
sample = self.em.get_audio()
if self.naudio_samples is not None:
info['audio'] = librosa.util.fix_length(sample.T, int(self.naudio_samples)).T
if self.make_video:
self.audio_clip.extend(sample)
if self.make_video:
baseline_str = 'b-' if self.is_baseline else ''
cv2.imwrite('video_frames/'+baseline_str + self.gamename + '-' + str(self.n) + '.png',cv2.cvtColor(ob, cv2.COLOR_RGB2BGR))
self.n +=1
return ob, rew, bool(done), dict(info)
def reset(self):
if self.audio_clip:
if self.make_video:
baseline_str = 'b-' if self.is_baseline else ''
path = baseline_str + self.gamename + '_audio.wav'
numpy_audio = np.asarray(self.audio_clip)
wv.write(path, int(self.em.get_audio_rate()), numpy_audio)
# Combine all saved frames into video
cmd1 = 'ffmpeg -y -r 60 -f image2 -i video_frames/' + baseline_str + self.gamename + '-%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p ' + baseline_str + self.gamename + '_noaudio.mp4 -hide_banner -loglevel panic'
# Add audio to video
os.system(cmd1)
cmd = "ffmpeg -y -i " + baseline_str + self.gamename + "_noaudio.mp4 -i " + path + " -y -c:v copy -c:a aac -strict experimental -hide_banner -loglevel panic " + baseline_str + self.gamename + '-' + str(self.n)+'.mp4'
os.system(cmd)
os.system('rm video_frames/' + baseline_str + self.gamename+'*.png')
print('saved video to ',baseline_str + self.gamename + '-' + str(self.n)+'.mp4')
sys.exit(0)
self.audio_clip = []
self.n = 0
if self.initial_state:
self.em.set_state(self.initial_state)
for p in range(self.players):
self.em.set_button_mask(np.zeros([self.num_buttons], np.uint8), p)
self.em.step()
if self.movie_path is not None:
rel_statename = os.path.splitext(os.path.basename(self.statename))[0]
self.record_movie(os.path.join(self.movie_path, '%s-%s-%06d.bk2' % (self.gamename, rel_statename, self.movie_id)))
self.movie_id += 1
if self.movie:
self.movie.step()
self.data.reset()
self.data.update_ram()
return self._update_obs()
def seed(self, seed=None):
self.np_random, seed1 = seeding.np_random(seed)
# Derive a random seed. This gets passed as a uint, but gets
# checked as an int elsewhere, so we need to keep it below
# 2**31.
seed2 = seeding.hash_seed(seed1 + 1) % 2**31
return [seed1, seed2]
def render(self, mode='human', close=False):
if close:
if self.viewer:
self.viewer.close()
return
img = self.get_screen() if self.img is None else self.img
if mode == "rgb_array":
return img
elif mode == "human":
if self.viewer is None:
from gym.envs.classic_control.rendering import SimpleImageViewer
self.viewer = SimpleImageViewer()
self.viewer.imshow(img)
return self.viewer.isopen
def close(self):
if hasattr(self, 'em'):
del self.em
def get_action_meaning(self, act):
actions = []
for p, action in enumerate(self.action_to_array(act)):
actions.append([self.buttons[i] for i in np.extract(action, np.arange(len(action)))])
if self.players == 1:
return actions[0]
return actions
def get_ram(self):
blocks = []
for offset in sorted(self.data.memory.blocks):
arr = np.frombuffer(self.data.memory.blocks[offset], dtype=np.uint8)
blocks.append(arr)
return np.concatenate(blocks)
def get_screen(self, player=0):
img = self.em.get_screen()
x, y, w, h = self.data.crop_info(player)
if not w or x + w > img.shape[1]:
w = img.shape[1]
else:
w += x
if not h or y + h > img.shape[0]:
h = img.shape[0]
else:
h += y
if x == 0 and y == 0 and w == img.shape[1] and h == img.shape[0]:
return img
return img[y:h, x:w]
def load_state(self, statename, inttype=retro.data.Integrations.DEFAULT):
if not statename.endswith('.state'):
statename += '.state'
with gzip.open(retro.data.get_file_path(self.gamename, statename, inttype), 'rb') as fh:
self.initial_state = fh.read()
self.statename = statename
def compute_step(self):
if self.players > 1:
reward = [self.data.current_reward(p) for p in range(self.players)]
else:
reward = self.data.current_reward()
done = self.data.is_done()
return reward, done, self.data.lookup_all()
def record_movie(self, path):
self.movie = retro.Movie(path, True, self.players)
self.movie.configure(self.gamename, self.em)
if self.initial_state:
self.movie.set_state(self.initial_state)
def stop_record(self):
self.movie_path = None
self.movie_id = 0
if self.movie:
self.movie.close()
self.movie = None
def auto_record(self, path=None):
if not path:
path = os.getcwd()
self.movie_path = path