forked from VinAIResearch/PCC-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
executable file
·182 lines (142 loc) · 6.63 KB
/
datasets.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
import os
from os import path
import numpy as np
from torch.utils.data import Dataset
import torch
from data import sample_planar
from data import sample_pole
torch.set_default_dtype(torch.float64)
class BaseDataset(Dataset):
def __init__(self, data_path, sample_size, noise):
self.sample_size = sample_size
self.noise = noise
self.data_path = data_path
if not os.path.exists(self.data_path):
os.makedirs(self.data_path)
self._process()
self.data_x, self.data_u, self.data_x_next = torch.load(self.data_path + '{:d}_{:.0f}.pt'.format(self.sample_size, self.noise))
def __len__(self):
return len(self.data_x)
def __getitem__(self, index):
return self.data_x[index], self.data_u[index], self.data_x_next[index]
def _process_image(self, img):
pass
def check_exists(self):
return (path.exists(self.data_path + '{:d}_{:.0f}.pt'.format(self.sample_size, self.noise)))
def _process(self):
pass
class PlanarDataset(BaseDataset):
width = 40
height = 40
action_dim = 2
def __init__(self, sample_size, noise):
data_path = 'data/planar/'
super(PlanarDataset, self).__init__(data_path, sample_size, noise)
def _process_image(self, img):
return torch.from_numpy(img.flatten()).unsqueeze(0)
def _process(self):
if self.check_exists():
return
else:
x_numpy_data, u_numpy_data, x_next_numpy_data, state_numpy_data, state_next_numpy_data = \
sample_planar.sample(sample_size=self.sample_size, noise=self.noise)
data_len = len(x_numpy_data)
# place holder for data
data_x = torch.zeros(data_len, self.width * self.height)
data_u = torch.zeros(data_len, self.action_dim)
data_x_next = torch.zeros(data_len, self.width * self.height)
for i in range(data_len):
data_x[i] = self._process_image(x_numpy_data[i])
data_u[i] = torch.from_numpy(u_numpy_data[i])
data_x_next[i] = self._process_image(x_next_numpy_data[i])
data_set = (data_x, data_u, data_x_next)
with open(self.data_path + '{:d}_{:.0f}.pt'.format(self.sample_size, self.noise), 'wb') as f:
torch.save(data_set, f)
class PendulumDataset(BaseDataset):
width = 48
height = 48 * 2
action_dim = 1
def __init__(self, sample_size, noise):
data_path = 'data/pendulum/'
super(PendulumDataset, self).__init__(data_path, sample_size, noise)
def _process_image(self, img):
x = np.vstack((img[:, :, 0], img[:, :, 1])).flatten()
return torch.from_numpy(x).unsqueeze(0)
def _process(self):
if self.check_exists():
return
else:
x_numpy_data, u_numpy_data, x_next_numpy_data, state_numpy_data, state_next_numpy_data = \
sample_pole.sample(env_name='pendulum', sample_size=self.sample_size, noise=self.noise)
data_len = len(x_numpy_data)
# place holder for data
data_x = torch.zeros(data_len, self.width * self.height)
data_u = torch.zeros(data_len, self.action_dim)
data_x_next = torch.zeros(data_len, self.width * self.height)
for i in range(data_len):
data_x[i] = self._process_image(x_numpy_data[i])
data_u[i] = torch.from_numpy(u_numpy_data[i])
data_x_next[i] = self._process_image(x_next_numpy_data[i])
data_set = (data_x, data_u, data_x_next)
with open(self.data_path + '{:d}_{:.0f}.pt'.format(self.sample_size, self.noise), 'wb') as f:
torch.save(data_set, f)
class CartPoleDataset(BaseDataset):
width = 80
height = 80 * 2
action_dim = 1
def __init__(self, sample_size, noise):
data_path = 'data/cartpole/'
super(CartPoleDataset, self).__init__(data_path, sample_size, noise)
def _process_image(self, img):
x = torch.zeros(size=(2, self.width, self.width))
x[0, :, :] = torch.from_numpy(img[:, :, 0])
x[1, :, :] = torch.from_numpy(img[:, :, 1])
return x.unsqueeze(0)
def _process(self):
if self.check_exists():
return
else:
x_numpy_data, u_numpy_data, x_next_numpy_data, state_numpy_data, state_next_numpy_data = \
sample_pole.sample(env_name='cartpole', sample_size=self.sample_size, noise=self.noise)
data_len = len(x_numpy_data)
# place holder for data
data_x = torch.zeros(data_len, 2, self.width, self.width)
data_u = torch.zeros(data_len, self.action_dim)
data_x_next = torch.zeros(data_len, 2, self.width, self.width)
for i in range(data_len):
data_x[i] = self._process_image(x_numpy_data[i])
data_u[i] = torch.from_numpy(u_numpy_data[i])
data_x_next[i] = self._process_image(x_next_numpy_data[i])
data_set = (data_x, data_u, data_x_next)
with open(self.data_path + '{:d}_{:.0f}.pt'.format(self.sample_size, self.noise), 'wb') as f:
torch.save(data_set, f)
class ThreePoleDataset(BaseDataset):
width = 80
height = 80 * 2
action_dim = 3
def __init__(self, sample_size, noise):
data_path = 'data/threepole/'
super(ThreePoleDataset, self).__init__(data_path, sample_size, noise)
def _process_image(self, img):
x = torch.zeros(size=(2, self.width, self.width))
x[0, :, :] = torch.from_numpy(img[:, :, 0])
x[1, :, :] = torch.from_numpy(img[:, :, 1])
return x.unsqueeze(0)
def _process(self):
if self.check_exists():
return
else:
x_numpy_data, u_numpy_data, x_next_numpy_data, state_numpy_data, state_next_numpy_data = \
sample_pole.sample(env_name='threepole', sample_size=self.sample_size, noise=self.noise)
data_len = len(x_numpy_data)
# place holder for data
data_x = torch.zeros(data_len, 2, self.width, self.width)
data_u = torch.zeros(data_len, self.action_dim)
data_x_next = torch.zeros(data_len, 2, self.width, self.width)
for i in range(data_len):
data_x[i] = self._process_image(x_numpy_data[i])
data_u[i] = torch.from_numpy(u_numpy_data[i])
data_x_next[i] = self._process_image(x_next_numpy_data[i])
data_set = (data_x, data_u, data_x_next)
with open(self.data_path + '{:d}_{:.0f}.pt'.format(self.sample_size, self.noise), 'wb') as f:
torch.save(data_set, f)