Flare encoder of pixels observations.
| 149 | |
| 150 | |
| 151 | class PixelDelta2DEncoder(nn.Module): |
| 152 | """Flare encoder of pixels observations.""" |
| 153 | def __init__(self, obs_shape, feature_dim, |
| 154 | channels=[16, 32, 32], |
| 155 | num_layers=2, |
| 156 | num_filters=32, |
| 157 | output_logits=False, |
| 158 | image_channel=3): |
| 159 | super().__init__() |
| 160 | |
| 161 | assert len(obs_shape) == 3 |
| 162 | self.obs_shape = obs_shape |
| 163 | self.feature_dim = feature_dim |
| 164 | self.num_layers = num_layers |
| 165 | self.image_channel = image_channel |
| 166 | |
| 167 | time_step = obs_shape[0] // self.image_channel |
| 168 | |
| 169 | self.convs = nn.ModuleList( |
| 170 | [nn.Conv2d(self.image_channel, num_filters, 3, stride=2)] |
| 171 | ) |
| 172 | self.convs.append(nn.Conv2d(num_filters, num_filters, 3, stride=1)) |
| 173 | self.convs.append(nn.Conv2d(num_filters, num_filters, 3, stride=1)) |
| 174 | for i in range(2, num_layers - 1): |
| 175 | self.convs.append(nn.Conv2d(num_filters, num_filters, 3, stride=1)) |
| 176 | self.outputs = dict() |
| 177 | |
| 178 | x = torch.randn([32]+list(obs_shape)) |
| 179 | self.out_dim = self.forward_conv(x,flatten=False).shape[-1] |
| 180 | |
| 181 | print('conv output dim: ' + str(self.out_dim)) |
| 182 | |
| 183 | self.fc = nn.Linear(num_filters * self.out_dim * self.out_dim * (2*time_step-2), self.feature_dim) |
| 184 | self.ln = nn.LayerNorm(self.feature_dim) |
| 185 | |
| 186 | self.output_logits = output_logits |
| 187 | |
| 188 | |
| 189 | def reparameterize(self, mu, logstd): |
| 190 | std = torch.exp(logstd) |
| 191 | eps = torch.randn_like(std) |
| 192 | return mu + eps * std |
| 193 | |
| 194 | def forward_conv(self, obs,flatten=True): |
| 195 | if obs.max() > 1.: |
| 196 | obs = obs / 255. |
| 197 | |
| 198 | time_step = obs.shape[1] // self.image_channel |
| 199 | obs = obs.view(obs.shape[0], time_step, self.image_channel, obs.shape[-2], obs.shape[-1]) |
| 200 | obs = obs.view(obs.shape[0]*time_step, self.image_channel, obs.shape[-2], obs.shape[-1]) |
| 201 | |
| 202 | self.outputs['obs'] = obs |
| 203 | conv = torch.relu(self.convs[0](obs)) |
| 204 | self.outputs['conv1'] = conv |
| 205 | |
| 206 | conv = torch.relu(self.convs[1](conv)) |
| 207 | self.outputs['conv%s' % (1 + 1)] = conv |
| 208 |
nothing calls this directly
no outgoing calls
no test coverage detected