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
| class DARTSCell(nn.Module): """DARTS的Cell结构""" def __init__(self, in_channels, out_channels, num_nodes=4, is_reduction=False): super().__init__() self.num_nodes = num_nodes self.is_reduction = is_reduction if is_reduction: self.preprocess0 = nn.Sequential( nn.Conv2d(in_channels, out_channels, 1, stride=2, bias=False), nn.BatchNorm2d(out_channels) ) self.preprocess1 = nn.Sequential( nn.Conv2d(in_channels, out_channels, 1, stride=2, bias=False), nn.BatchNorm2d(out_channels) ) else: self.preprocess0 = nn.Sequential( nn.Conv2d(in_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels) ) self.preprocess1 = nn.Sequential( nn.Conv2d(in_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels) ) self._ops = nn.ModuleList([ nn.Sequential( nn.Conv2d(out_channels, out_channels, 3, padding=1, bias=False, groups=out_channels), nn.BatchNorm2d(out_channels), nn.Conv2d(out_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels) ), nn.Sequential( nn.Conv2d(out_channels, out_channels, 5, padding=2, bias=False, groups=out_channels), nn.BatchNorm2d(out_channels), nn.Conv2d(out_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels) ), nn.Sequential( nn.AvgPool2d(3, padding=1, stride=1), nn.BatchNorm2d(out_channels) ), nn.Sequential( nn.MaxPool2d(3, padding=1, stride=1), nn.BatchNorm2d(out_channels) ), nn.Sequential( nn.Identity(), nn.BatchNorm2d(out_channels, affine=False) ), ]) self.edge_weights = nn.Parameter( torch.randn(num_nodes, len(self._ops)) * 1e-3 ) def forward(self, s0, s1): s0 = self.preprocess0(s0) s1 = self.preprocess1(s1) states = [s0, s1] for node_idx in range(self.num_nodes): node_result = torch.zeros_like(s0) for prev_idx in range(node_idx + 2): weights = F.softmax(self.edge_weights[node_idx], dim=0) for op_idx, op in enumerate(self._ops): node_result += weights[op_idx] * op(states[prev_idx]) states.append(node_result) return torch.cat(states[-self.num_nodes:], dim=1)
class DARTSNetwork(nn.Module): """DARTS网络""" def __init__(self, num_classes=10, num_layers=8, channels=36): super().__init__() self.num_layers = num_layers self.channels = channels self.stem = nn.Sequential( nn.Conv2d(3, channels, 3, padding=1, bias=False), nn.BatchNorm2d(channels) ) self.cells = nn.ModuleList() self.reductions = nn.ModuleList() for i in range(num_layers): if i in [num_layers // 3, 2 * num_layers // 3]: self.cells.append( DARTSCell(channels, channels * 2, is_reduction=True) ) channels *= 2 else: self.cells.append( DARTSCell(channels, channels) ) self.classifier = nn.Sequential( nn.ReLU(inplace=True), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(channels, num_classes) ) def forward(self, x): x = self.stem(x) s0 = s1 = x for cell in self.cells: s0, s1 = s1, cell(s0, s1) return self.classifier(s1)
|