-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
136 lines (109 loc) · 4.19 KB
/
utils.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
import numpy as np
import torch
import os
import random
import imageio
import gym
from tqdm import trange
import pickle
class ReplayBuffer(object):
def __init__(self, state_dim, action_dim, max_size=int(1e6)):
self.max_size = max_size
self.ptr = 0
self.size = 0
self.state = np.zeros((max_size, state_dim))
self.action = np.zeros((max_size, action_dim))
self.next_state = np.zeros((max_size, state_dim))
self.reward = np.zeros((max_size, 1))
self.not_done = np.zeros((max_size, 1))
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def add(self, state, action, next_state, reward, done):
self.state[self.ptr] = state
self.action[self.ptr] = action
self.next_state[self.ptr] = next_state
self.reward[self.ptr] = reward
self.not_done[self.ptr] = 1. - done
self.ptr = (self.ptr + 1) % self.max_size
self.size = min(self.size + 1, self.max_size)
def sample(self, batch_size):
ind = np.random.randint(0, self.size, size=batch_size)
return (
torch.FloatTensor(self.state[ind]).to(self.device),
torch.FloatTensor(self.action[ind]).to(self.device),
torch.FloatTensor(self.next_state[ind]).to(self.device),
torch.FloatTensor(self.reward[ind]).to(self.device),
torch.FloatTensor(self.not_done[ind]).to(self.device)
)
def convert_D4RL(self, dataset):
self.state = dataset['observations']
self.action = dataset['actions']
self.next_state = dataset['next_observations']
self.reward = dataset['rewards'].reshape(-1, 1)
self.not_done = 1. - dataset['terminals'].reshape(-1, 1)
self.size = self.state.shape[0]
def convert_D4RL_finetune(self, dataset):
self.ptr = dataset['observations'].shape[0]
self.size = dataset['observations'].shape[0]
self.state[:self.ptr] = dataset['observations']
self.action[:self.ptr] = dataset['actions']
self.next_state[:self.ptr] = dataset['next_observations']
self.reward[:self.ptr] = dataset['rewards'].reshape(-1, 1)
self.not_done[:self.ptr] = 1. - dataset['terminals'].reshape(-1, 1)
def normalize_states(self, eps=1e-3):
mean = self.state.mean(0, keepdims=True)
std = self.state.std(0, keepdims=True) + eps
self.state = (self.state - mean) / std
self.next_state = (self.next_state - mean) / std
return mean, std
def clip_to_eps(self, eps=1e-5):
lim = 1 - eps
self.action = np.clip(self.action, -lim, lim)
def make_dir(dir_path):
try:
os.mkdir(dir_path)
except OSError:
pass
return dir_path
def set_seed_everywhere(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
def snapshot_src(src, target, exclude_from):
make_dir(target)
os.system(f"rsync -rv --exclude-from={exclude_from} {src} {target}")
class VideoRecorder(object):
def __init__(self, dir_name, height=512, width=512, camera_id=0, fps=30):
self.dir_name = dir_name
self.height = height
self.width = width
self.camera_id = camera_id
self.fps = fps
self.frames = []
def init(self, enabled=True):
self.frames = []
self.enabled = self.dir_name is not None and enabled
def record(self, env):
if self.enabled:
frame = env.render(
mode='rgb_array',
height=self.height,
width=self.width,
# camera_id=self.camera_id
)
self.frames.append(frame)
def save(self, file_name):
if self.enabled:
path = os.path.join(self.dir_name, file_name)
imageio.mimsave(path, self.frames, fps=self.fps)
def grad_norm(model):
total_norm = 0.
for p in model.parameters():
param_norm = p.grad.data.norm(2)
total_norm += param_norm.item() ** 2
total_norm = total_norm ** (1. / 2)
return total_norm