-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractors.py
272 lines (230 loc) · 10.4 KB
/
extractors.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
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
from net import RelationalNet
import torch.nn as nn
import torch as th
import gym
import numpy as np
from torch import autograd
class RelationalExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.Space):
super().__init__(observation_space, features_dim=256)
# the network does not contain the final projection
# with mlp_depth = 4, set net_arch = []
# else put the mlp layers into a custom network
self.net = RelationalNet(
input_size=8,
mlp_depth=4,
depth_transformer=2,
heads=2,
baseline=False,
recurrent_transformer=True,
)
def forward(self, observations: th.Tensor) -> th.Tensor:
return self.net(observations)
class SimpleExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.Space):
super().__init__(observation_space, features_dim=256)
# the network does not contain the final projection
# with mlp_depth = 4, set net_arch = []
# else put the mlp layers into a custom network
self.conv = nn.Sequential(
nn.Conv2d(3, 12, kernel_size=(2, 2), stride=1),
nn.BatchNorm2d(12),
nn.GELU(),
nn.Conv2d(12, 24, kernel_size=(2, 2), stride=1),
nn.BatchNorm2d(24),
nn.GELU(),
nn.Flatten(),
)
# for some reason the standard is 256
# self.linear = nn.Sequential(nn.Linear(864, 512),
# nn.GELU(),
# nn.Linear(512, 256),
# nn.GELU(),
# nn.Linear(256, 256),
# nn.GELU())
self.linear = nn.Sequential(
nn.Linear(3456, 512),
nn.GELU(),
nn.Linear(512, 256),
nn.GELU(),
nn.Linear(256, 256),
nn.GELU(),
)
def forward(self, observations: th.Tensor) -> th.Tensor:
x = self.conv(observations)
print(x.shape)
return self.linear(x)
class SimpleExtractorDict(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.Space):
super().__init__(observation_space, features_dim=256)
# the network does not contain the final projection
# with mlp_depth = 4, set net_arch = []
# else put the mlp layers into a custom network
self.conv = nn.Sequential(
nn.Conv2d(3, 12, kernel_size=(2, 2), stride=1),
nn.BatchNorm2d(12),
nn.GELU(),
nn.Conv2d(12, 24, kernel_size=(2, 2), stride=1),
nn.BatchNorm2d(24),
nn.GELU(),
nn.Flatten(),
)
# for some reason the standard is 256
# self.linear = nn.Sequential(nn.Linear(864, 512),
# nn.GELU(),
# nn.Linear(512, 256),
# nn.GELU(),
# nn.Linear(256, 256),
# nn.GELU())
self.linear = nn.Sequential(
nn.Linear(3456, 512),
nn.GELU(),
nn.Linear(512, 256),
nn.GELU(),
nn.Linear(256, 256),
nn.GELU(),
)
def forward(self, observations) -> th.Tensor:
image = observations["image"]
print("image shape ", image.shape)
x_image = self.conv(image)
x_state = th.cat(
(
th.squeeze(observations["agent_orientation"]),
observations["agent_carrying"],
)
)
x = th.cat((x_image, x_state))
return self.linear(x)
class DeeperExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.Space):
super().__init__(observation_space, features_dim=64)
# the network does not contain the final projection
# with mlp_depth = 4, set net_arch = []
# else put the mlp layers into a custom network
self.conv = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=(2, 2), stride=1),
nn.GELU(),
nn.Conv2d(16, 32, kernel_size=(2, 2), stride=1),
nn.GELU(),
nn.Conv2d(32, 64, kernel_size=(2, 2), stride=1),
nn.GELU(),
nn.Flatten(),
)
# for some reason the standard is 256
# self.linear = nn.Sequential(nn.Linear(48, 64), nn.ReLU())
def forward(self, observations: th.Tensor) -> th.Tensor:
x = self.conv(observations)
print(x.shape)
return x
class CustomCombinedExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.spaces.Dict):
# We do not know features-dim here before going over all the items,
# so put something dummy for now. PyTorch requires calling
# nn.Module.__init__ before adding modules
super(CustomCombinedExtractor, self).__init__(observation_space, features_dim=1)
extractors = {}
total_concat_size = 0
# We need to know size of the output of this extractor,
# so go over all the spaces and compute output feature sizes
for key, subspace in observation_space.spaces.items():
if key == "image":
extractors[key] = nn.Sequential(
nn.Conv2d(3, 12, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(12, 12, kernel_size=3, stride=1),
nn.ReLU(),
nn.Flatten()
)
total_concat_size += self.feature_size(subspace.shape, extractors[key])
print(total_concat_size)
elif key == "vector":
# keep it the same
extractors[key] = nn.Identity(subspace.shape[0])
total_concat_size += subspace.shape[0]
self.extractors = nn.ModuleDict(extractors)
# Update the features dim manually
self._features_dim = total_concat_size
def feature_size(self, shape, layer):
return layer(autograd.Variable(th.zeros(1, *shape))).view(1, -1).size(1)
def forward(self, observations) -> th.Tensor:
encoded_tensor_list = []
# self.extractors contain nn.Modules that do all the processing.
for key, extractor in self.extractors.items():
encoded_tensor_list.append(extractor(observations[key]))
# Return a (B, self._features_dim) PyTorch tensor, where B is batch dimension.
return th.cat(encoded_tensor_list, dim=1)
class CustomCombinedShallowExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.spaces.Dict):
# We do not know features-dim here before going over all the items,
# so put something dummy for now. PyTorch requires calling
# nn.Module.__init__ before adding modules
super(CustomCombinedExtractor, self).__init__(observation_space, features_dim=1)
extractors = {}
total_concat_size = 0
# We need to know size of the output of this extractor,
# so go over all the spaces and compute output feature sizes
for key, subspace in observation_space.spaces.items():
if key == "image":
extractors[key] = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, stride=1),
nn.ReLU(),
nn.Flatten()
)
total_concat_size += self.feature_size(subspace.shape, extractors[key])
elif key == "vector":
# keep it the same
extractors[key] = nn.Identity(subspace.shape[0])
total_concat_size += subspace.shape[0]
self.extractors = nn.ModuleDict(extractors)
# Update the features dim manually
self._features_dim = total_concat_size
def feature_size(self, shape, layer):
return layer(autograd.Variable(th.zeros(1, *shape))).view(1, -1).size(1)
def forward(self, observations) -> th.Tensor:
encoded_tensor_list = []
# self.extractors contain nn.Modules that do all the processing.
for key, extractor in self.extractors.items():
encoded_tensor_list.append(extractor(observations[key]))
# Return a (B, self._features_dim) PyTorch tensor, where B is batch dimension.
return th.cat(encoded_tensor_list, dim=1)
class CustomDeeperCombinedExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.spaces.Dict):
# We do not know features-dim here before going over all the items,
# so put something dummy for now. PyTorch requires calling
# nn.Module.__init__ before adding modules
super(CustomDeeperCombinedExtractor, self).__init__(observation_space, features_dim=1)
extractors = {}
total_concat_size = 0
# We need to know size of the output of this extractor,
# so go over all the spaces and compute output feature sizes
for key, subspace in observation_space.spaces.items():
if key == "image":
extractors[key] = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=(3, 3), stride=1),
nn.GELU(),
nn.Conv2d(16, 16, kernel_size=(3, 3), stride=1),
nn.GELU(),
nn.Conv2d(16, 16, kernel_size=(3, 3), stride=1),
nn.GELU(),
nn.Flatten(),
)
total_concat_size += self.feature_size(subspace.shape, extractors[key])
print(total_concat_size)
elif key == "vector":
# keep it the same
extractors[key] = nn.Identity(subspace.shape[0])
total_concat_size += subspace.shape[0]
self.extractors = nn.ModuleDict(extractors)
# Update the features dim manually
self._features_dim = total_concat_size
def feature_size(self, shape, layer):
return layer(autograd.Variable(th.zeros(1, *shape))).view(1, -1).size(1)
def forward(self, observations) -> th.Tensor:
encoded_tensor_list = []
# self.extractors contain nn.Modules that do all the processing.
for key, extractor in self.extractors.items():
encoded_tensor_list.append(extractor(observations[key]))
# Return a (B, self._features_dim) PyTorch tensor, where B is batch dimension.
return th.cat(encoded_tensor_list, dim=1)