-
Notifications
You must be signed in to change notification settings - Fork 4
/
impala.py
203 lines (170 loc) · 6.45 KB
/
impala.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from simple_bc._interfaces.encoder import Encoder
from simple_bc.utils.torch_utils import to_torch, to_numpy
import hydra
from omegaconf import DictConfig
class IMPALA(Encoder):
def __init__(self,
in_channels,
shape,
use_depth=True,
large=True,
larger=True,
num_views=2,
**kwargs):
super().__init__(**kwargs)
self.feat_convs = []
self.resnet1 = []
self.resnet2 = []
self.convs = []
self.use_depth = use_depth
self.num_views = num_views
H, W = shape
if larger:
fcs = [128, 128, 128]
else:
fcs = [64, 64, 64]
self.shape = [H, W]
self.large = large
if self.large:
in_channels = 4
print("IMPALA: using large network, so using 4 channels, and not convolving over time and views.")
self.stem = nn.Conv2d(in_channels, fcs[0], kernel_size=4, stride=4)
in_channels = fcs[0]
for num_ch in fcs:
feats_convs = []
feats_convs.append(
nn.Conv2d(
in_channels=in_channels,
out_channels=num_ch,
kernel_size=3,
stride=1,
padding=1,
)
)
feats_convs.append(nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
self.feat_convs.append(nn.Sequential(*feats_convs))
in_channels = num_ch
for i in range(2):
resnet_block = []
resnet_block.append(nn.ReLU())
resnet_block.append(
nn.Conv2d(
in_channels=in_channels,
out_channels=num_ch,
kernel_size=3,
stride=1,
padding=1,
)
)
resnet_block.append(nn.ReLU())
resnet_block.append(
nn.Conv2d(
in_channels=in_channels,
out_channels=num_ch,
kernel_size=3,
stride=1,
padding=1,
)
)
if i == 0:
self.resnet1.append(nn.Sequential(*resnet_block))
else:
self.resnet2.append(nn.Sequential(*resnet_block))
self.feat_convs = nn.ModuleList(self.feat_convs)
self.resnet1 = nn.ModuleList(self.resnet1)
self.resnet2 = nn.ModuleList(self.resnet2)
self.img_feat_size = (H * W) // (4 ** len(fcs) * 16) * fcs[-1]
if self.large:
self.fc = nn.Identity()
self.out_shape = self.img_feat_size * self.num_views * self.num_frames
else:
self.fc = nn.Linear(self.img_feat_size, self.out_shape)
self._update_out_shape(self.out_shape)
def preprocess(self, obs):
with torch.no_grad():
feature = []
if "rgb" in self.obs_shapes and "rgb" in obs:
B = len(obs["rgb"])
assert (
obs["rgb"].shape[-3:] == self.obs_shapes["rgb"]
), f"Observation shape of rgb is {obs['rgb'].shape}, but should be {(B, *self.obs_shapes['rgb'])}"
# B, F (Frames), V (views), C, H, W
if self.large:
rgb = rearrange(obs["rgb"], "b f v c h w -> (b f v) c h w")
else:
rgb = rearrange(obs["rgb"], "b f v c h w -> b (f v c) h w")
feature.append((rgb / 255.0))
if "depth" in self.obs_shapes and "depth" in obs:
B = len(obs["depth"])
assert (
obs["depth"].shape[-3:] == self.obs_shapes["depth"]
), f"Observation shape of depth is {obs['depth'].shape}, but should be {(B, *self.obs_shapes['depth'])}"
if self.large:
depth = rearrange(obs["depth"], "b f v c h w -> (b f v) c h w")
else:
depth = rearrange(obs["depth"], "b f v c h w -> b (f v c) h w")
if not self.use_depth:
depth = torch.zeros_like(depth)
feature.append(depth)
feature = torch.cat(feature, dim=1)
if "state" in obs:
state = obs["state"]
else:
state = None
return feature, state
def forward(self, obs):
"""Return feature and info."""
feature, state = self.preprocess(obs)
x = self.stem(feature)
res_input = None
for i, fconv in enumerate(self.feat_convs):
x = fconv(x)
res_input = x
x = self.resnet1[i](x)
x += res_input
res_input = x
x = self.resnet2[i](x)
x += res_input
x = F.relu(x)
x = x.reshape(x.shape[0], self.img_feat_size)
x = F.relu(self.fc(x))
if self.large:
x = rearrange(x, "(b f v) c -> b (f v c)", f=self.num_frames, v=self.num_views)
B = x.shape[0]
state = state.view(B, -1)
out = torch.cat([x, state], dim=1)
return out, {}
def _update_out_shape(self, out_shape):
if "state" in self.obs_shapes:
state_shape = np.prod(self.obs_shapes["state"]) * self.num_frames
print(f"IMPALA: updated out shape to {self.out_shape + state_shape}")
self.out_shape = out_shape + state_shape
else:
self.out_shape = out_shape
@hydra.main(config_path="../../conf/encoder", config_name="impala", version_base="1.1")
def test(cfg):
cfg = DictConfig(cfg)
cfg.in_channels = 32
cfg.shape = [224, 224]
cfg.obs_shapes = {"rgb": [3, 224, 224], "depth": [1, 224, 224], "state": [26]}
cfg.num_frames = 4
cfg.out_shape = 384
print(cfg)
encoder = IMPALA(**cfg)
print(encoder)
obs = {
"rgb": to_torch(torch.randn(6, 4, 2, 3, 224, 224)),
"depth": to_torch(torch.randn(6, 4, 2, 1, 224, 224)),
"state": to_torch(torch.randn(6, 4, 26)),
}
out, _ = encoder(obs)
assert (
out.shape[1:] == encoder.out_shape
), f"out shape is {out.shape}, but should be {encoder.out_shape}"
if __name__ == "__main__":
test()