-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
341 lines (264 loc) · 12.5 KB
/
data.py
File metadata and controls
341 lines (264 loc) · 12.5 KB
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
import json
import os
import numpy as np
import torch
import skimage.io as io
#import vizdoom as vzd
import skimage.transform as transform
import cv2
from torch.utils.data import Dataset
from PIL import Image
from tqdm import tqdm
from action_mapping import ActionMapper
# Chunked dataset code from: https://discuss.pytorch.org/t/an-iterabledataset-implementation-for-chunked-data/124437/3
class RandomPolicyDataset(Dataset):
def __init__(self, images):
super().__init__()
self.images = images
def __len__(self):
return len(self.images)
def __getitem__(self, item):
return self.images[item]
class StoredDataset(Dataset):
def __init__(self, prefix_path, file_paths, img_shape=(64, 64, 3), observation_key="observations"):
self.prefix_path = prefix_path
self.file_paths = file_paths
self.img_shape = img_shape
self.grayscale = True if len(img_shape) == 2 or img_shape[-1] == 1 else False
self.images = []
full_file_paths = [os.path.join(self.prefix_path, x) for x in self.file_paths if ".csv" not in x]
for fp in full_file_paths:
data = np.load(fp, allow_pickle=True)
if self.grayscale:
images = 0.299 * data[observation_key][:, :, :, 0] + 0.587 * data[observation_key][:, :, :, 1] + 0.114 * data[observation_key][:, :, :, 2]
else:
images = data[observation_key]
self.images.append(np.array(images))
self.images = np.concatenate(self.images)
def __len__(self):
return len(self.images)
def __getitem__(self, item):
img = self.images[item]
if img.shape != self.img_shape:
img = transform.resize(img, self.img_shape)
if np.max(img) > 1:
img = img / 255.
return torch.FloatTensor(img).permute(2, 0, 1)
class StoredDatasetFromVideo(Dataset):
def __init__(self, prefix_path, file_paths, img_shape=(64, 64, 3), max_frames=None):
self.prefix_path = prefix_path
self.file_paths = file_paths
self.img_shape = img_shape
self.grayscale = True if len(img_shape) == 2 or img_shape[-1] == 1 else False
self.images = []
full_file_paths = [os.path.join(self.prefix_path, x, "recording.mp4") for x in self.file_paths]
for fp in full_file_paths:
cap = cv2.VideoCapture(fp)
count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret or (max_frames and count >= max_frames):
break
if self.grayscale:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.images.append(frame)
count += 1
cap.release()
self.images = np.array(self.images)
def __len__(self):
return len(self.images)
def __getitem__(self, item):
img = self.images[item]
# Ensure the image has 3 channels if not grayscale
if self.grayscale:
if img.ndim == 2:
img = np.expand_dims(img, axis=-1)
elif img.ndim == 2:
img = np.stack([img] * 3, axis=-1)
if img.shape != self.img_shape:
img = transform.resize(img, self.img_shape, anti_aliasing=True)
if np.max(img) > 1:
img = img / 255.
img_tensor = torch.FloatTensor(img)
if not self.grayscale:
img_tensor = img_tensor.permute(2, 0, 1)
else:
img_tensor = img_tensor.permute(2, 0, 1) # (1, H, W)
return img_tensor
class StoredDatasetWithHistory(Dataset):
def __init__(self, prefix_path, file_paths, img_shape=(64, 64), history_length=4):
self.prefix_path = prefix_path
self.file_paths = file_paths
self.img_shape = img_shape
self.history_length = history_length
self.images = []
full_file_paths = [os.path.join(self.prefix_path, x) for x in self.file_paths if ".csv" not in x]
for fp in full_file_paths:
data = np.load(fp, allow_pickle=True)
images = 0.299 * data["observations"][:, :, :, 0] + 0.587 * data["observations"][:, :, :, 1] + 0.114 * data["observations"][:, :, :, 2]
self.images.append(self._compute_history_obs(images))
self.images = np.concatenate(self.images)
def __len__(self):
return len(self.images)
def __getitem__(self, item):
img = self.images[item]
return torch.FloatTensor(img)
def _compute_history_obs(self, images):
images_with_history = []
obs_buffer = [np.zeros(shape=self.img_shape) for _ in range(self.history_length)]
for i in range(images.shape[0]):
obs_buffer.append(images[i])
obs_buffer.pop(0)
images_with_history.append(np.array(obs_buffer))
return np.array(images_with_history)
class VizdoomDataset(Dataset):
def __init__(self, prefix_path, file_paths=None, img_shape=(64, 64, 3), resize=True, grayscale=False):
import matplotlib.pyplot as plt
self.prefix_path = prefix_path
self.file_paths = file_paths if file_paths is not None else os.listdir(prefix_path)
self.img_shape = img_shape
self.images = []
full_file_paths = [os.path.join(self.prefix_path, x) for x in self.file_paths if ".csv" not in x]
game = vzd.DoomGame()
game.load_config(f"{prefix_path.split(sep='/')[-1]}.cfg")
game.set_screen_resolution(vzd.ScreenResolution.RES_160X120)
game.set_window_visible(False)
game.set_render_hud(False)
game.init()
for fp in tqdm(full_file_paths, desc="Encoding Vizdoom trajectories..."):
game.replay_episode(fp)
obs = []
while not game.is_episode_finished():
s = game.get_state().screen_buffer
game.advance_action()
if resize:
obs.append(np.array(Image.fromarray(s.swapaxes(0, 2)).resize(size=(64, 64))).swapaxes(0, 2).astype(np.float32) / 255.)
else:
obs.append(s.astype(np.float32) / 255.)
obs = np.array(obs)
if grayscale:
obs = 0.299 * obs[:, :, 0] + 0.587 * obs[:, :, 1] + 0.114 * obs[:, :, 2]
self.images.append(np.array(obs))
self.images = np.concatenate(self.images)
game.close()
def __len__(self):
return len(self.images)
def __getitem__(self, item):
img = self.images[item]
return torch.FloatTensor(img)
class FullAtariGameDataset(Dataset):
def __init__(self, prefix_path, game_name, img_shape=(64, 64, 3), train=True):
self.prefix_path = prefix_path
self.split = "train" if train else "test"
self.img_shape = img_shape
self.grayscale = True if len(img_shape) == 2 or img_shape[-1] == 1 else False
self.images = []
self.split = "train" if train else "test"
self.game_name = game_name
images_path = os.path.join(os.path.join(self.prefix_path, game_name), self.split)
self.images = [os.path.join(images_path, x) for x in os.listdir(images_path) if ".json" not in x]
"""#with open(os.path.join(images_path, "actions.json"), "r") as f:
self.actions = json.load(f)
f.close()"""
def __len__(self):
return len(self.images)
def __getitem__(self, item):
img = io.imread(self.images[item]) / 255.
if self.grayscale:
img = 0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2]
img = transform.resize(img, self.img_shape)
#frame_id = self.images[item].split(sep=".")[0]
#action = int(self.actions[frame_id])
return torch.FloatTensor(img).permute(2, 0, 1)
class StoredDatasetFullSuite(Dataset):
def __init__(self, prefix_path, suite_name, img_shape=(64, 64, 3), observation_key="observations"):
self.prefix_path = prefix_path
self.suite_name = suite_name
self.img_shape = img_shape
self.grayscale = True if len(img_shape) == 2 or img_shape[-1] == 1 else False
self.images = []
full_file_paths = self._discover_image_paths()
for fp in full_file_paths:
data = np.load(fp, allow_pickle=True)
if self.grayscale:
data[observation_key] = 0.299 * data[observation_key][:, :, 0] + 0.587 * data[observation_key][:, :, 1] + 0.114 * data[observation_key][:, :, 2]
self.images.append(np.array(data[observation_key]))
self.images = np.concatenate(self.images)
def __len__(self):
return len(self.images)
def __getitem__(self, item):
img = self.images[item]
if img.shape != self.img_shape:
img = transform.resize(img, self.img_shape)
if np.max(img) > 1:
img = img / 255.
return torch.FloatTensor(img).permute(2, 0, 1)
def _discover_image_paths(self):
suite_path = os.path.join(self.prefix_path, self.suite_name)
full_game_paths = []
for game_name in os.listdir(suite_path):
game_path = os.path.join(suite_path, game_name)
traj_list = [os.path.join(game_path, x) for x in os.listdir(game_path) if x.endswith(".npz")]
full_game_paths.append(traj_list)
return list(np.concatenate(full_game_paths))
class ExpertExperienceWorldModel(Dataset):
def __init__(self, data_path, file_names, suite="atari"):
self.data_path = data_path
self.suite_name = suite
self.trajectories_names = os.listdir(data_path) if file_names is None else file_names
self.action_mapper = ActionMapper(game=self.data_path.split(sep="/")[-1]) if suite == "atari" else None
self.images = []
self.actions = []
for tn in self.trajectories_names:
if not tn.endswith(".npz"):
tn += ".npz"
data = np.load(os.path.join(self.data_path, f"{tn}"), allow_pickle=True)
self.images.append(data["observations"])
if self.action_mapper:
actions = [self.action_mapper.map_ale_to_gym(a) for a in data["actions"]]
self.actions.append(np.array(actions))
else:
self.actions.append(data["actions"])
self.images = np.concatenate(self.images)
self.actions = np.concatenate(self.actions)
def __len__(self):
return len(self.images) - 1
def __getitem__(self, item):
return {"obs": torch.FloatTensor(self.images[item]).permute(2, 0, 1), "act": torch.LongTensor([self.actions[item]]), "next_obs": torch.FloatTensor(self.images[item + 1]).permute(2, 0, 1)}
def _discover_image_paths(self):
suite_path = os.path.join(self.data_path, self.suite_name)
full_game_paths = []
for game_name in os.listdir(suite_path):
game_path = os.path.join(suite_path, game_name)
traj_list = [os.path.join(game_path, x) for x in os.listdir(game_path) if x.endswith(".npz")]
full_game_paths.append(traj_list)
return list(np.concatenate(full_game_paths))
class ExpertExperienceWorldModelFullSuite(Dataset):
def __init__(self, data_path, file_names, suite="atari"):
self.data_path = data_path
self.trajectories_names = os.listdir(data_path) if file_names is None else file_names
self.action_mapper = ActionMapper(game=self.data_path.split(sep="/")[-1]) if suite == "atari" else None
self.images = []
self.actions = []
for tn in self.trajectories_names:
if not tn.endswith(".npz"):
tn += ".npz"
data = np.load(os.path.join(self.data_path, f"{tn}"), allow_pickle=True)
self.images.append(data["observations"])
if self.action_mapper:
actions = [self.action_mapper.map_ale_to_gym(a) for a in data["action"]]
self.actions.append(np.array(actions))
else:
self.actions.append(data["actions"])
self.images = np.concatenate(self.images)
self.actions = np.concatenate(self.actions)
def __len__(self):
return len(self.images) - 1
def __getitem__(self, item):
return {"obs": torch.FloatTensor(self.images[item]).permute(2, 0, 1), "act": torch.LongTensor([self.actions[item]]), "next_obs": torch.FloatTensor(self.images[item + 1]).permute(2, 0, 1)}
if __name__ == '__main__':
base_path = "vizdoom/deadly_corridor"
data = VizdoomDataset(prefix_path=base_path,)
obs = data.__getitem__(123)