|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +from gymnasium import spaces |
| 4 | +from stable_baselines3.common.torch_layers import BaseFeaturesExtractor |
| 5 | + |
| 6 | + |
| 7 | +class CNN1DResNetNoCamExtractor(BaseFeaturesExtractor): |
| 8 | + context_size = 1 |
| 9 | + lidar_horizontal_resolution = 1024 |
| 10 | + camera_horizontal_resolution = 0 |
| 11 | + n_sensors = 1 |
| 12 | + |
| 13 | + # just an alias to avoid confusion because |
| 14 | + # the lidar and camera have the same resolution |
| 15 | + horizontal_resolution = 1024 |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + space: spaces.Box, |
| 20 | + device: str = "cpu", |
| 21 | + ): |
| 22 | + net = nn.Sequential( |
| 23 | + # shape = [batch_size, 1, 1024] |
| 24 | + Compressor(device), |
| 25 | + # shape = [batch_size, 64, 256] |
| 26 | + ResidualBlock(64, 64, device=device), |
| 27 | + ResidualBlock(64, 64, device=device), |
| 28 | + ResidualBlock(64, 64, downsample=True, device=device), |
| 29 | + # shape = [batch_size, 128, 128] |
| 30 | + ResidualBlock(64, 64, device=device), |
| 31 | + ResidualBlock(64, 64, device=device), |
| 32 | + ResidualBlock(64, 128, downsample=True, device=device), |
| 33 | + # shape = [batch_size, 128, 64] |
| 34 | + ResidualBlock(128, 128, device=device), |
| 35 | + ResidualBlock(128, 128, device=device), |
| 36 | + ResidualBlock(128, 128, downsample=True, device=device), |
| 37 | + # shape = [batch_size, 256, 32] |
| 38 | + ResidualBlock(128, 128, device=device), |
| 39 | + ResidualBlock(128, 128, device=device), |
| 40 | + ResidualBlock(128, 256, downsample=True, device=device), |
| 41 | + # shape = [batch_size, 256, 16] |
| 42 | + ResidualBlock(256, 256, device=device), |
| 43 | + ResidualBlock(256, 256, device=device), |
| 44 | + ResidualBlock(256, 256, downsample=True, device=device), |
| 45 | + # shape = [batch_size, 256, 8] |
| 46 | + nn.AvgPool1d(8), |
| 47 | + # shape = [batch_size, 256, 1] |
| 48 | + nn.Flatten(), |
| 49 | + # shape = [batch_size, 256] |
| 50 | + ) |
| 51 | + |
| 52 | + # Compute shape by doing one forward pass |
| 53 | + with torch.no_grad(): |
| 54 | + n_flatten = net( |
| 55 | + torch.zeros( |
| 56 | + [1, 1, self.context_size, self.horizontal_resolution], device=device |
| 57 | + ) |
| 58 | + ).shape[1] |
| 59 | + |
| 60 | + super().__init__(space, n_flatten) |
| 61 | + |
| 62 | + # we cannot assign this directly to self.cnn before calling the super constructor |
| 63 | + self.net = net |
| 64 | + |
| 65 | + def forward(self, observations: torch.Tensor) -> torch.Tensor: |
| 66 | + return self.net(observations) |
| 67 | + |
| 68 | + |
| 69 | +class Compressor(nn.Module): |
| 70 | + def __init__(self, device: str = "cpu"): |
| 71 | + super().__init__() |
| 72 | + # WARNING : do not use inplace=True because it would modify the rollout buffer |
| 73 | + self.conv = nn.Conv1d(1, 64, kernel_size=7, stride=2, padding=3, device=device) |
| 74 | + self.dropout = nn.Dropout1d(0.3) |
| 75 | + self.pool = nn.MaxPool1d(kernel_size=3, stride=2, padding=1) |
| 76 | + |
| 77 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 78 | + x = x[:, :, 0] |
| 79 | + x = self.conv(x) |
| 80 | + x = self.dropout(x) |
| 81 | + x = self.pool(x) |
| 82 | + return x |
| 83 | + |
| 84 | + |
| 85 | +class ResidualBlock(nn.Module): |
| 86 | + """ |
| 87 | + basic block with a residual connection |
| 88 | + """ |
| 89 | + |
| 90 | + def __init__( |
| 91 | + self, |
| 92 | + in_channels: int, |
| 93 | + out_channels: int, |
| 94 | + downsample: bool = False, |
| 95 | + device: str = "cpu", |
| 96 | + ): |
| 97 | + super().__init__() |
| 98 | + if downsample: |
| 99 | + stride = 2 |
| 100 | + self.downsample = nn.Conv1d( |
| 101 | + in_channels, out_channels, kernel_size=1, stride=2, device=device |
| 102 | + ) |
| 103 | + elif in_channels == out_channels: |
| 104 | + stride = 1 |
| 105 | + self.downsample = nn.Identity() |
| 106 | + else: |
| 107 | + stride = 1 |
| 108 | + self.downsample = nn.Conv1d( |
| 109 | + in_channels, out_channels, kernel_size=1, stride=1, device=device |
| 110 | + ) |
| 111 | + |
| 112 | + self.bn1 = nn.BatchNorm1d(in_channels, device=device) |
| 113 | + self.conv1 = nn.Conv1d( |
| 114 | + in_channels, |
| 115 | + out_channels, |
| 116 | + kernel_size=3, |
| 117 | + stride=stride, |
| 118 | + padding=1, |
| 119 | + device=device, |
| 120 | + ) |
| 121 | + |
| 122 | + self.bn2 = nn.BatchNorm1d(out_channels, device=device) |
| 123 | + self.conv2 = nn.Conv1d( |
| 124 | + out_channels, out_channels, kernel_size=3, padding=1, device=device |
| 125 | + ) |
| 126 | + |
| 127 | + self.relu = nn.ReLU(inplace=True) |
| 128 | + self.dropout = nn.Dropout1d(0.4) |
| 129 | + |
| 130 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 131 | + y = self.bn1(x) |
| 132 | + y = self.relu(y) |
| 133 | + y = self.conv1(y) |
| 134 | + |
| 135 | + y = self.bn2(y) |
| 136 | + y = self.relu(y) |
| 137 | + y = self.dropout(y) |
| 138 | + y = self.conv2(y) |
| 139 | + |
| 140 | + y += self.downsample(x) |
| 141 | + |
| 142 | + return y |
0 commit comments