-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay_buffer.py
297 lines (226 loc) · 10.7 KB
/
replay_buffer.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
import numpy as np
import kornia
import torch
import torch.nn as nn
import torch.nn.functional as F
import utils
class ReplayBuffer:
"""Buffer to store environment transitions."""
def __init__(self, obs_shape, action_shape, capacity, image_pad, device, env):
self.capacity = capacity
self.device = device
self.env = env
self.image_pad = image_pad
self.aug_trans = nn.Sequential(
nn.ReplicationPad2d(image_pad),
kornia.augmentation.RandomCrop((obs_shape[-1], obs_shape[-1]))
)
self.obses = np.empty((capacity, *obs_shape), dtype=np.uint8)
self.next_obses = np.empty((capacity, *obs_shape), dtype=np.uint8)
self.actions = np.empty((capacity, *action_shape), dtype=np.float32)
self.rewards = np.empty((capacity, 1), dtype=np.float32)
self.not_dones = np.empty((capacity, 1), dtype=np.float32)
self.not_dones_no_max = np.empty((capacity, 1), dtype=np.float32)
self.eoo = np.empty((capacity, 1), dtype=np.float32)
self.idx = 0
self.full = False
def __len__(self):
return self.capacity if self.full else self.idx
def add(self, obs, action, reward, next_obs, done, done_no_max, eoo):
np.copyto(self.obses[self.idx], obs)
np.copyto(self.actions[self.idx], action)
np.copyto(self.rewards[self.idx], reward)
np.copyto(self.next_obses[self.idx], next_obs)
np.copyto(self.not_dones[self.idx], not done)
np.copyto(self.not_dones_no_max[self.idx], not done_no_max)
np.copyto(self.eoo[self.idx], eoo)
self.idx = (self.idx + 1) % self.capacity
self.full = self.full or self.idx == 0
def sample(self, batch_size):
idxs = np.random.randint(
0, self.capacity if self.full else self.idx, size=batch_size
)
obses = self.obses[idxs]
next_obses = self.next_obses[idxs]
obses_aug = obses.copy()
next_obses_aug = next_obses.copy()
obses = torch.as_tensor(obses, device=self.device).float()
next_obses = torch.as_tensor(next_obses, device=self.device).float()
obses_aug = torch.as_tensor(obses_aug, device=self.device).float()
next_obses_aug = torch.as_tensor(next_obses_aug, device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
# KEEP UNCOMMENTED FOR TRANSLATION
# For ablations with no augmentation, *comment out* the following lines
obses = self.aug_trans(obses)
next_obses = self.aug_trans(next_obses)
obses_aug = self.aug_trans(obses_aug)
next_obses_aug = self.aug_trans(next_obses_aug)
return obses, actions, rewards, next_obses, not_dones_no_max, obses_aug, next_obses_aug
def sample_traj(self, batch_size, k):
"""Really will only work for envs with fixed length episodes, such as in dm_control"""
end_idxs = np.where(self.eoo == 1)[0]
beg_ranges = end_idxs + 1
beg_ranges = np.delete(beg_ranges, -1)
beg_ranges = np.insert(beg_ranges, np.array([0]), 0)
end_ranges = end_idxs - k
traj_idxs = []
n_slots = len(end_idxs)
for _ in range(batch_size):
slot_idx = np.random.choice(n_slots)
beg = np.random.choice(range(beg_ranges[slot_idx], end_ranges[slot_idx]))
traj_idxs.append([beg + i for i in range(k)])
actions = np.array([self.actions[traj_idxs[i]] for i in range(batch_size)])
# KEEP UNCOMMENTED FOR TRANSLATION
obses = np.array(
[
random_crop(self.obses[traj_idxs[i]], self.image_pad) for i in range(batch_size)
]
)
# obses_next = np.array(
# [
# random_crop(self.next_obses[traj_idxs[i]], self.image_pad) for i in range(batch_size)
# ]
# )
# ORIGINAL
# obses = np.array(
# [
# random_crop(self.obses[traj_idxs[i]], self.image_pad) for i in range(batch_size)
# ]
# )
# obses_next = np.array(
# [
# random_crop(self.next_obses[traj_idxs[i]], self.image_pad) for i in range(batch_size)
# ]
# )
# For ablations with no augmentation, *uncomment* the following lines
# obses = np.array([
# self.obses[traj_idxs[i]] for i in range(batch_size)
# ])
# obses_next = np.array([
# self.next_obses[traj_idxs[i]] for i in range(batch_size)
# ])
rewards = np.array([self.rewards[traj_idxs[i]] for i in range(batch_size)])
not_dones = np.array([self.not_dones_no_max[traj_idxs[i]] for i in range(batch_size)])
actions = torch.as_tensor(actions, device=self.device)
obses = torch.tensor(obses, device=self.device).float()
obses_next = torch.tensor(obses_next, device=self.device).float()
rewards = torch.tensor(rewards, device=self.device)
not_dones = torch.tensor(not_dones, device=self.device)
return obses, actions, obses_next, rewards#, not_dones
def sample_traj_efficient(self, batch_size, k):
"""Really will only work for envs with fixed length episodes, such as in dm_control"""
end_idxs = np.where(self.eoo == 1)[0]
beg_ranges = end_idxs + 1
beg_ranges = np.delete(beg_ranges, -1)
beg_ranges = np.insert(beg_ranges, np.array([0]), 0)
end_ranges = end_idxs - k - 1
traj_idxs = []
n_slots = len(end_idxs)
for _ in range(batch_size):
slot_idx = np.random.choice(n_slots)
beg = np.random.choice(range(beg_ranges[slot_idx], end_ranges[slot_idx]))
traj_idxs.append([beg + i for i in range(k + 1)])
actions = np.array([self.actions[traj_idxs[i]] for i in range(batch_size)])
# KEEP UNCOMMENTED FOR TRANSLATION
obses = np.array(
[
random_crop(self.obses[traj_idxs[i]], self.image_pad) for i in range(batch_size)
]
)
rewards = np.array([self.rewards[traj_idxs[i]] for i in range(batch_size)])
not_dones = np.array([self.not_dones_no_max[traj_idxs[i]] for i in range(batch_size)])
actions = torch.as_tensor(actions, device=self.device)
obses = torch.tensor(obses, device=self.device).float()
rewards = torch.tensor(rewards, device=self.device)
not_dones = torch.tensor(not_dones, device=self.device)
return obses, actions, None, rewards, not_dones
def sample_episode(self, batch_size):
end_idxs = np.where(self.eoo == 1)[0]
beg_ranges = end_idxs + 1
beg_ranges = np.delete(beg_ranges, -1)
beg_ranges = np.insert(beg_ranges, np.array([0]), 0)
episode_len = end_idxs[0] - beg_ranges[0]
n_slots = len(end_idxs)
traj_idxs = []
slot_idx = np.random.choice(n_slots, size=batch_size, replace=False)
for s in slot_idx:
beg = beg_ranges[s]
traj_idxs.append([beg + i for i in range(episode_len + 1)])
actions = np.array([self.actions[traj_idxs[i]] for i in range(batch_size)])
# KEEP UNCOMMENTED FOR TRANSLATION
obses = np.array(
[
random_crop(self.obses[traj_idxs[i]], self.image_pad) for i in range(batch_size)
]
)
rewards = np.array([self.rewards[traj_idxs[i]] for i in range(batch_size)])
not_dones = np.array([self.not_dones[traj_idxs[i]] for i in range(batch_size)])
actions = torch.as_tensor(actions, device=self.device)
obses = torch.tensor(obses, device=self.device).float()
rewards = torch.tensor(rewards, device=self.device)
not_dones = torch.tensor(not_dones, device=self.device)
return obses, actions, None, rewards, not_dones
def sample_traj_ends(self, batch_size, k):
"""Really will only work for envs with fixed length episodes, such as in dm_control"""
end_idxs = np.where(self.eoo == 1)[0]
beg_ranges = end_idxs + 1
beg_ranges = np.delete(beg_ranges, -1)
beg_ranges = np.insert(beg_ranges, np.array([0]), 0)
end_ranges = end_idxs - k
traj_idxs = []
n_slots = len(end_idxs)
for _ in range(batch_size):
slot_idx = np.random.choice(n_slots)
beg = np.random.choice(range(beg_ranges[slot_idx], end_ranges[slot_idx]))
traj_idxs.append([beg + i for i in range(k)])
# Ends are for the states, you need the actions and rewards
ends = []
for traj in traj_idxs:
ends.append([traj[0], traj[-1]])
actions = np.array([self.actions[traj_idxs[i]] for i in range(batch_size)])
# KEEP UNCOMMENTED FOR TRANSLATION
obses = np.array(
[
random_crop(self.obses[ends[i]], self.image_pad) for i in range(batch_size)
]
)
obses_next = np.array(
[
random_crop(self.next_obses[ends[i]], self.image_pad) for i in range(batch_size)
]
)
rewards = np.array([self.rewards[traj_idxs[i]] for i in range(batch_size)])
actions = torch.as_tensor(actions, device=self.device)
obses = torch.tensor(obses, device=self.device).float()
obses_next = torch.tensor(obses_next, device=self.device).float()
rewards = torch.tensor(rewards, device=self.device)
return obses, actions, obses_next, rewards
def random_crop(imgs, image_pad, out=84):
"""
args:
imgs: np.array shape (B,C,H,W)
out: output size (e.g. 84)
returns np.array
"""
n, c, h, w = imgs.shape
crop_max = h + (image_pad * 2) - out + 1
w1 = np.random.randint(0, crop_max, n)
h1 = np.random.randint(0, crop_max, n)
cropped = np.empty((n, c, out, out), dtype=imgs.dtype)
for i, (img, w11, h11) in enumerate(zip(imgs, w1, h1)):
img = np.pad(img, image_pad, mode='constant')[image_pad:-image_pad, :, :]
cropped[i] = img[:, h11:h11 + out, w11:w11 + out]
return cropped
def random_translate(imgs, size, return_random_idxs=False, h1s=None, w1s=None):
n, c, h, w = imgs.shape
assert size >= h and size >= w
outs = np.zeros((n, c, size, size), dtype=imgs.dtype)
h1s = np.random.randint(0, size - h + 1, n) if h1s is None else h1s
w1s = np.random.randint(0, size - w + 1, n) if w1s is None else w1s
for out, img, h1, w1 in zip(outs, imgs, h1s, w1s):
out[:, h1:h1 + h, w1:w1 + w] = img
if return_random_idxs: # So can do the same to another set of imgs.
return outs, dict(h1s=h1s, w1s=w1s)
return outs