| 85 | |
| 86 | # Downsample observations before representation network (See paper appendix Network Architecture) |
| 87 | class DownSample(nn.Module): |
| 88 | def __init__(self, in_channels, out_channels, momentum=0.1): |
| 89 | super().__init__() |
| 90 | self.conv1 = nn.Conv2d( |
| 91 | in_channels, |
| 92 | out_channels // 2, |
| 93 | kernel_size=3, |
| 94 | stride=2, |
| 95 | padding=1, |
| 96 | bias=False, |
| 97 | ) |
| 98 | self.bn1 = nn.BatchNorm2d(out_channels // 2, momentum=momentum) |
| 99 | self.resblocks1 = nn.ModuleList( |
| 100 | [ResidualBlock(out_channels // 2, out_channels // 2, momentum=momentum) for _ in range(1)] |
| 101 | ) |
| 102 | self.conv2 = nn.Conv2d( |
| 103 | out_channels // 2, |
| 104 | out_channels, |
| 105 | kernel_size=3, |
| 106 | stride=2, |
| 107 | padding=1, |
| 108 | bias=False, |
| 109 | ) |
| 110 | self.downsample_block = ResidualBlock(out_channels // 2, out_channels, momentum=momentum, stride=2, downsample=self.conv2) |
| 111 | self.resblocks2 = nn.ModuleList( |
| 112 | [ResidualBlock(out_channels, out_channels, momentum=momentum) for _ in range(1)] |
| 113 | ) |
| 114 | self.pooling1 = nn.AvgPool2d(kernel_size=3, stride=2, padding=1) |
| 115 | self.resblocks3 = nn.ModuleList( |
| 116 | [ResidualBlock(out_channels, out_channels, momentum=momentum) for _ in range(1)] |
| 117 | ) |
| 118 | self.pooling2 = nn.AvgPool2d(kernel_size=3, stride=2, padding=1) |
| 119 | |
| 120 | def forward(self, x): |
| 121 | x = self.conv1(x) |
| 122 | x = self.bn1(x) |
| 123 | x = nn.functional.relu(x) |
| 124 | for block in self.resblocks1: |
| 125 | x = block(x) |
| 126 | x = self.downsample_block(x) |
| 127 | for block in self.resblocks2: |
| 128 | x = block(x) |
| 129 | x = self.pooling1(x) |
| 130 | for block in self.resblocks3: |
| 131 | x = block(x) |
| 132 | x = self.pooling2(x) |
| 133 | return x |
| 134 | |
| 135 | |
| 136 | # Encode the observations into hidden states |